Public EC2 인스턴스 생성 및 LAMP 웹서버 구성

Chori·2024년 12월 29일
0
post-thumbnail

스스로 구축하는 AWS 클라우드 인프라 - 기본편을 수강하며 AWS 인프라를 Terraform으로 작성한 내용입니다.


  • LAMP란 Linux 기반 위에 Apache 웹 서버, MySQL 데이터베이스, PHP 애플리케이션을 구성한 스택

Key Pair 생성

  • EC2 인스턴스에 연결할 때 사용할 Key Pair 만들기

프로바이더 업데이트

  • provider.tf 파일에 tlslocal 추가
# TLS
provider "tls" {}

# Local
provider "local" {}
  • terraform init -upgrade 명령어를 실행

Key Pair 리소스 만들기

  • 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 파일이 생성됨
  • Git으로 Terraform 코드의 버전을 관리하고 있었다면 ec2_key_pair.pem.gitignore에 추가

Security Group 생성

  • EC2 인스턴스에 적용할 Security Group 만들기
  • 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"
}
  • 모든 IP 주소에 대해 SSH, HTTP, HTTPS 트래픽을 허용
  • terraform plan을 하고 terraform apply 명령을 실행

EC2 생성

  • EC2 인스턴스 만들기
  • 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
}
  • AMI는 Amazon Machine Image로 서버를 시작하는데 필요한 운영체제나 관련 애플리케이션, 소프트웨어 패키지 등이 포함된 템플릿
  • AMI를 Ubuntu 24.04 x86으로, 인스턴스 타입을 t2.micro로 설정
  • Key Pair는 key_pair.tf로 만든 것을 사용
  • EC2 인스턴스에 적용할 보안 그룹과 서브넷 지정
  • EC2 인스턴스가 부팅될 때 실행되는 스크립트를 user_data에 작성
    • 위 코드에서는 LAMP 스택을 구성하는 스크립트 사용
  • terraform plan을 하고 terraform apply 명령을 실행하면 EC2 인스턴스가 생성됨

Elastic IP

  • EC2 인스턴스에 할당할 Elastic IP 만들기
  • 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 인스턴스에 할당됨

참고자료

profile
전부인 것처럼, 전부가 아닌 것처럼

0개의 댓글