Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resources for managing R2 #1664

Closed
andyli opened this issue Jun 1, 2022 · 14 comments · Fixed by #2378
Closed

Resources for managing R2 #1664

andyli opened this issue Jun 1, 2022 · 14 comments · Fixed by #2378
Labels
kind/enhancement Categorizes issue or PR as related to improving an existing feature. service/r2 Categorizes issue or PR as related to the R2 service. triage/accepted Indicates an issue or PR is ready to be actively worked on.
Milestone

Comments

@andyli
Copy link

andyli commented Jun 1, 2022

Current Terraform and Cloudflare provider version

N/A

Description

Would be nice to have resources for managing R2 buckets.

Use cases

To manage everything in Terraform.

Potential Terraform configuration

Something similar to the aws_s3_bucket and the related resources.

References

No response

@andyli andyli added kind/enhancement Categorizes issue or PR as related to improving an existing feature. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Jun 1, 2022
@jacobbednarz jacobbednarz added workflow/pending-upstream-library Indicates an issue or PR requires changes from an upstream library. and removed needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Jun 2, 2022
@pkoch
Copy link

pkoch commented Jun 12, 2022

@jacobbednarz can you elaborate what upstream movement we're jammed on? I can programmatically manipulate buckets with wrangler r2 bucket OPERATION.

@jacobbednarz
Copy link
Member

the upstream library that Terraform uses to make the API calls - cloudflare-go.

@ghuntley
Copy link

Disappointed to see there's no support for buckets w/terraform.

@yonran
Copy link
Contributor

yonran commented Aug 4, 2022

If you just want to create buckets, you can use the terraform-provider-aws with the S3 Compatibility API and an S3 Auth token, and using a custom service endpoint and skipping sanity checks within the aws provider. Example of creating an R2 bucket:

variable "account_id" {
  type = string
  sensitive = true
}
variable "cloudflare_r2_access_key_id" {
  type = string
}
variable "cloudflare_r2_secret_access_key" {
  type = string
  sensitive = true
}
provider "aws" {
  access_key = var.cloudflare_r2_access_key_id
  secret_key = var.cloudflare_r2_secret_access_key
  # https://developers.cloudflare.com/r2/platform/s3-compatibility/api/#bucket-region
  region = "auto"
  # fix error validating provider credentials: error calling sts:GetCallerIdentity
  # … lookup sts.auto.amazonaws.com on …: no such host
  skip_credentials_validation = true
  # fix Error: Invalid AWS Region: auto
  skip_region_validation = true
  # fix error retrieving account details: AWS account ID not previously found
  # and failed retrieving via all available methods.
  # caused by iam:ListRoles to iam.amazonaws.com
  # and sts:GetCallerIdentity to sts.auto.amazonaws.com
  skip_requesting_account_id = true
  # skip loading instance profile credentials from 169.254.169.254
  skip_metadata_api_check = true
  # skip ec2/DescribeAccountAttributes to ec2.auto.amazonaws.com
  skip_get_ec2_platforms = true
  endpoints {
    # https://developers.cloudflare.com/r2/platform/s3-compatibility/api/
    s3 = "https://${var.account_id}.r2.cloudflarestorage.com"
  }
  # optional: use an alias so you can also use the real aws provider
  alias = "cloudflare_r2"
}

resource "aws_s3_bucket" "terraform" {
  provider = aws.cloudflare_r2
  bucket = "my-bucket"
}

However, it seems that it is still not possible to bind a bucket to a cloudflare_worker_script from terraform, which is pretty important.

Edit: it seems that it is not possible to create a cloudflare object using terraform-provider-aws 4.x aws_s3_object (aka aws_s3_bucket_object), since that resource tries to read the tags using GetObjectTagging (GET /key?tagging=), but R2 does not recognize ?tagging and just returns the object itself.

