Notice
Recent Posts
Recent Comments
Link
«   2024/09   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
Tags
more
Archives
Today
Total
관리 메뉴

클라우드 엔지니어 꿈나무

Terraform S3 구축 및 Dyanmo DB 생성 본문

카테고리 없음

Terraform S3 구축 및 Dyanmo DB 생성

새싹싹이 2024. 4. 11. 11:20

/backend/main.tf

provider "aws" {
    region = "eu-west-2"
}

resource "aws_s3_bucket" "terraform_state" {
    bucket = "s3-911031"
}

#s3 버저닝 -> 오래된 상태 파일도 저장
resource "aws_s3_bucket_versioning" "enabled" {
    bucket = aws_s3_bucket.terraform_state.id
    versioning_configuration {
        status = "Enabled"
    }
}

#s3 암호화: SSE(서버 측 암호화)
resource "aws_s3_bucket_server_side_encryption_configuration" "default"{
    bucket = aws_s3_bucket.terraform_state.id

    rule{
        apply_server_side_encryption_by_default {
          sse_algorithm = "AES256"
        }
    }
}

# 공개 액세스 차단
resource "aws_s3_bucket_public_access_block" "public_access" {
    bucket = aws_s3_bucket.terraform_state.id
    block_public_acls = true
    block_public_policy = true
    ignore_public_acls = true
    restrict_public_buckets = true
}

#DynamoDB 생성 
resource "aws_dynamodb_table" "terraform_lock" {
    name = "911031-dynamodb"
    billing_mode = "PAY_PER_REQUEST"
    hash_key = "LockID"

    attribute {
      name = "LockID"
      type = "S"
    }
}

 

 

S3 생성 확인

 

 

DynamoDB 생성 확인

 

 

s3dp tfstate 파일을 올리기 위한 테라폼 코드

terraform {
  required_version = ">= 1.0.0, < 2.0.0"

  backend "s3" {
    bucket = "s3-911031"
    key = "vpc/terraform.tfstate"
    region = "eu-west-2"
    encrypt = true
    dynamodb_table = "911031-dynamodb"
  }
}

#Terraform은 상태 파일 락(lock) 관리를 위해 911031-dynamodb라는 DynamoDB 테이블을 사용