๋ฐ์ํ
- CloudNeta์ ๊ฐ์๋ค๋์ด ์งํํ์๋ Terraform 101 Study์ ์ฐธ๊ฐํ๋ฉฐ ์์ฑํ ๋ด์ฉ์ ๋๋ค.
- ์คํฐ๋ ๊ต์ฌ โ ํ ๋ผํผ์ผ๋ก ์์ํ๋ IaC
[2์ฃผ์ฐจ ๋์ ๊ณผ์ ] 1. ๋ฐ์ดํฐ ์์ค ์ค์ต
- ๋ฆฌ์ ๋ด์์ ์ฌ์ฉ ๊ฐ๋ฅํ ๊ฐ์ฉ์์ญ ๋ชฉ๋ก ๊ฐ์ ธ์ค๊ธฐ
data "aws_availability_zones" "available" {
state = "available"
}
output "available_az_names" {
value = data.aws_availability_zones.available.names
}
[2์ฃผ์ฐจ ๋์ ๊ณผ์ ] 2. ๋ฆฌ์์ค ์ ํ๊ณผ ์ด๋ฆ์ ์ฐจ์ด
- vpc์ subnet์ ๋ฐฐํฌํ๋ฉด์ ๋ฆฌ์์ค ์ ํ๊ณผ ์ด๋ฆ์ ์ฐจ์ด๋ฅผ ์ดํด
- ๋ฆฌ์์ค ๋ธ๋ญ์ โ<ํ๋ก๋ฐ์ด๋>_<๋ฆฌ์์ค ์ ํ>โ โ<์ด๋ฆ>โ์ผ๋ก ์ ์ธ
- <ํ๋ก๋ฐ์ด๋>_<๋ฆฌ์์ค ์ ํ>.<์ด๋ฆ>.<์์ฑ>์ผ๋ก ์ฐธ์กฐ
provider "aws" {
region = "ap-northeast-2"
}
resource "aws_vpc" "hyuckang_vpc" {
cidr_block = "10.10.0.0/16"
enable_dns_support = true
enable_dns_hostnames = true
tags = {
Name = "t101-hyuckang-vpc"
}
}
resource "aws_subnet" "hyuckang_subnet_1" {
vpc_id = aws_vpc.hyuckang_vpc.id
cidr_block = "10.10.1.0/24"
availability_zone = "ap-northeast-2a"
tags = {
Name = "t101-hyuckang_subnet_1"
}
}
resource "aws_subnet" "hyuckang_subnet_2" {
vpc_id = aws_vpc.hyuckang_vpc.id
cidr_block = "10.10.2.0/24"
availability_zone = "ap-northeast-2c"
tags = {
Name = "t101-hyuckang_subnet-2"
}
}
output "aws_vpc_id" {
value = aws_vpc.hyuckang_vpc.id
}
[2์ฃผ์ฐจ ๋์ ๊ณผ์ ] 3. ์ ๋ ฅ๋ณ์๋ฅผ ํ์ฉํ ๋ฆฌ์์ค ์์ฑ
- ์ ๋ ฅ ๋ณ์๋ฅผ ํ์ฉํ์ฌ EC2์ Name ํ๊ทธ ์ ์ฉ
variable "name_tag" {
default = "hyuckang"
}
resource "aws_instance" "variable_test" {
instance_type = "t3.micro"
ami = "ami-0c9c942bd7bf113a2"
tags = {
Name = var.name_tag
}
}
output "variable_test_instance_arn" {
value = aws_instance.variable_test.arn
}
[2์ฃผ์ฐจ ๋์ ๊ณผ์ ] 4. local์ ํ์ฉํ ๋ฆฌ์์ค ์์ฑ
- ํ๋ก๋ฐ์ด๋์ ๊ตฌ์ฑ๋ current ๋ฆฌ์ ์ local์์ ์ฐธ์กฐํ์ฌ region ํ๊ทธ ์ ์ฉ
# provider์ ๊ตฌ์ฑ๋ region์ ํ
locals {
regions = {
"ap-northeast-1" = "tokyo"
"ap-northeast-2" = "seoul"
"ap-northeast-3" = "osaka"
}
}
data "aws_region" "current" {}
resource "aws_instance" "locals_test" {
instance_type = "t3.micro"
ami = "ami-0c9c942bd7bf113a2"
tags = {
"Name" = "locals_test"
"region" = local.regions[data.aws_region.current.name]
}
}
[2์ฃผ์ฐจ ๋์ ๊ณผ์ ] 5. ๋ฐ๋ณต๋ฌธ์ ํ์ฉํ ๋ฆฌ์์ค ์์ฑ
- dynamic์ ํ์ฉํ์ฌ sg ๋ด์์ sg rule ๋ฐ๋ณต ์์ฑ
variable "ingress_rules" {
default = [
{ protocol = "tcp", port = 80, cidr_blocks = ["0.0.0.0/0"], description = "http" },
{ protocol = "tcp", port = 443, cidr_blocks = ["0.0.0.0/0"], description = "https" },
{ protocol = "tcp", port = 22, cidr_blocks = ["0.0.0.0/0"], description = "ssh" }
]
}
resource "aws_security_group" "dynamic_test" {
name = "dynamic_test"
description = "A security group for terraform dynamic test"
dynamic "ingress" {
for_each = var.ingress_rules
content {
protocol = ingress.value.protocol
from_port = ingress.value.port
to_port = ingress.value.port
cidr_blocks = ingress.value.cidr_blocks
description = ingress.value.description
}
}
}
๋ฐ์ํ