1. IaC환경


1. Terraform 설치

sudo su -

wget -O- <https://apt.releases.hashicorp.com/gpg> | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg

echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] <https://apt.releases.hashicorp.com> $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list

apt-get update && apt-get install terraform -y

terraform -version

2. Terraform을 이용한 AWS 인프라 구축

# IAM 역할 생성
resource "aws_iam_role" "s3_create_bucket_role" {
  name = "s3-create-bucket-role"
  
  assume_role_policy = jsonencode({
    "Version": "2012-10-17",
    "Statement": [
      {
        "Action": "sts:AssumeRole",
        "Effect": "Allow",
        "Principal": {
          "Service": "ec2.amazonaws.com"
        }
      }
    ]
  })
}

# IAM 정책 정의 (S3에 대한 모든 권한 부여)
resource "aws_iam_policy" "s3_full_access_policy" {
  name        = "s3-full-access-policy"
  description = "Full access to S3 resources"
  
  policy = jsonencode({
    Version = "2012-10-17"
    Statement = [
      {
        Effect = "Allow"
        Action = [
          "s3:*"  # 모든 S3 액세스 허용
        ]
        Resource = [
          "*"  # 모든 S3 리소스에 대한 권한
        ]
      }
    ]
  })
}

# IAM 역할에 정책 연결
resource "aws_iam_role_policy_attachment" "attach_s3_policy" {
  role       = aws_iam_role.s3_create_bucket_role.name
  policy_arn = aws_iam_policy.s3_full_access_policy.arn
}

# S3 버킷 생성
resource "aws_s3_bucket" "bucket1" {
  bucket = "ce30-bucket1"  # 생성하고자 하는 S3 버킷 이름
}

# S3 버킷의 public access block 설정
resource "aws_s3_bucket_public_access_block" "bucket1_public_access_block" {
  bucket = aws_s3_bucket.bucket1.id

  block_public_acls       = false
  block_public_policy     = false
  ignore_public_acls      = false
  restrict_public_buckets = false
}

# 이미 존재하는 S3 버킷에 index.html 파일을 업로드
resource "aws_s3_object" "index" {
  bucket        = aws_s3_bucket.bucket1.id  # 생성된 S3 버킷 이름 사용
  key           = "index.html"
  source        = "index.html"
  content_type  = "text/html"
}

# S3 버킷의 웹사이트 호스팅 설정
resource "aws_s3_bucket_website_configuration" "xweb_bucket_website" {
  bucket = aws_s3_bucket.bucket1.id  # 생성된 S3 버킷 이름 사용

  index_document {
    suffix = "index.html"
  }
}

# S3 버킷의 public read 정책 설정
resource "aws_s3_bucket_policy" "public_read_access" {
  bucket = aws_s3_bucket.bucket1.id  # 생성된 S3 버킷 이름 사용

  policy = <<EOF
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": "*",
      "Action": [ "s3:GetObject" ],
      "Resource": [
        "arn:aws:s3:::ce30-bucket1",
        "arn:aws:s3:::ce30-bucket1/*"
      ]
    }
  ]
}
EOF
}

output "website_endpoint" {
  value = aws_s3_bucket.bucket1.website_endpoint
  description = "The endpoint for the S3 bucket website."
}

3. Terraform을 이용한 EC2 생성

<aside> 💡

미션

terraform으로 EC2에 ce30-ec2 생성해보기

</aside>

resource "aws_instance" "ce30_ec2" {
  ami           = "ami-01123b84e2a4fba05"  # Amazon Linux 2 AMI (서울 리전용)
  instance_type = "t2.micro"               # 프리티어 인스턴스

  # 태그 설정
  tags = {
    Name = "ce30-ec2"
  }

  # EC2 키 페어 설정 (기존 키를 사용하거나 새 키를 생성)
  key_name = "ce30-key"                      # EC2 액세스에 필요한 키페어 이름

  # 인스턴스 접근을 위한 보안 그룹
  vpc_security_group_ids = [aws_security_group.ec2_security_group.id]

  # 인스턴스 시작 후 초기 스크립트
  user_data = <<-EOF
    #!/bin/bash
    yum update -y
    echo "Hello from ce30-ec2" > /var/www/html/index.html
  EOF
}

# EC2에 접근 허용을 위한 보안 그룹
resource "aws_security_group" "ec2_security_group" {
  name        = "ce30-ec2-security-group"
  description = "Allow SSH and HTTP"

  ingress {
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]  # SSH 접근 허용
  }

  ingress {
    from_port   = 80
    to_port     = 80
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]  # HTTP 접근 허용
  }

  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }
}

resource "aws_instance" "ce30_ec2" {
  ami           = "ami-01123b84e2a4fba05"  # Amazon Linux 2 AMI (서울 리전용)
  instance_type = "t2.micro"               # 프리티어 인스턴스

  # 태그 설정
  tags = {
    Name = "ce30-ec2"
  }

  # EC2 키 페어 설정 (기존 키를 사용하거나 새 키를 생성)
  key_name = "ce30-key"                      # EC2 액세스에 필요한 키페어 이름

  # 인스턴스 접근을 위한 보안 그룹
  vpc_security_group_ids = [aws_security_group.ec2_security_group.id]

  # 인스턴스 시작 후 초기 스크립트
  user_data = <<-EOF
    #!/bin/bash
    yum update -y
    echo "Hello from ce30-ec2" > /var/www/html/index.html
  EOF
}

# EC2에 접근 허용을 위한 보안 그룹
resource "aws_security_group" "ec2_security_group" {
  name        = "ce30-ec2-security-group"
  description = "Allow SSH and HTTP"

  ingress {
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]  # SSH 접근 허용
  }

  ingress {
    from_port   = 80
    to_port     = 80
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]  # HTTP 접근 허용
  }

  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }
}