IaC (Infrastructure as Code, 코드로써의 인프라)
.tfstate 파일을 생성한다. 여기에는 가장 마지막에 적용한 테라폼 내역이 저장된다..terraform 파일이 생성된다.# provider
provider "aws" {
region = "ap-northeast-2"
}
# VPC ##############################
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/24"
tags = {
Name = "my-vpc"
}
}
# subnet (Public / Private Subnet) ##############################
resource "aws_subnet" "first_subnet" {
vpc_id = aws_vpc.main.id
cidr_block = "10.1.0.0/16"
availability_zone = "ap-northeast-2a"
tags = {
Name = "my-public-subnet"
}
}
resource "aws_subnet" "second_subnet" {
vpc_id = aws_vpc.main.id
cidr_block = "10.2.0.0/16"
availability_zone = "ap-northeast-2b"
tags = {
Name = "my-private-subnet"
}
}
# Internet Gateway ##############################
resource "aws_internet_gateway" "igw" {
vpc_id = aws_vpc.main.id
tags = {
Name = "my-igw"
}
}
# Route Table (Route Table 생성은 aws_route_table, 서브넷과 연결은 aws_route_table_association)
# 퍼블릭 서브넷인 first_subnet과 IGW와 연결된 라우팅 테이블을 연결 ##############################
resource "aws_route_table" "route_table_public" {
vpc_id = aws_vpc.main.id
tags = {
Name = "public-subnet-rtb"
}
}
resource "aws_route_table_association" "route_table_association_1" {
subnet_id = aws_subnet.first_subnet.id
route_table_id = aws_route_table.route_table_public.id
}
# Priavet Subent
# Elastic IP, Nat Gateway (+ 라우팅 테이블 연결) ##############################
resource "aws_eip" "my-ip" {
vpc = true
lifecycle {
create_before_destroy = true
}
}
resource "aws_nat_gateway" "nat_gateway_1" {
allocation_id = aws_eip.my-ip.id
# NAT Gateway는 퍼블릭 서브넷에 위치
subnet_id = aws_subnet.first_subnet.id
tags = {
Name = "my-nat"
}
}
resource "aws_route_table" "route_table_private" {
vpc_id = aws_vpc.main.id
tags = {
Name = "private-subent-rtb"
}
}
resource "aws_route_table_association" "route_table_association_private_2" {
subnet_id = aws_subnet.second_private_subnet.id
route_table_id = aws_route_table.route_table_private.id
}
resource "aws_route" "private_nat_1" {
route_table_id = aws_route_table.route_table_private.id
destination_cidr_block = "0.0.0.0/0"
nat_gateway_id = aws_nat_gateway.nat_gateway_1.id
}
# S3 Bucket 생성 ##############################
resource "aws_s3_bucket" "main" {
bucket = "my-s3-bucket-123"
tags = {
Name = "my-bucket"
}
}
# IAM User 생성 ##############################
resource "aws_iam_user" "gildong_hong" {
name = "gildong.hong"
}
# IAM group 생성
resource "aws_iam_group" "devops_group" {
name = "devops"
}
# 생성한 IAM User를 IAM group에 등록
resource "aws_iam_group_membership" "devops" {
name = aws_iam_group.devops_group.name
users = [
aws_iam_user.gildong_hong.name
]
group = aws_iam_group.devops_group.name
}