yonran added a commit to yonran/terraform-provider-r2-s3-compatibility that referenced this issue Aug 8, 2022
I am using this provider solely to create Cloudflare buckets and objects, since terraform-provider-cloudflare does not have support yet (cloudflare/terraform-provider-cloudflare#1664). But since the CloudFlare provider does not support the ?tagging api, this commit removes that functionality from aws_s3_object.
@alex8bitw
Copy link

alex8bitw commented Sep 21, 2022

the upstream library that Terraform uses to make the API calls - cloudflare-go.

@jacobbednarz @andyli
Looks like support was already added cloudflare/cloudflare-go#1028

https://pkg.go.dev/github.com/cloudflare/cloudflare-go#API.CreateR2Bucket

Example here:
https://github.com/cloudflare/terraform-provider-cloudflare/blob/3d0674f8c32e3a1b060233ef85bd5c0d3774ebf7/internal/provider/resource_cloudflare_worker_script_test.go
Looks like it's already being used? Just not exposed yet.

@jacobbednarz jacobbednarz added triage/accepted Indicates an issue or PR is ready to be actively worked on. service/r2 Categorizes issue or PR as related to the R2 service. and removed workflow/pending-upstream-library Indicates an issue or PR requires changes from an upstream library. labels Sep 21, 2022
@Elycin
Copy link

Elycin commented Oct 9, 2022

Looking forward to this. +1

@fredsig
Copy link

fredsig commented Oct 9, 2022

Looking forwad as well now that R2 is GA.

@Cyb3r-Jak3
Copy link
Contributor

FYI, this still isn't possible as there is no publicly documented API end to get a bucket which terraform needs to check to see if a resource exists.

@jpalomaki
Copy link

When this does get implemented, having CORS support baked-in would be great: https://kian.org.uk/configuring-cors-on-cloudflare-r2/

@knpwrs
Copy link

knpwrs commented Jan 21, 2023

@jpalomaki that may be possible using the AWS provider to configure the R2 bucket (I haven't tried yet but I plan on it): https://developers.cloudflare.com/r2/examples/terraform/

@laurencegill
Copy link

I have found the following aws resources seem to work fine:

aws_s3_bucket
aws_s3_bucket_acl
aws_s3_bucket_lifecycle_configuration
aws_s3_bucket_public_access_block
aws_s3_bucket_server_side_encryption_configuration
aws_s3_bucket_versioning

aws_s3_bucket_public_access_block doesn't work, and nor can you bind a CF domain using TF, which is quite annoying.

@knpwrs
Copy link

knpwrs commented Mar 8, 2023

@jpalomaki I recently got configuring CORS with terraform working:

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "4.57.1"
    }
  }
}

provider "aws" {
  alias                       = "cloudflare"
  region                      = "us-east-1"
  access_key                  = var.cloudflare_r2_access_key
  secret_key                  = var.cloudflare_r2_secret_key
  skip_credentials_validation = true
  skip_region_validation      = true
  skip_requesting_account_id  = true

  endpoints {
    s3 = var.cloudflare_r2_endpoint
  }
}

resource "aws_s3_bucket" "public_bucket" {
  provider = aws.cloudflare
  bucket   = var.cloudflare_r2_public_bucket
}

resource "aws_s3_bucket_cors_configuration" "public_bucket_cors" {
  provider = aws.cloudflare
  bucket   = aws_s3_bucket.public_bucket.id

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

@heyhippari
Copy link

Would be great to be able to configure Domain Access as well, since it currently doesn't seem to be possible to set some required metadata with cloudflare_record, like which R2 bucket the DNS record points to (Even though importing R2 records works just fine).

@Cyb3r-Jak3 Cyb3r-Jak3 mentioned this issue Apr 15, 2023
@github-actions github-actions bot added this to the v4.7.0 milestone May 24, 2023
@github-actions
Copy link
Contributor

This functionality has been released in v4.7.0 of the Terraform Cloudflare Provider.

Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading.

For further feature requests or bug reports with this functionality, please create a new GitHub issue following the template. Thank you!

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 31, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
kind/enhancement Categorizes issue or PR as related to improving an existing feature. service/r2 Categorizes issue or PR as related to the R2 service. triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
None yet
Development

Successfully merging a pull request may close this issue.