하나의 VPC 하나의 NAT
하나의 VPC 하나의 보안그룹
하나의 VPC 하나의 IGW
IGW/NAT는 퍼블릭 서브넷 배치
# vi /etc/httpd/conf/httpd.conf
/Listen 0.0.0.0:80 - 입력
# netstat -tulnp | grep :80
tcp6 0 0 :::80 :::* LISTEN 1234/httpd
**변경 필요**
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1234/httpd
# httpd.conf 수정: IPv6 대신 IPv4 리슨
sed -i 's/^Listen .*/Listen 0.0.0.0:80/' /etc/httpd/conf/httpd.conf
✅ 해결 방법 3가지 중 선택
✅ 방법 1: 이미 있는 키를 Terraform에서 무시하도록 처리
이미 AWS에 존재하는 키를 재사용하고 싶다면 terraform import 명령어로 기존 키페어를 Terraform 상태에 가져옵니다:
erraform import aws_key_pair.tf_keypair tf_keypair
이렇게 하면 이미 존재하는 키를 Terraform이 관리하게 됩니다.
✅ 방법 2: 키페어 이름 변경
코드 내 키 이름을 변경해서 충돌을 피합니다:
resource "aws_key_pair" "tf_keypair" {
key_name = "tf_keypair_2" # 이름 변경
public_key = file("C:/ssh/tf_keypair.pub")
tags = {
Description = "TF-KeyPair"
}
}
✅ 방법 3: AWS에서 기존 키 삭제
키페어를 새로 만들고 싶다면 AWS 콘솔 또는 CLI에서 기존 키페어를 삭제:
- AWS 콘솔 > EC2 > 키페어 > tf_keypair 삭제
- 또는 CLI:
- aws ec2 delete-key-pair --key-name tf_keypair
삭제 후 다시 terraform apply 실행
provider "aws" {
region = "ap-northeast-2"
}
# 키페어
resource "aws_key_pair" "example" {
key_name = "example-key"
public_key = file("~/.ssh/id_rsa.pub")
}
# VPC & 서브넷
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
}
resource "aws_subnet" "public" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.1.0/24"
map_public_ip_on_launch = true
availability_zone = "ap-northeast-2a"
}
# 인터넷 게이트웨이
resource "aws_internet_gateway" "gw" {
vpc_id = aws_vpc.main.id
}
resource "aws_route_table" "public_rt" {
vpc_id = aws_vpc.main.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.gw.id
}
}
resource "aws_route_table_association" "a" {
subnet_id = aws_subnet.public.id
route_table_id = aws_route_table.public_rt.id
}
# 보안 그룹 (HTTP 허용)
resource "aws_security_group" "web_sg" {
vpc_id = aws_vpc.main.id
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
# EC2 인스턴스 2개 (웹 서버)
resource "aws_instance" "web1" {
ami = data.aws_ami.amzn2.id
instance_type = "t3.micro"
subnet_id = aws_subnet.public.id
key_name = aws_key_pair.example.key_name
vpc_security_group_ids = [aws_security_group.web_sg.id]
user_data = <<-EOF
#!/bin/bash
yum update -y
yum install -y httpd
echo "<h1>Web Server 1</h1>" > /var/www/html/index.html
systemctl enable httpd
systemctl start httpd
EOF
}
resource "aws_instance" "web2" {
ami = data.aws_ami.amzn2.id
instance_type = "t3.micro"
subnet_id = aws_subnet.public.id
key_name = aws_key_pair.example.key_name
vpc_security_group_ids = [aws_security_group.web_sg.id]
user_data = <<-EOF
#!/bin/bash
yum update -y
yum install -y httpd
echo "<h1>Web Server 2</h1>" > /var/www/html/index.html
systemctl enable httpd
systemctl start httpd
EOF
}
# 최신 Amazon Linux 2 AMI
data "aws_ami" "amzn2" {
most_recent = true
owners = ["amazon"]
filter {
name = "name"
values = ["amzn2-ami-hvm-*-x86_64-gp2"]
}
}
# NLB
resource "aws_lb" "nlb" {
name = "simple-nlb"
internal = false
load_balancer_type = "network"
subnets = [aws_subnet.public.id]
}
resource "aws_lb_target_group" "tg" {
name = "simple-tg"
port = 80
protocol = "TCP"
target_type = "instance"
vpc_id = aws_vpc.main.id
}
resource "aws_lb_target_group_attachment" "web1_attach" {
target_group_arn = aws_lb_target_group.tg.arn
target_id = aws_instance.web1.id
port = 80
}
resource "aws_lb_target_group_attachment" "web2_attach" {
target_group_arn = aws_lb_target_group.tg.arn
target_id = aws_instance.web2.id
port = 80
}
resource "aws_lb_listener" "listener" {
load_balancer_arn = aws_lb.nlb.arn
port = 80
protocol = "TCP"
default_action {
type = "forward"
target_group_arn = aws_lb_target_group.tg.arn
}
}
output "nlb_dns_name" {
value = aws_lb.nlb.dns_name
}
'IT 엔지니어 > CLOUD' 카테고리의 다른 글
AWS -NLB (0) | 2025.06.14 |
---|---|
AWS - ALB (0) | 2025.06.13 |
AWS - Terraform -3 (0) | 2025.06.11 |
AWS - terraform -2 (0) | 2025.06.10 |
AWS - NLB (0) | 2025.06.10 |