๐กcloudNet@ ํ์ ๊ฐ์๋ค ๋์ด ์งํํ๋ Terraform 101 Study 4๊ธฐ 2์ฃผ์ฐจ ๋ด์ฉ์ผ๋ก,
[ํ ๋ผํผ์ผ๋ก ์์ํ๋ IaC] ๋์๋ฅผ ์ฐธ์กฐํ์์ต๋๋ค. ๊ฐ์ฌํฉ๋๋ค..
cloudnet ์คํฐ๋ ์ ๋ถ๋์ด count๋ฅผ ์ค์ตํด๋ณผ ์ ์๋ ์ข์ ์๋๋ฆฌ์ค๋ฅผ ๊ณต์ ํด์ฃผ์ จ์ต๋๋ค. (๊ฐ์ฌํฉ๋๋ค)
ํ๋ฒ ๋ฐ๋ผ์ ์ ๋ฆฌํด๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค.
1. count ์ค์ต (์คํฐ๋ ์ ๋ถ๋ ์ ๊ณต)
๋ค์๊ณผ ๊ฐ์ด ์ค์ตํ๊ฒฝ์ ๊ตฌ์ฑํ๋ค. ์ด 10๊ฐ์ ์๋๋ฆฌ์ค๋ฅผ ํตํด ํ์ตํ ๋ด์ฉ์ ํ์ธํด ๋ณด๊ฒ ๋ค.
์๋๋ฆฌ์ค 1. aws_subnet
- AWS VPC Subnet ํ ๋ผํผ ์ฝ๋ ์์ฑ
- ์๊ตฌ์ฌํญ : subnet cidr๋ฅผ ๋ณ์๋ก ์ ๋ ฅ
๋ค์๊ณผ ๊ฐ์ด main.tf, variables.tf, terraform.tfvars ์ด 3๊ฐ์ ํ์ผ์ ์์ฑํ๋ค.
# main.tf
resource "aws_vpc" "main" {
cidr_block = var.vpc_cidr
tags = {
Name = "terraform VPC"
}
}
resource "aws_subnet" "main" {
vpc_id = aws_vpc.main.id
cidr_block = **var.subnet_cidr**
}
**output "myvpc_id" {
value = aws_vpc.main.id
}**
# variables.tf
variable "vpc_cidr" {
type = string
}
**variable "subnet_cidr" {
type = string
}**
# terraform.tfvars
vpc_cidr = "192.168.0.0/16"
subnet_cidr = "192.168.1.0/24"
๋ฐฐํฌ ์คํ ๊ฒฐ๊ณผ
์๋๋ฆฌ์ค 2. index element length
- ์๊ตฌ์ฌํญ : subnet cidr ๋ณ์ ๊ฐ์ ์ฌ๋ฌ ๊ฐ ์ ๋ ฅ๋ฐ๋๋ก ๊ตฌ์ฑ
๋ค์๊ณผ ๊ฐ์ด main.tf, variables.tf, terraform.tfvars ์ด 3๊ฐ์ ํ์ผ์ ์์ ํ๋ค.
# main.tf
resource "aws_vpc" "main" {
cidr_block = var.vpc_cidr
tags = {
Name = "terraform VPC"
}
}
resource "aws_subnet" "main" {
**count = length(var.subnet_cidr)**
vpc_id = aws_vpc.main.id
cidr_block = **element(var.subnet_cidr, count.index)**
}
output "myvpc_id" {
value = aws_vpc.main.id
}
# variables.tf
variable "vpc_cidr" {
type = string
}
**variable "subnet_cidr" {
type = list(string)
}**
# terraform.tfvars
vpc_cidr = "192.168.0.0/16"
subnet_cidr = **[**"192.168.1.0/24"**, "192.168.2.0/24"]**
๋ฐฐํฌ ์คํ ๊ฒฐ๊ณผ
์๋๋ฆฌ์ค 3. aws_subnet_arg_az (AZ for the subnet)
- ์๊ตฌ์ฌํญ : ์๋ธ๋ท์ด ๋ฐฐ์น๋๋ AZ ์ค์ ์ด ํ์ํ๋ค. AZ๋ ๋ณ์๋ก ์ ๋ ฅํ๋๋ก ๊ตฌ์ฑ
# main.tf
resource "aws_vpc" "main" {
cidr_block = var.vpc_cidr
tags = {
Name = "terraform VPC"
}
}
resource "aws_subnet" "main" {
**count = length(var.subnet_cidr)**
vpc_id = aws_vpc.main.id
cidr_block = **element(var.subnet_cidr, count.index)
availability_zone = element(var.subnet_az, count.index)**
}
output "myvpc_id" {
value = aws_vpc.main.id
}
# variables.tf
variable "vpc_cidr" {
type = string
}
variable "subnet_cidr" {
type = list(string)
}
**variable "subnet_az" {
type = list(string)
}**
# terraform.tfvars
vpc_cidr = "192.168.0.0/16"
subnet_cidr = ["192.168.1.0/24", "192.168.2.0/24"]
**subnet_az = ["ap-northeast-2a", "ap-northeast-2c"]**
๋ฐฐํฌ ์คํ ๊ฒฐ๊ณผ, AZ๊น์ง ์ ๋๋์ด์ ธ ์ฝ์์์๋ ์ ๋ ฌ์ด ๋ถ๋ฆฌ๋์๋ค.
์๋๋ฆฌ์ค 4. aws_subnet-arg_tags (A map of tags assigned to the resource) map
- ์๊ตฌ์ฌํญ : ์ ์๋๋ฆฌ์ค 3 ์ด๋ฏธ์ง๋ฅผ ๋ณด๋ฉด name์ด ๋น์ด์๋ค. subnet tag ์ค์ ์ ์ถ๊ฐํ๋ค.
mian.tf ์ ๋ค์๊ณผ ๊ฐ์ด ์์ ํ๋ค.
# main.tf
resource "aws_vpc" "main" {
cidr_block = var.vpc_cidr
tags = {
Name = "terraform VPC"
}
}
resource "aws_subnet" "main" {
**count = length(var.subnet_cidr)**
vpc_id = aws_vpc.main.id
cidr_block = **element(var.subnet_cidr, count.index)
availability_zone = element(var.subnet_az, count.index)**
}
output "myvpc_id" {
value = aws_vpc.main.id
}
๋ฐฐํฌ ์คํ ๊ฒฐ๊ณผ
์๋๋ฆฌ์ค 5. map
- ์๊ตฌ์ฌํญ : subnet tag๋ฅผ ์ค์ ํ๋ ๋ณ์ ์์ฑ
# main.tf
resource "aws_vpc" "main" {
cidr_block = var.vpc_cidr
tags = {
Name = "terraform VPC"
}
}
resource "aws_subnet" "main" {
count = length(var.subnet_cidr)
vpc_id = aws_vpc.main.id
cidr_block = element(var.subnet_cidr, count.index)
availability_zone = element(var.subnet_az, count.index)
**tags = element(var.subnet_tag, count.index)**
}
output "myvpc_id" {
value = aws_vpc.main.id
}
# variables.tf
variable "vpc_cidr" {
type = string
}
variable "subnet_cidr" {
type = list(string)
}
variable "subnet_az" {
type = list(string)
}
**variable "subnet_tag" {
type = list(map(string))
}**
# terraform.tfvars
vpc_cidr = "192.168.0.0/16"
subnet_cidr = ["192.168.1.0/24", "192.168.2.0/24"]
subnet_az = ["ap-northeast-2a", "ap-northeast-2c"]
**subnet_tag = [
{
Name = "public-subnet"
Environment = "dev"
},
{
Name = "private-subnet"
Environment = "dev"
}
]**
๋ฐฐํฌ ์คํ ๊ฒฐ๊ณผ
์๋๋ฆฌ์ค 6. (๋ฆฌํฉํ ๋ง) - map_list_object
- ์๊ตฌ์ฌํญ : subnet ์ค์ ๋ณ์๋ฅผ ํ ๋ณ์๋ก ์ค์ ํ๋๋ก ๋ฆฌํฉํ ๋ง ๊ตฌ์ฑ
# main.tf
resource "aws_vpc" "main" {
cidr_block = var.vpc_cidr
tags = {
Name = "terraform VPC"
}
}
resource "aws_subnet" "main" {
count = length(var.subnets) # subnet_cidr > subnets ๋ณ๊ฒฝ
vpc_id = aws_vpc.main.id
cidr_block = **var.subnets[count.index].cidr # elment์์ ๋ณ๊ฒฝ**
availability_zone = **var.subnets[count.index].az**
tags = **var.subnets[count.index].tags #elment์์ ๋ณ๊ฒฝ**
}
output "myvpc_id" {
value = aws_vpc.main.id
}
# variables.tf
variable "vpc_cidr" {
type = string
}
**variable "subnets" {
type = list(object({
cidr = string
az = string
tags = map(string)
}))
}**
# terraform.tfvars
vpc_cidr = "192.168.0.0/16"
**subnets = [
{
cidr = "192.168.1.0/24",
az = "ap-northeast-2a",
tags = {
Name = "public-subnet"
Environment = "dev"
}
},
{
cidr = "192.168.2.0/24",
az = "ap-northeast-2c",
tags = {
Name = "private-subnet"
Environment = "dev"
}
}**
]
๋ฐฐํฌ ์คํ ๊ฒฐ๊ณผ
์๋๋ฆฌ์ค 7. ์ฅ์ ์ํฉ ์ฌํ
- ์๊ตฌ์ฌํญ : ์ค๋ฅ ํ์ธ ํ ์ฝ๋ ์์
๊ธฐ์กด VPC, Subnet ๊ตฌ์ฑ์ ์ ๊ฑฐํ์๊ณ , ๋ค์๊ณผ ๊ฐ์ด ์ค์ต ํ๊ฒฝ์ ๊ตฌ์ฑ/๋ฐฐํฌ ํ๋ค.
# main
resource "aws_vpc" "main" {
cidr_block = var.vpc_cidr
tags = {
Name = "terraform VPC"
}
}
resource "aws_subnet" "main" {
count = length(var.subnets)
vpc_id = aws_vpc.main.id
cidr_block = var.subnets[count.index].cidr
availability_zone = var.subnets[count.index].az
tags = var.subnets[count.index].tags
}
resource "aws_instance" "server" {
ami = "ami-0e8bd0820b6e1360b"
instance_type = "t4g.nano"
subnet_id = aws_subnet.main[1].id
# index ์ ๊ทผ ๋ฐฉ๋ฒ ์ค๋ฅ ํด๊ฒฐ ์ฝ๋
# subnet_id = index(aws_subnet.main.*.cidr_block, "192.168.2.0/24")
tags = {
Name = "Terraform demo"
}
}
# ์ค์ต ํธ๋ฆฌ๋ฅผ ์ํด output ์ถ๊ฐ
output "myvpc_id" {
value = aws_vpc.main.id
}
# variables.tf
variable "vpc_cidr" {
type = string
}
variable "subnets" {
type = list(object({
cidr = string
az = string
tags = map(string)
}))
}
# terraform.tfvars
vpc_cidr = "192.168.0.0/16"
subnets = [
# (์ ํฌ๋ธ 7๋ฒ์งธ ์๋๋ฆฌ์ค) terrafprm apply ์ด ํ, ์ฒซ ๋ฒ์งธ ์์๋ฅผ ์ฃผ์ํ์ธ์
{
cidr = "192.168.1.0/24",
az = "ap-northeast-2a",
tags = {
Name = "public-subnet"
Environment = "dev"
}
},
# (์ ํฌ๋ธ 8๋ฒ์งธ ์๋๋ฆฌ์ค) terrafprm apply ์ด ํ, ์ฃผ์์ ํด์ ํ๊ณ terraform applyํด๋ณด์ธ์
# {
# cidr = "192.168.5.0/24",
# az = "ap-northeast-2a",
# tags = {
# Name = "public-subnet"
# Environment = "dev"
# }
# },
{
cidr = "192.168.2.0/24",
az = "ap-northeast-2a",
tags = {
Name = "private-subnet"
Environment = "dev"
}
},
{
cidr = "192.168.3.0/24",
az = "ap-northeast-2a",
tags = {
Name = "public-subnet"
Environment = "dev"
}
},
{
cidr = "192.168.4.0/24",
az = "ap-northeast-2c",
tags = {
Name = "private-subnet"
Environment = "dev"
}
}
]
๋ค์ ์์ฑํ VPC์ 4๊ฐ์ ์๋ธ๋ท, ๊ทธ๋ฆฌ๊ณ EC2 1๊ฐ๊ฐ ์์ฑ๋์๋ค. EC2๋ private-subnet 192.168.2.0/24 ์๋ธ๋ท์ผ๋ก ๊ตฌ์ฑ๋์ด ์๋ค.
!! ์ฅ์ ์ฌํ : terraform.tfvars ์์
ํด๋น ๋ถ๋ถ ์ฃผ์์ฒ๋ฆฌํ์ฌ ์ฅ์ ๋ฅผ ๋ฐ์์ํจ๋ค.
vpc_cidr = "192.168.0.0/16"
subnets = [
# (์ ํฌ๋ธ 7๋ฒ์งธ ์๋๋ฆฌ์ค) terrafprm apply ์ด ํ, ์ฒซ ๋ฒ์งธ ์์๋ฅผ ์ฃผ์ํ์ธ์
#{
# cidr = "192.168.1.0/24",
# az = "ap-northeast-2a",
# tags = {
# Name = "public-subnet"
# Environment = "dev"
# }
#},
# (์ ํฌ๋ธ 8๋ฒ์งธ ์๋๋ฆฌ์ค) terrafprm apply ์ด ํ, ์ฃผ์์ ํด์ ํ๊ณ terraform applyํด๋ณด์ธ์
# {
# cidr = "192.168.5.0/24",
# az = "ap-northeast-2a",
# tags = {
# Name = "public-subnet"
# Environment = "dev"
# }
# },
{
cidr = "192.168.2.0/24",
az = "ap-northeast-2a",
tags = {
Name = "private-subnet"
Environment = "dev"
}
},
{
cidr = "192.168.3.0/24",
az = "ap-northeast-2a",
tags = {
Name = "public-subnet"
Environment = "dev"
}
},
{
cidr = "192.168.4.0/24",
az = "ap-northeast-2c",
tags = {
Name = "private-subnet"
Environment = "dev"
}
}
]
plan์ ์คํํด ๋ณ๊ฒฝ ๋ด์ฉ์ ํ์ธํด๋ณด๊ฒ ๋ค.
# aws_subnet.main[0] must be replaced
-/+ resource "aws_subnet" "main" {
~ arn = "arn:aws:ec2:ap-northeast-2:891376941699:subnet/subnet-070df28e60c3fef07" -> (known after apply)
~ availability_zone_id = "apne2-az1" -> (known after apply)
~ cidr_block = "192.168.1.0/24" -> "192.168.2.0/24" # forces replacement
- enable_lni_at_device_index = 0 -> null
~ id = "subnet-070df28e60c3fef07" -> (known after apply)
+ ipv6_cidr_block_association_id = (known after apply)
- map_customer_owned_ip_on_launch = false -> null
~ owner_id = "891376941699" -> (known after apply)
~ private_dns_hostname_type_on_launch = "ip-name" -> (known after apply)
~ tags = {
"Environment" = "dev"
~ "Name" = "public-subnet" -> "private-subnet"
}
~ tags_all = {
~ "Name" = "public-subnet" -> "private-subnet"
# (1 unchanged element hidden)
}
# (11 unchanged attributes hidden)
}
# aws_subnet.main[1] must be replaced
-/+ resource "aws_subnet" "main" {
~ arn = "arn:aws:ec2:ap-northeast-2:891376941699:subnet/subnet-040a2fc0e8c278c0a" -> (known after apply)
~ availability_zone_id = "apne2-az1" -> (known after apply)
~ cidr_block = "192.168.2.0/24" -> "192.168.3.0/24" # forces replacement
- enable_lni_at_device_index = 0 -> null
~ id = "subnet-040a2fc0e8c278c0a" -> (known after apply)
+ ipv6_cidr_block_association_id = (known after apply)
- map_customer_owned_ip_on_launch = false -> null
~ owner_id = "891376941699" -> (known after apply)
~ private_dns_hostname_type_on_launch = "ip-name" -> (known after apply)
~ tags = {
"Environment" = "dev"
~ "Name" = "private-subnet" -> "public-subnet"
}
~ tags_all = {
~ "Name" = "private-subnet" -> "public-subnet"
# (1 unchanged element hidden)
}
# (11 unchanged attributes hidden)
}
# aws_subnet.main[2] must be replaced
-/+ resource "aws_subnet" "main" {
~ arn = "arn:aws:ec2:ap-northeast-2:891376941699:subnet/subnet-07846067fffb97419" -> (known after apply)
~ availability_zone = "ap-northeast-2a" -> "ap-northeast-2c" # forces replacement
~ availability_zone_id = "apne2-az1" -> (known after apply)
~ cidr_block = "192.168.3.0/24" -> "192.168.4.0/24" # forces replacement
- enable_lni_at_device_index = 0 -> null
~ id = "subnet-07846067fffb97419" -> (known after apply)
+ ipv6_cidr_block_association_id = (known after apply)
- map_customer_owned_ip_on_launch = false -> null
~ owner_id = "891376941699" -> (known after apply)
~ private_dns_hostname_type_on_launch = "ip-name" -> (known after apply)
~ tags = {
"Environment" = "dev"
~ "Name" = "public-subnet" -> "private-subnet"
}
~ tags_all = {
~ "Name" = "public-subnet" -> "private-subnet"
# (1 unchanged element hidden)
}
# (10 unchanged attributes hidden)
}
# aws_subnet.main[3] will be destroyed
# (because index [3] is out of range for count)
- resource "aws_subnet" "main" {
- arn = "arn:aws:ec2:ap-northeast-2:891376941699:subnet/subnet-0223cb176694dda91" -> null
- assign_ipv6_address_on_creation = false -> null
- availability_zone = "ap-northeast-2c" -> null
- availability_zone_id = "apne2-az3" -> null
- cidr_block = "192.168.4.0/24" -> null
- enable_dns64 = false -> null
- enable_lni_at_device_index = 0 -> null
- enable_resource_name_dns_a_record_on_launch = false -> null
- enable_resource_name_dns_aaaa_record_on_launch = false -> null
- id = "subnet-0223cb176694dda91" -> null
- ipv6_native = false -> null
- map_customer_owned_ip_on_launch = false -> null
- map_public_ip_on_launch = false -> null
- owner_id = "891376941699" -> null
- private_dns_hostname_type_on_launch = "ip-name" -> null
- tags = {
- "Environment" = "dev"
- "Name" = "private-subnet"
} -> null
- tags_all = {
- "Environment" = "dev"
- "Name" = "private-subnet"
} -> null
- vpc_id = "vpc-0c564090f849155b1" -> null
# (4 unchanged attributes hidden)
}
์ ๋ด์ฉ์ ๋ณด๋ฉด 192.168.1.0/24 ์๋ธ๋ท์ ์ฃผ์์ฒ๋ฆฌํ์ฌ ์ ๊ฑฐํจ์ผ๋ก์ ํ๋์ฉ ์์ผ๋ก ๋น๊ฒจ์ง ๊ฒ์ ๋ณผ ์ ์๋ค.
์คํ ๊ฒฐ๊ณผ๋ฅผ ํ์ธํด๋ณด๊ฒ ๋ค. 192.168.1.0/24 ์๋ธ๋ท์ด ์ค์ข ๋์๊ณ , EC2 ์ธ์คํด์ค๋ 192.168.3.0/24 ๋์ญ์ผ๋ก ํ ๋น๋ฐ์๋ค.
์๋๋ฆฌ์ค 8. ์ฅ์ ์ํฉ ์ฌํ 2
- ์๊ตฌ์ฌํญ : ์ค๋ฅ ํ์ธ ํ ์ฝ๋ ์์
ํ ๋ผํผ ๋ณ์์์ subnet ์ถ๊ฐ ํ ์๋ฌ ๋ฐ์
terraform.tfvars ํ์ผ ๋ด์ฉ์ ์์ ํ๋ค.
# terraform.tfvars
vpc_cidr = "192.168.0.0/16"
subnets = [
{
cidr = "192.168.1.0/24",
az = "ap-northeast-2a",
tags = {
Name = "public-subnet"
Environment = "dev"
}
},
# ๋ค์ ์๋ธ๋ท ์ถ๊ฐ!!!
{
cidr = "192.168.5.0/24",
az = "ap-northeast-2a",
tags = {
Name = "public-subnet"
Environment = "dev"
}
},
{
cidr = "192.168.2.0/24",
az = "ap-northeast-2a",
tags = {
Name = "private-subnet"
Environment = "dev"
}
},
{
cidr = "192.168.3.0/24",
az = "ap-northeast-2a",
tags = {
Name = "public-subnet"
Environment = "dev"
}
},
{
cidr = "192.168.4.0/24",
az = "ap-northeast-2c",
tags = {
Name = "private-subnet"
Environment = "dev"
}
}
]
์์ ํ์ฌ terraform apply ์งํ ์, ๋ค์๊ณผ ๊ฐ์ด ์๋ฌ๊ฐ ๋ฐ์ํ๋ค. 192.168.4.0/24, 192.168.3.0/24 ์๋ธ๋ท์ด ์ถฉ๋ํ๋ค๋ ๊ฒ์ธ๋ฐ ์ด์ ๊ฐ ๋ฌด์์ผ๊น?
ํ์ฌ ์์ฑ๋ ์๋ธ๋ท
์ผ๋จ ์๋ฒ ์์ฑ ๋ก๊ทธ๋ฅผ ํ์ธํด๋ณด๋ฉด, ๊ธฐ์กด 192.168.3.0/24 ๋์ญ์์ ์๋ก์ด ๋์ญ์ผ๋ก ๋ณ๊ฒฝ๋ ๊ฒ์ ํ์ธ ๊ฐ๋ฅํ๋ค.
# aws_instance.server must be replaced
-/+ resource "aws_instance" "server" {
~ arn = "arn:aws:ec2:ap-northeast-2:891376941699:instance/i-0605531f9d99f6d34" -> (known after apply)
~ associate_public_ip_address = false -> (known after apply)
~ availability_zone = "ap-northeast-2a" -> (known after apply)
~ cpu_core_count = 2 -> (known after apply)
~ cpu_threads_per_core = 1 -> (known after apply)
~ disable_api_stop = false -> (known after apply)
~ disable_api_termination = false -> (known after apply)
~ ebs_optimized = false -> (known after apply)
- hibernation = false -> null
+ host_id = (known after apply)
+ host_resource_group_arn = (known after apply)
+ iam_instance_profile = (known after apply)
~ id = "i-0605531f9d99f6d34" -> (known after apply)
~ instance_initiated_shutdown_behavior = "stop" -> (known after apply)
+ instance_lifecycle = (known after apply)
~ instance_state = "running" -> (known after apply)
~ ipv6_address_count = 0 -> (known after apply)
~ ipv6_addresses = [] -> (known after apply)
+ key_name = (known after apply)
~ monitoring = false -> (known after apply)
+ outpost_arn = (known after apply)
+ password_data = (known after apply)
+ placement_group = (known after apply)
~ placement_partition_number = 0 -> (known after apply)
~ primary_network_interface_id = "eni-0069722efa458f0f9" -> (known after apply)
~ private_dns = "ip-192-168-3-13.ap-northeast-2.compute.internal" -> (known after apply)
~ private_ip = "192.168.3.13" -> (known after apply)
+ public_dns = (known after apply)
+ public_ip = (known after apply)
~ secondary_private_ips = [] -> (known after apply)
~ security_groups = [] -> (known after apply)
+ spot_instance_request_id = (known after apply)
~ subnet_id = "subnet-0c3a63cee06465cf7" # forces replacement -> (known after apply) # forces replacement
tags = {
"Name" = "Terraform demo"
}
~ tenancy = "default" -> (known after apply)
+ user_data = (known after apply)
+ user_data_base64 = (known after apply)
~ vpc_security_group_ids = [
- "sg-0550d5c309aac5d32",
] -> (known after apply)
# (6 unchanged attributes hidden)
๋ค์ subnet ์์ฑ ๋ก๊ทธ๋ฅผ ํ์ธํด๋ณด๊ฒ ๋ค. ์์ฝํ์๋ฉด,
- priveate 192.168.2.0 → public 192.168.1.0
- public 192.168.3.0 → public 192.168.5.0 (๊ธฐ์กด EC2์์ ์ฌ์ฉ๋๋ subnet)
- private 192.168.4.0 → private 192.168.2.0
- public 192.168.3.0 →์์ฑ error
- private 192.168.4.0 → ์์ฑ error
aws_subnet.main[0] must be replaced
-/+ resource "aws_subnet" "main" {
~ arn = "arn:aws:ec2:ap-northeast-2:891376941699:subnet/subnet-0b0c16c4004fb1225" -> (known after apply)
~ availability_zone_id = "apne2-az1" -> (known after apply)
~ cidr_block = "192.168.2.0/24" -> "192.168.1.0/24" # forces replacement
- enable_lni_at_device_index = 0 -> null
~ id = "subnet-0b0c16c4004fb1225" -> (known after apply)
+ ipv6_cidr_block_association_id = (known after apply)
- map_customer_owned_ip_on_launch = false -> null
~ owner_id = "891376941699" -> (known after apply)
~ private_dns_hostname_type_on_launch = "ip-name" -> (known after apply)
~ tags = {
"Environment" = "dev"
~ "Name" = "private-subnet" -> "public-subnet"
}
~ tags_all = {
~ "Name" = "private-subnet" -> "public-subnet"
# (1 unchanged element hidden)
}
# (11 unchanged attributes hidden)
}
# aws_subnet.main[1] must be replaced
-/+ resource "aws_subnet" "main" {
~ arn = "arn:aws:ec2:ap-northeast-2:891376941699:subnet/subnet-0c3a63cee06465cf7" -> (known after apply)
~ availability_zone_id = "apne2-az1" -> (known after apply)
~ cidr_block = "192.168.3.0/24" -> "192.168.5.0/24" # forces replacement
- enable_lni_at_device_index = 0 -> null
~ id = "subnet-0c3a63cee06465cf7" -> (known after apply)
+ ipv6_cidr_block_association_id = (known after apply)
- map_customer_owned_ip_on_launch = false -> null
~ owner_id = "891376941699" -> (known after apply)
~ private_dns_hostname_type_on_launch = "ip-name" -> (known after apply)
tags = {
"Environment" = "dev"
"Name" = "public-subnet"
}
# (12 unchanged attributes hidden)
}
# aws_subnet.main[2] must be replaced
-/+ resource "aws_subnet" "main" {
~ arn = "arn:aws:ec2:ap-northeast-2:891376941699:subnet/subnet-0c458aa6685399eb0" -> (known after apply)
~ availability_zone = "ap-northeast-2c" -> "ap-northeast-2a" # forces replacement
~ availability_zone_id = "apne2-az3" -> (known after apply)
~ cidr_block = "192.168.4.0/24" -> "192.168.2.0/24" # forces replacement
- enable_lni_at_device_index = 0 -> null
~ id = "subnet-0c458aa6685399eb0" -> (known after apply)
+ ipv6_cidr_block_association_id = (known after apply)
- map_customer_owned_ip_on_launch = false -> null
~ owner_id = "891376941699" -> (known after apply)
~ private_dns_hostname_type_on_launch = "ip-name" -> (known after apply)
tags = {
"Environment" = "dev"
"Name" = "private-subnet"
}
# (11 unchanged attributes hidden)
}
# aws_subnet.main[3] will be created
+ resource "aws_subnet" "main" {
+ arn = (known after apply)
+ assign_ipv6_address_on_creation = false
+ availability_zone = "ap-northeast-2a"
+ availability_zone_id = (known after apply)
+ cidr_block = "192.168.3.0/24"
+ enable_dns64 = false
+ enable_resource_name_dns_a_record_on_launch = false
+ enable_resource_name_dns_aaaa_record_on_launch = false
+ id = (known after apply)
+ ipv6_cidr_block_association_id = (known after apply)
+ ipv6_native = false
+ map_public_ip_on_launch = false
+ owner_id = (known after apply)
+ private_dns_hostname_type_on_launch = (known after apply)
+ tags = {
+ "Environment" = "dev"
+ "Name" = "public-subnet"
}
+ tags_all = {
+ "Environment" = "dev"
+ "Name" = "public-subnet"
}
+ vpc_id = "vpc-0ff5212e9d1af6f7c"
}
# aws_subnet.main[4] will be created
+ resource "aws_subnet" "main" {
+ arn = (known after apply)
+ assign_ipv6_address_on_creation = false
+ availability_zone = "ap-northeast-2c"
+ availability_zone_id = (known after apply)
+ cidr_block = "192.168.4.0/24"
+ enable_dns64 = false
+ enable_resource_name_dns_a_record_on_launch = false
+ enable_resource_name_dns_aaaa_record_on_launch = false
+ id = (known after apply)
+ ipv6_cidr_block_association_id = (known after apply)
+ ipv6_native = false
+ map_public_ip_on_launch = false
+ owner_id = (known after apply)
+ private_dns_hostname_type_on_launch = (known after apply)
+ tags = {
+ "Environment" = "dev"
+ "Name" = "private-subnet"
}
+ tags_all = {
+ "Environment" = "dev"
+ "Name" = "private-subnet"
}
+ vpc_id = "vpc-0ff5212e9d1af6f7c"
}
์ด๋ฐ ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ ์ด์ ๋ count ๋ฐ๋ณต๋ฌธ์ ๋ฆฌ์์ค์ ๋ฐฐ์ด๋ก ์ ์ฅ๋๊ธฐ ๋๋ฌธ์ธ๋ฐ, ๋ฆฌ์์ค๋ฅผ ์๋ณํ ๋ ํด๋น ๋ฐฐ์ด์ ์ธ๋ฑ์ค๋ฅผ ์ฌ์ฉํ๊ฒ ๋๋ค.
์ฆ, ์๋ก 192.168.0.5 subnet์ ์์ฑ ์ ๋ค ๋ฐฐ์ด์ ๋ฆฌ์์ค(์ฌ๊ธฐ์ ์๋ธ๋ท)๋ค๋ ์ฌ ์์ฑ์ด ๋๋ฉฐ ํ๋ก๋น์ ๋์ด ๋๋ค. (๊ทธ๋ฅ ์๋ ์๋ธ๋ท๋ง ์ถ๊ฐํ๋๊ฒ ์๋)
์ด๋ for_each ๋ฐ๋ณต๋ฌธ์ผ๋ก ํด๊ฒฐ์ด ๊ฐ๋ฅํ๋ฐ, for_each์ map, set์ ํ์ฉํ๋ค๊ณ ํ๋ค. ์ฆ key,value๋ฅผ ์ฌ์ฉํด์ ํด๋น ํค์ ๊ฐ์ ์ ๊ทผ์ด ๊ฐ๋ฅํ์ฌ ์ญ์ ๋๊ณ ์ถ๊ฐ๋๋ ๋ฆฌ์์ค์ ๋ํด ์ ํํ๊ฒ ์ง์ ํด์ ์์ ํ ์ ์์ ๋ฏ ํ๋ค.
for_each์ ๋ํด์๋ ๋ค์ ํฌ์คํ ์ผ๋ก ์ ๋ฆฌํด๋ณด๋๋ก ํ๊ฒ ๋ค.