반응형
SMALL
Create Internet Gateway
resource "aws_internet_gateway" "terraform_igw" {
vpc_id = aws_vpc.terraform_vpc.id
tags = {
Name = "${var.company_name}-${var.project_name}-${var.environmet}-igw"
}
}
resource
- aws_internet_gateway AWS 내 IGW를 생성하기 위한 명령어입니다.
vpc_id
- Attatch할 VPC 값을 적어줍니다.
tags
- internet gateway 이름 지정합니다.
Create EIP for NAT
resource "aws_eip" "terraform_ngw_eip" {
vpc = true
tags = {
Name = "${var.company_name}-${var.project_name}-${var.enviroment}-ngw-eip"
}
}
NAT 게이트웨이를 생성하려면 EIP가 필요하다.
resource
- aws_eip AWS 내 Elastic IP를 생성하기 위한 명령어입니다.
vpc = true
- 생성 된 EIP가 VPC의 내부에서 실행된다는 명령어입니다.
tags
- eip 이름입니다.
Create NAT Gateway
resource "aws_nat_gateway" "terraform_ngw" {
allocation_id = aws_eip.terraform_ngw_eip.id
subnet_id = aws_subent.terraform_public_subnet.0.id
tags = {
Name = "${var.company_name}-${var.project_name}-${var.environment}-ngw"
}
}
resource
- aws_nat_gateway AWS 내 NAT G/W를 생성하기 위한 명령어입니다.
allocation_id
- 게이트웨이에 대한 탄력적 IP 주소의 ID입니다.
- 위에서 생성했던 eip를 nat에 할당합니다.
subnet_id
- NAT가 위치 할 서브넷 지정합니다.
- ‘0’은 첫 번째 서브넷 값을 뜻합니다.
tags
- NAT 게이트웨이 이름입니다.
Create Route Table
resource "aws_route_table" "terraform_public_route_table" {
vpc_id = aws_vpc.terraform_vpc.id
route = [{
cidr_block = "0.0.0.0/0"
gateway_id = "aws_internet_gateway.terrform.igw.id"
}]
tags = {
Name = "${var.company_name}-${var.project_name}-${var.environment}-pub-rt"
}
}
resource "aws_route_table" "terraform_private_route_table" {
vpc_id = aws.vpc.terrform_vpc.id
route = [{
cidr_block = "0.0.0.0/0"
gateway_id = "aws_internet_gateway.terrform.ngw.id"
}]
tags = {
Name = "${var.company_name}-${var.project_name}-${var.environment}-pri-rt"
}
}
resource
- aws_route_table AWS 내 RT를 생성하기 위한 명령어입니다.
vpc_id
- 참조할 vpc 지정합니다.
route
- Public은 IGW를 대상으로,
- Private 은 NGW를 대상으로 0.0.0.0/0 오픈해줍니다.
Associate Subnets to Route Tables
resource "aws_route_table_association" "terraform_public_subnet_association" {
count = length(var.aws_public_subnets)
subnet_id = aws_subnet.terraform_public_subnet.*.id[count.index]
route_table_id = aws_route_talbe.terraform_public_route_table.id
}
resource "aws_route_table_association" "terraform_private_subnet_association" {
count = length(var.aws_private_subnets)
subnet_id = aws_subnet.terraform_private_subnet.*.id[count.index]
route_table_id = aws_route_table.terraform_private_route_table.id
}
resource
- aws_route_table_association AWS 내 Route table을 Subnet과 연결 시켜주는 명령어입니다.
subnet_id
- 연결할 서브넷 지정합니다
*
- 모든 Pub/Pri 서브넷에 대한 index 값을 가져옵니다.
route_table_id
- 연결할 라우팅 테이블 지정합니다
Create bastion Security Group
resource "aws_security_group" "terraform_bastion_sg" {
vpc_id = aws_vpc.terraform_vpc.id
name = "${var.company_name}-${var.project_name}-${var.environment}-bastion-sg"
ingress = [{
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"] #관리자의 ip를 작성합니다.
}]
egress = [{
from_port = 0
to_port = 0
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}]
}
resource
- "aws_security_group" AWS 내 Security Group을 생성하는 명령어입니다.
vpc_id
- 참조할 vpc 지정합니다
ingress
- 관리자가 22번 port로 접속이 가능합니다.
egress
- 아웃바운드는 0.0.0.0/0이 기본 값입니다.
from_port / to_port
- Port의 범위를 나타냅니다. (’0’은 모든 트래픽을 뜻합니다.)
Create Security Group
resource "aws_security_group" "terraform" {
vpc_id = aws_vpc.terraform_vpc.id
name = "${var.company_name}-${var.project_name}-${var.environment}-asg-sg"
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
security_groups = [aws_security_group.terraform_lb_sg.id]
}
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
security_groups = [aws_security_group.terraform_bastion_sg.id]
}
egress = [{
from_port = 0
to_port = 0
protocol = "tcp"
cidr_block = ["0.0.0.0/0"]
}]
}
ingress
- Bastion의 22번 포트와 private 인스턴스의 80포트를 인바운드에 추가해줍니다.
egress
- 아웃바운드는 0.0.0.0/0이 기본 값입니다.
Create Load Balancer Security Group
resource "aws_security_group" "terraform_lb_sg" {
vpc_id = "${aws_vpc.terraform_vpc.id}"
nmae = "${var.company_name}-${var.project_name}-${var.environment}-lb-sg"
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
ingress
- LB는 IGW에서 트래픽을 받기 때문에 80포트를 열어줍니다.
egress
- 아웃바운드는 0.0.0.0/0이 기본 값입니다.
Create Key-pair
resource "aws_key_pair" "aws_ec2_key" {
key_name = "pri-ec2-key"
public_key = file("~/.ssh/pri-ec2.pub")
}
resource "aws_key_pair" "aws_bastion_key" {
key_name = "bastion-key"
public_key = file("~/.ssh/bastion.pub")
}
aws_key_pair
- AWS 내 Key를 생성해주는 명령어입니다
- 키를 미리 생성한 후 생성한 키를 AWS로 복사합니다.
- PEM Key 생성 명령어→ .ssh 디렉토리에서 확인 가능
- ssh-keygen -m PEM -f .ssh\[키이름] -q -N “”
- → .ssh 디렉토리에서 확인 가능
key_name
- key pair 이름입니다.
public_key
- 사용할 public key입니다.
반응형
LIST
'Clould > Terraform' 카테고리의 다른 글
Terraform 실습 #3 (0) | 2022.07.28 |
---|---|
Terraform 실습 #1 (0) | 2022.07.28 |
Terraform 설치 (0) | 2022.07.28 |