I have an existing bucket that was not created via Terraform. I'm currently setting a policy on that bucket for additional access rights. I need to add a cors_rule to the bucket, but everything I'm finding suggests that you need to create the resource in order to add the cors rule. Is there a way to add a cors_rule to an existing bucket data source?
- Adding CORS to a bucket that's being created: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket#using-cors
- Documentation on the bucket data source: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/s3_bucket
data "aws_s3_bucket" "my_bucket" {
bucket = "my-bucket"
# This produces a failure on plan
cors_rule {
allowed_headers = ["*"]
allowed_methods = ["GET", "HEAD"]
allowed_origins = [
"https://example.my-website.com"
]
expose_headers = [
"Access-Control-Allow-Origin",
"ETag"
]
max_age_seconds = 3000
}
}
resource "aws_s3_bucket_policy" "allow_access" {
bucket = data.aws_s3_bucket.my_bucket.id
policy = data.aws_iam_policy_document.allow_access.json
}
data "aws_iam_policy_document" "allow_access" {
statement {
sid = "Access"
principals {
type = "AWS"
identifiers = ["arn:aws:iam::123456789012:user/test"]
}
actions = [
"s3:GetObject",
"s3:GetBucketLocation",
"s3:ListBucket",
]
resources = [
data.aws_s3_bucket.my_bucket.arn,
"${data.aws_s3_bucket.my_bucket.arn}/*"
]
}
}
