Skip to content

Commit dca84b6

Browse files
committed
Terraform syntax - part 2
1 parent 5037328 commit dca84b6

File tree

3 files changed

+61
-29
lines changed

3 files changed

+61
-29
lines changed

main.tf

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,31 @@
1-
terraform {
2-
required_providers {
3-
aws = {
4-
source = "hashicorp/aws"
5-
version = "~> 3.0"
6-
}
7-
}
8-
}
9-
10-
provider "aws" {
11-
region = var.region
12-
}
1+
//Using for_each meta-argument
132

14-
variable "region" {
15-
default = "us-west-1"
16-
description = "AWS Region"
17-
}
3+
resource "aws_instance" "demo" {
4+
for_each = {
5+
fruit = "apple"
6+
vehicle = "car"
7+
continent = "Europe"
8+
}
9+
provider = aws.aws_west
10+
ami = var.ami
11+
instance_type = var.type
1812

19-
variable "ami" {
20-
default = "ami-00831fc7c1e3ddc60"
21-
description = "Amazon Machine Image ID for Ubuntu Server 20.04"
13+
tags = {
14+
name = "${each.key}: ${each.value}"
15+
}
2216
}
2317

24-
variable "type" {
25-
default = "t2.micro"
26-
description = "Size of VM"
27-
}
18+
/*
19+
Using count meta-argument
2820
2921
resource "aws_instance" "demo" {
30-
ami = var.ami
22+
count = 3
23+
provider = aws.aws_west
24+
ami = var.ami
3125
instance_type = var.type
3226
3327
tags = {
3428
name = "Demo System"
3529
}
3630
}
37-
38-
output "instance_id" {
39-
value = aws_instance.demo.id
40-
}
41-
31+
*/

provider.tf

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
terraform {
2+
required_providers {
3+
aws = {
4+
source = "hashicorp/aws"
5+
version = "~> 3.0"
6+
}
7+
}
8+
}
9+
10+
provider "aws" {
11+
alias = "aws_west"
12+
region = var.region_west
13+
}
14+
15+
provider "aws" {
16+
alias = "aws_east"
17+
region = var.region_east
18+
}

variables.tf

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
variable "region_west" {
2+
default = "us-west-1"
3+
description = "AWS West Region"
4+
}
5+
6+
variable "region_east" {
7+
default = "us-east-1"
8+
description = "AWS East Region"
9+
}
10+
11+
variable "ami" {
12+
default = "ami-00831fc7c1e3ddc60"
13+
description = "Amazon Machine Image ID for Ubuntu Server 20.04"
14+
}
15+
16+
variable "type" {
17+
default = "t2.micro"
18+
description = "Size of VM"
19+
}
20+
21+
output "instance_id" {
22+
//value = aws_instance.demo[*].id
23+
value = [for b in aws_instance.demo : b.id]
24+
}

0 commit comments

Comments
 (0)