在开始之前,您必须生成 Access Key。所有示例都将使用 access_key_id 和 access_key_secret 变量,分别代表您生成的 Access Key ID(访问密钥 ID) 和 Secret Access Key(秘密访问密钥) 值。
本示例演示如何使用 AWS provider ↗ 通过 Terraform 配置 R2。
安装 terraform ↗ 后:
- 创建
main.tf文件,或编辑现有的 Terraform 配置 - 在
endpoints.s3中填入您的 Cloudflare 账户 ID 对应的 endpoint URL - 在
access_key和secret_key中填入相应的 R2 API 凭据。 - 确保在 provider 配置中设置
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 应用更改。