스스로 구축하는 AWS 클라우드 인프라 - 기본편을 수강하며 AWS 인프라를 Terraform으로 작성한 내용입니다.
provider.tf
파일에 tls
와 local
추가# TLS
provider "tls" {}
# Local
provider "local" {}
terraform init -upgrade
명령어를 실행key_pair.tf
파일을 아래와 같이 작성# Create a PEM formatted private key
resource "tls_private_key" "ec2_private_key" {
algorithm = "RSA"
rsa_bits = 4096
}
# EC2 Key Pair
resource "aws_key_pair" "ec2_key_pair" {
key_name = "ec2_key_pair"
public_key = tls_private_key.ec2_private_key.public_key_openssh
}
# Generate a local file with private key
resource "local_file" "ec2_key" {
filename = "ec2_key_pair.pem"
content = tls_private_key.ec2_private_key.private_key_pem
}
terraform plan
을 하고 terraform apply
명령을 실행하면 AWS에 새로운 Key Pair가 등록되고 ec2_key_pair.pem
파일이 생성됨ec2_key_pair.pem
은 .gitignore
에 추가security_group.tf
파일을 아래와 같이 작성# Security group for public ec2
resource "aws_security_group" "public_ec2_sg" {
name = "public-ec2-sg"
description = "Security group for public ec2 instance"
vpc_id = aws_vpc.main.id
tags = {
Name = "public-ec2-sg"
}
}
# Inbound rule allowing SSH
resource "aws_vpc_security_group_ingress_rule" "allow_ssh" {
security_group_id = aws_security_group.public_ec2_sg.id
cidr_ipv4 = "0.0.0.0/0"
from_port = 22
ip_protocol = "tcp"
to_port = 22
}
# Inbound rule allowing HTTP
resource "aws_vpc_security_group_ingress_rule" "allow_http" {
security_group_id = aws_security_group.public_ec2_sg.id
cidr_ipv4 = "0.0.0.0/0"
from_port = 80
ip_protocol = "tcp"
to_port = 80
}
# Inbound rule allowing HTTPS
resource "aws_vpc_security_group_ingress_rule" "allow_https" {
security_group_id = aws_security_group.public_ec2_sg.id
cidr_ipv4 = "0.0.0.0/0"
from_port = 443
ip_protocol = "tcp"
to_port = 443
}
# Outbound rule allowing all traffic
resource "aws_vpc_security_group_egress_rule" "allow_all_outbound_traffic" {
security_group_id = aws_security_group.public_ec2_sg.id
cidr_ipv4 = "0.0.0.0/0"
ip_protocol = "-1"
}
terraform plan
을 하고 terraform apply
명령을 실행ec2.tf
파일을 아래와 같이 작성# Public-ec2-0
resource "aws_instance" "public" {
ami = "ami-0dc44556af6f78a7b" # Ubuntu Server 24.04 LTS (HVM), x86
instance_type = "t2.micro"
key_name = aws_key_pair.ec2_key_pair.key_name
vpc_security_group_ids = [ aws_security_group.public_ec2_sg.id ]
subnet_id = aws_subnet.public[0].id
tags = {
Name = "public-ec2-0-${var.vpc_name}"
}
metadata_options {
http_endpoint = "enabled"
http_put_response_hop_limit = 1
http_tokens = "optional"
instance_metadata_tags = "enabled"
}
user_data = <<-EOF
#!/bin/bash
apt-get update -y
apt-get upgrade -y
apt-get install -y apache2 mariadb-server php libapache2-mod-php php-mysql
systemctl start apache2
systemctl enable apache2
systemctl start mysql
systemctl enable mysql
usermod -a -G www-data ubuntu
chown -R ubuntu:www-data /var/www/html
chmod 2775 /var/www/html
find /var/www/html -type d -exec chmod 2775 {} \;
find /var/www/html -type f -exec chmod 0664 {} \;
echo "<?php phpinfo(); ?>" > /var/www/html/index.php
EOF
}
Ubuntu 24.04 x86
으로, 인스턴스 타입을 t2.micro
로 설정key_pair.tf
로 만든 것을 사용user_data
에 작성terraform plan
을 하고 terraform apply
명령을 실행하면 EC2 인스턴스가 생성됨eip.tf
파일을 아래와 같이 작성# Elastic IP for public-ec2-0
resource "aws_eip" "public" {
instance = aws_instance.public.id
domain = "vpc"
tags = {
Name = "eip-public-ec2"
}
}
terraform plan
을 하고 terraform apply
명령을 실행하면 Elastic IP가 생성되며 EC2 인스턴스에 할당됨