본문 바로가기

퍼블릭 클라우드

[iac] Amazon S3로 Terraform 백엔드 구성하기 - terraform code

728x90

Amazon S3로 Terraform 백엔드 구성하기 - terraform code

s3 버킷 생성

- 버킷 이름 : okahpt16-terraform-s3-bucket

dynamoDB 테이블 생성

- 테이블 이름 : okahpt16_terraform_lock

- 파티션 키 : LockID

Terraform Code

$ tree
.
├── main.tf
└── provider.tf
vim provider.tf
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.27.0"
    }

  }
}

provider "aws" {
  # Configuration options
  profile = "terraform"
  region  = "ap-northeast-2"

}
vim main.tf
### s3 버킷 생성
resource "aws_s3_bucket" "terraform_state" {
  bucket = "okahpt16-terraform-s3-bucket"
}

resource "aws_s3_bucket_acl" "terraform_state" {
  bucket = aws_s3_bucket.terraform_state.id
  acl    = "private"
}

resource "aws_s3_bucket_versioning" "terraform_state_version" {
  bucket = aws_s3_bucket.terraform_state.id
  versioning_configuration {
    status = "Enabled"
  }

}

###  dynamodb 테이블 생성
resource "aws_dynamodb_table" "terraform_lock" {
  name         = "okahpt16_terraform_lock"
  hash_key     = "LockID"
  billing_mode = "PAY_PER_REQUEST"

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

}
$ terraform state list
aws_dynamodb_table.terraform_lock
aws_s3_bucket.terraform_state
aws_s3_bucket_acl.terraform_state
aws_s3_bucket_versioning.terraform_state_version

 

참고URL

- https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_versioning

- https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/dynamodb_table

 

728x90