๐Ÿ“‚ IaC/Terraform

Terraform 101 Study - 1์ฃผ์ฐจ(2) ๋„์ „ ๊ณผ์ œ

dhyuck 2023. 7. 9. 02:39
๋ฐ˜์‘ํ˜•
  • CloudNeta์˜ ๊ฐ€์‹œ๋‹ค๋‹˜์ด ์ง„ํ–‰ํ•˜์‹œ๋Š” Terraform 101 Study์— ์ฐธ๊ฐ€ํ•˜๋ฉฐ ์ž‘์„ฑํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค.
  • ์Šคํ„ฐ๋”” ๊ต์žฌ โ†’ ํ…Œ๋ผํผ์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š” IaC

[1์ฃผ์ฐจ ๋„์ „๊ณผ์ œ] 1. EC2 ์›น์„œ๋ฒ„ ๋ฐฐํฌ


  • Ubuntu ์— apache(httpd) ๋ฅผ ์„ค์น˜ํ•˜๊ณ  index.html ์ƒ์„ฑ(๋‹‰๋„ค์ž„ ์ถœ๋ ฅ)ํ•˜๋Š” userdata ๋ฅผ ์ž‘์„ฑํ•ด์„œ ์„ค์ • ๋ฐฐํฌ ํ›„ ์›น ์ ‘์†

      provider "aws" {
        region = "ap-northeast-2"
      }
    
      resource "aws_instance" "apache" {
        ami                    = "ami-0c9c942bd7bf113a2"
        instance_type          = "t2.micro"
        vpc_security_group_ids = [aws_security_group.apache.id]
    
        user_data = <<-EOF
                    #!/bin/bash
                    sudo apt install -y apache2
                    sudo echo "Hello, T101 Study I am hyuckang" > /var/www/html/index.html
                    sudo systemctl start apache2.service
                    EOF
    
        tags = {
          Name = "apache"
        }
      }
    
      resource "aws_security_group" "apache" {
        name = "apache_sg"
    
        ingress {
          from_port   = 80
          to_port     = 80
          protocol    = "tcp"
          cidr_blocks = ["0.0.0.0/0"]
        }
    
        egress {
          from_port   = 0
          to_port     = 65535
          protocol    = "tcp"
          cidr_blocks = ["0.0.0.0/0"]
        }
    
        tags = {
          Name = "apache_sg"
        }
      }
    
      output "apache_public_dns" {
        value = aws_instance.apache.public_dns
      }

[1์ฃผ์ฐจ ๋„์ „๊ณผ์ œ] 2. AWS S3/DynamoDB ๋ฐฑ์—”๋“œ


  • AWS S3/DynamoDB ๋ฐฑ์—”๋“œ ์„ค์ • ์‹ค์Šต

      # terraform state๋ฅผ ์ €์žฅํ•˜๋Š” S3์™€ lock์„ ๊ด€๋ฆฌํ•˜๋Š” dynamoDB ์ƒ์„ฑํ•˜๋Š” CLI
      export BUCKET_NAME="hyuckang-tf101"
      export TABLE_NAME="hyuckang-tf101"
      export REGION="ap-northeast-2"
    
      # state๋ฅผ ์ €์žฅํ•  S3 ๋ฒ„ํ‚ท ์ƒ์„ฑ
      aws s3api create-bucket \
      --bucket $BUCKET_NAME --region $REGION --create-bucket-configuration LocationConstraint=$REGION
    
      # ๋กค๋ฐฑ์ด ๊ฐ€๋Šฅํ•˜๋„๋ก state์˜ ๋ฒ„์ „์„ ๊ด€๋ฆฌํ•˜๋Š” ๋ฒ„์ €๋‹์„ enable
      aws s3api put-bucket-versioning \
      --bucket $BUCKET_NAME --versioning-configuration Status=Enabled
    
      # Lock์„ ๊ด€๋ฆฌํ•˜๋Š” dynamoDB ํ…Œ์ด๋ธ” ์ƒ์„ฑ
      aws dynamodb create-table \
          --table-name $TABLE_NAME \
          --attribute-definitions \
              AttributeName=LockID,AttributeType=S \
          --key-schema \
              AttributeName=LockID,KeyType=HASH \
          --provisioned-throughput \
              ReadCapacityUnits=1,WriteCapacityUnits=1
      # backend๋กœ S3์™€ dynamoDB๋ฅผ ์ง€์ •
      provider "aws" {
        region = "ap-northeast-2"
      }
    
      terraform {
        backend "s3" {
          bucket = "hyuckang-tf101"
          key    = "hyuckang-tf101/terraform.tfstate"
          region = "ap-northeast-2"
    
          dynamodb_table = "hyuckang-tf101"
        }
      }

[1์ฃผ์ฐจ ๋„์ „๊ณผ์ œ] 3. lifecycle์˜ precondition ์‹ค์Šต


  • lifecycle์˜ precondition ์‹ค์Šต ๋‚ด์šฉ์—์„œ step0.txt ~ step6.txt ์ด 7๊ฐœ์˜ ํŒŒ์ผ ์ด๋ฆ„ ์ค‘ ํ•˜๋‚˜๊ฐ€ ์ผ์น˜ ์‹œ ๊ฒ€์ฆ ์กฐ๊ฑด ๋งŒ์กฑ์œผ๋กœ ์ฝ”๋“œ ์ž‘์„ฑ

      variable "file_name" {
        default = "step5.txt"
      }
    
      resource "local_file" "step6" {
        content  = "lifecycle - step 6"
        filename = "${path.module}/${var.file_name}"
    
        lifecycle {
          precondition {
            condition     = contains(["step0.txt", "step1.txt", "step2.txt", "step3.txt", "step4.txt", "step5.txt", "step6.txt"], var.file_name)
            error_message = "file name is not 'step0~6.txt'"
          }
        }
      }

[1์ฃผ์ฐจ ๋„์ „๊ณผ์ œ] 4. AWS ์„œ๋น„์Šค ๋ฆฌ์†Œ์Šค ๋ฐฐํฌ + ๋ฆฌ์†Œ์Šค ์ƒ์„ฑ ๊ทธ๋ž˜ํ”„ ํ™•์ธ


  • Hashicorp AWS Provider Document ์— Example Usage ์ค‘ ์•„๋ฌด๊ฑฐ๋‚˜ 1๊ฐœ์˜ AWS ์„œ๋น„์Šค ๋ฆฌ์†Œ์Šค ๋ฐฐํฌ ์‹ค์Šต

      provider "aws" {
        region = "ap-northeast-2"
      }
    
      resource "aws_vpc" "hyuckang" {
        cidr_block = "10.10.0.0/16"
      }
    
      resource "aws_subnet" "hyuckang" {
        vpc_id     = aws_vpc.hyuckang.id
        cidr_block = "10.10.10.0/24"
      }
    
      resource "aws_route_table" "hyuckang" {
        vpc_id = aws_vpc.hyuckang.id
      }
    
      resource "aws_route_table_association" "hyuckang" {
        subnet_id      = aws_subnet.hyuckang.id
        route_table_id = aws_route_table.hyuckang.id
      }

๋ฐ˜์‘ํ˜•