跳转到内容
搜索文档

Terraform (AWS)

最后更新 查看 MarkdownAgent 设置

在开始之前,您必须生成 Access Key。所有示例都将使用 access_key_idaccess_key_secret 变量,分别代表您生成的 Access Key ID(访问密钥 ID)Secret Access Key(秘密访问密钥) 值。


本示例演示如何使用 AWS provider 通过 Terraform 配置 R2。

安装 terraform 后:

  1. 创建 main.tf 文件,或编辑现有的 Terraform 配置
  2. endpoints.s3 中填入您的 Cloudflare 账户 ID 对应的 endpoint URL
  3. access_keysecret_key 中填入相应的 R2 API 凭据
  4. 确保在 provider 配置中设置 skip_region_validation = trueskip_requesting_account_id = trueskip_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 应用更改。

这篇文档对您有帮助吗?