Terraform AWS Module Github ์ ์ฅ์
ansible์์์ ์ญํ (role)๊ณผ ๋น์ทํ๋ฉฐ, ์์ฃผ ์ฌ์ฉํ๋ ๋ฆฌ์์ค๋ค์ ๋ชจ์๋ ์ปจํ ์ด๋
๋ชจ๋์ ์ข ๋ฅ
- root ๋ชจ๋ : ๊ธฐ๋ณธ ์์ ๋๋ ํ ๋ฆฌ ์ ํ์ผ์ ์ ์๋ ๋ฆฌ์์ค
- child ๋ชจ๋ : ๋ชจ๋์ ์ํด ํธ์ถ๋ ๋ชจ๋
๋ชจ๋ ์ด๊ธฐํ
์ด๊ธฐํํด์ผ ๋ชจ๋์ด ๋ค์ด๋ก๋ ๋๋ค.
terraform init
์์
module "myvpc" {
source =
...์
๋ ฅ ๋ณ์...
}
resource "aws_instance" "web" {
subnet_id = module.myvpc.<์ถ๋ ฅ๊ฐ>
}
์ฌ๊ธฐ์ ์ด์ด์ ์์ฑํ ๊ฒ
๋ชจ๋์ ์ฌ์ฉํ ๋๋ output ๋ณ์ ์ด๋ฆ ์ ํ์ธํด์ผํจโ : [aws vpc module outputs](terraform-aws-modules/vpc/aws | Terraform Registry)
๐main.tf
module "app_vpc" { #vpc ๋ชจ๋
source = "terraform-aws-modules/vpc/aws"
name = "app_vpc"
cidr = "10.0.0.0/16"
azs = [
"ap-northeast-2a",
"ap-northeast-2b",
"ap-northeast-2c",
"ap-northeast-2d"
]
public_subnets = [ #list๋ก ์์ฑํด์ผํจ
"10.0.0.0/24",
"10.0.1.0/24",
"10.0.2.0/24",
"10.0.3.0/24"
]
private_subnets = [ #list๋ก ์์ฑํด์ผํจ
"10.0.10.0/24",
"10.0.11.0/24",
"10.0.12.0/24",
"10.0.13.0/24"
]
}
resource "aws_key_pair" "app_server_key" { #๋ณด์ ๊ทธ๋ฃน
key_name = "app_server-key"
public_key = file("/home/vagrant/.ssh/id_rsa.pub")
}
resource "aws_instance" "app_server" { #EC2 ์ธ์คํด์ค
ami = var.aws_amazon_linux_ami[var.aws_region]
instance_type = "t3.small"
vpc_security_group_ids = [aws_security_group.app_server_sg.id]
key_name = aws_key_pair.app_server_key.key_name
subnet_id = module.app_vpc.public_subnets[0] #์๋ธ๋ท
tags = local.common_tags
}
resource "aws_instance" "app_server2" {
ami = var.aws_amazon_linux_ami[var.aws_region]
instance_type = "t3.small"
vpc_security_group_ids = [aws_security_group.app_server_sg.id]
key_name = aws_key_pair.app_server_key.key_name
subnet_id = module.app_vpc.public_subnets[1] #output value๋ฅผ ์ฐธ์กฐํ๊ฒ
tags = local.common_tags
}
resource "aws_eip" "app_server_eip" {
vpc = true
instance = aws_instance.app_server.id
tags = local.common_tags
}
๐security_group.tf
๋ณด์ ๊ทธ๋ฃน์ vpc์ ์ํ๋ ๊ฒ์ด๋ฏ๋ก, ๋ณด์๊ทธ๋ฃน์ vpc๋ฅผ ์์ ํด์ผํจ
resource "aws_security_group" "app_server_sg" {
name = "Allow SSH & HTTP"
vpc_id = module.app_vpc.vpc_id #output value ์ฐธ์กฐ
ingress { #์ธ๋ฐ์ด๋ ๊ท์น
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress { #์ธ๋ฐ์ด๋ ๊ท์น
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["๋ด IP/32"]
}
egress { #์์๋ฐ์ด๋ ๊ท์น
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
vpc ์์ฑ ํ์ธ
์๋ธ๋ท ์์ฑ ํ์ธ
์ธ์คํด์ค ์์ฑ ํ์ธ