본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

Terraform (AWS)

원문 보기 위키 갱신

AWS 제공자를 사용해서 Terraform으로 R2를 구성하는 방법을 배워 보아요. 시작 전에 액세스 키를 생성해야 해요.

출처: 문서

본문

시작하기 전에 액세스 키를 생성해야 해요. 모든 예시는 여러분이 생성한 Access Key ID와 Secret Access Key 값을 나타내는 access_key_id와 access_key_secret 변수를 사용해요.

이 예시는 AWS 제공자 ↗︎를 사용해서 Terraform으로 R2를 구성하는 방법을 보여줘요.

AWS 제공자 사용 시 참고

Cloudflare 제공자만 사용하려면 Terraform 문서를 보세요.

terraform ↗︎이 설치되어 있다면 다음 단계를 따라 해요.

  1. main.tf 파일을 만들거나 기존 Terraform 구성을 편집해요.
  2. endpoints.s3의 엔드포인트 URL을 여러분의 Cloudflare account ID로 채워 넣어요.
  3. access_key와 secret_key를 해당 R2 API 자격 증명으로 채워 넣어요.
  4. 제공자 구성에 skip_region_validation = true, skip_requesting_account_id = true, skip_credentials_validation = true이 설정되어 있는지 확인해요.
terraform {
  required_providers {
    aws = {
      source = "hashicorp/aws"
      version = "~> 5"
    }
  }
}

provider "aws" {
  region = "us-east-1"

  access_key = <R2 Access Key>
  secret_key = <R2 Secret Key>

	# Required for R2.
	# These options disable S3-specific validation on the client (Terraform) side.
  skip_credentials_validation = true
  skip_region_validation      = true
  skip_requesting_account_id  = true

  endpoints {
    s3 = "https://<account id>.r2.cloudflarestorage.com"
  }
}

resource "aws_s3_bucket" "default" {
  bucket = "<org>-test"
}

resource "aws_s3_bucket_cors_configuration" "default" {
  bucket   = aws_s3_bucket.default.id

  cors_rule {
    allowed_methods = ["GET"]
    allowed_origins = ["*"]
  }
}

resource "aws_s3_bucket_lifecycle_configuration" "default" {
  bucket = aws_s3_bucket.default.id

  rule {
    id     = "expire-bucket"
    status = "Enabled"
    expiration {
      days = 1
    }
  }

  rule {
    id     = "abort-multipart-upload"
    status = "Enabled"
    abort_incomplete_multipart_upload {
      days_after_initiation = 1
    }
  }
}

그런 다음 terraform plan으로 변경 사항을 확인하고 terraform apply로 변경 사항을 적용할 수 있어요.

더 알아보기 (Learn more)