๋ฐ์ํ
- CloudNeta์ ๊ฐ์๋ค๋์ด ์งํํ์๋ Terraform 101 Study์ ์ฐธ๊ฐํ๋ฉฐ ์์ฑํ ๋ด์ฉ์ ๋๋ค.
- ์คํฐ๋ ๊ต์ฌ โ ํ ๋ผํผ์ผ๋ก ์์ํ๋ IaC
[3์ฃผ์ฐจ ๋์ ๊ณผ์ ] 1. ์กฐ๊ฑด๋ฌธ์ ํ์ฉํ์ฌ AWS ๋ฆฌ์์ค ๋ฐฐํฌ
# ์
๋ ฅ ๋ณ์๋ก ๋ฐฐํฌ ํ๊ฒฝ(dev, prod)๋ฅผ ๊ตฌ๋ถํ์ฌ EC2 ์ธ์คํด์ค์ ์ฌ์ด์ฆ ์กฐ์ ํ๊ธฐ
variable "env" {}
resource "aws_instance" "app" {
# ๋ฐฐํฌ ํ๊ฒฝ์ ์๋ฏธํ๋ env ๋ณ์๊ฐ prod๊ฐ ์๋๋ฉด EC2 ์ธ์คํด์ค๋ t3.micro๋ก ๋ฐฐํฌํ๋ค.
instance_type = var.env != "prod" ? "t3.micro" : "m5.xlarge"
ami = "ami-0c9c942bd7bf113a2"
tags = {
Name = "app"
env = var.env
}
}
[3์ฃผ์ฐจ ๋์ ๊ณผ์ ] 2. ๋ด์ฅ ํจ์๋ฅผ ํ์ฉํ์ฌ AWS ๋ฆฌ์์ค๋ฅผ ๋ฐฐํฌํ๋ ์์
- cidrhost(๋ด์ฅ ํจ์)์ count๋ฅผ ์ด์ฉํ์ฌ 2๊ฐ์ ENI๋ฅผ ์์ฑํ๊ณ EC2์ attach ์ํค๊ธฐ
- cidrhost : ์ฃผ์ด์ง cidr ๋ธ๋ญ์์ ์ธ๋ฑ์ค์ ํด๋นํ๋ IP ์ฃผ์๋ฅผ ๋ฐํ
- cidrhost("172.20.255.0/24", 10)์ 172.20.255.10๋ฅผ ๋ฐํ
# cidrhost(๋ด์ฅ ํจ์)์ count๋ฅผ ์ด์ฉํ์ฌ 2๊ฐ์ ENI๋ฅผ ์์ฑํ๊ณ EC2์ attach ์ํค๊ธฐ
resource "aws_instance" "app" {
instance_type = "t3.medium"
ami = "ami-0c9c942bd7bf113a2"
subnet_id = "subnet-018bd7027b82d5e8e"
}
resource "aws_network_interface" "app_eni" {
count = 2
subnet_id = "subnet-018bd7027b82d5e8e"
# cidrhost : ์ฃผ์ด์ง cidr ๋ธ๋ญ์์ ์ธ๋ฑ์ค์ ํด๋นํ๋ IP ์ฃผ์๋ฅผ ๋ฐํ
# cidrhost("172.20.255.0/24", 10) -> 172.20.255.10
private_ips = [
cidrhost("172.31.255.0/24", (count.index * 5) + 10),
cidrhost("172.31.255.0/24", (count.index * 5) + 11),
cidrhost("172.31.255.0/24", (count.index * 5) + 12),
cidrhost("172.31.255.0/24", (count.index * 5) + 13),
cidrhost("172.31.255.0/24", (count.index * 5) + 14),
]
attachment {
instance = aws_instance.app.id
device_index = count.index + 1
}
}
[3์ฃผ์ฐจ ๋์ ๊ณผ์ ] 6. provider alias๋ก 2๊ฐ์ ๋ฆฌ์ (seoul, tokyo)์ S3 ๋ฐฐํฌํ๊ธฐ
# provider alias๋ก 2๊ฐ์ ๋ฆฌ์ (seoul, tokyo)์ S3 ๋ฐฐํฌํ๊ธฐ
provider "aws" {
region = "ap-northeast-1"
alias = "tokyo"
}
provider "aws" {
region = "ap-northeast-2"
alias = "seoul"
}
resource "aws_s3_bucket" "hyuckang_tokyo" {
provider = aws.tokyo
bucket = "hyuckang-bucket-tokyo"
}
resource "aws_s3_bucket" "hyuckang_seoul" {
provider = aws.seoul
bucket = "hyuckang-bucket-seoul"
}
๋ฐ์ํ