简体   繁体   中英

How to modify CORS of an existing bucket in google cloud using Terraform

I create app engine using Terraform which creates a default bucket. I would like to modify its CORS in the same terraform code.

When I use following code it tries to create a new bucket:

resource "google_storage_bucket" "app_engine_bucket" {
  name     = local.app_engine_default_bucket
  location = "US"
  cors {
    origin          = ["*"]
    method          = ["GET", "PUT", "DELETE"]
    response_header = ["Content-Type"]
    max_age_seconds = 3600
  }
}

Since you've added a comment that you're using Appengine, you'd use the app configuration yaml to specify your CORS settings.

Example:

handlers:
- url: /images
  static_dir: static/images
  http_headers:
    Access-Control-Allow-Origin: https://mygame.uc.r.appspot.com
  # ...

If you wanted to modify buckets not managed by AppEngine you could specify the CORS configuration in the configuration for the bucket as Marcin mentioned in the comments.

example:

resource "google_storage_bucket" "static-site" {
  name          = "image-store.com"
  location      = "EU"
  force_destroy = true

  uniform_bucket_level_access = true

  website {
    main_page_suffix = "index.html"
    not_found_page   = "404.html"
  }
  cors {
    origin          = ["http://image-store.com"]
    method          = ["GET", "HEAD", "PUT", "POST", "DELETE"]
    response_header = ["*"]
    max_age_seconds = 3600
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM