google_storage_bucket

Creates a new bucket in Google cloud storage service (GCS). Once a bucket has been created, its location can't be changed.

For more information see the official documentation and API.

Note: If the project id is not set on the resource or in the provider block it will be dynamically determined which will require enabling the compute api.

Example Usage - creating a private bucket in standard storage, in the EU region. Bucket configured as static website and CORS configurations

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
  }
}

Example Usage - Life cycle settings for storage bucket objects

resource "google_storage_bucket" "auto-expire" {
  name          = "auto-expiring-bucket"
  location      = "US"
  force_destroy = true

  lifecycle_rule {
    condition {
      age = 3
    }
    action {
      type = "Delete"
    }
  }

  lifecycle_rule {
    condition {
      age = 1
    }
    action {
      type = "AbortIncompleteMultipartUpload"
    }
  }
}

Example Usage - Enabling public access prevention

resource "google_storage_bucket" "auto-expire" {
  name          = "no-public-access-bucket"
  location      = "US"
  force_destroy = true

  public_access_prevention = "enforced"
}

Argument Reference

The following arguments are supported:


The lifecycle_rule block supports:

The action block supports:

The condition block supports the following elements, and requires at least one to be defined. If you specify multiple conditions in a rule, an object has to match all of the conditions for the action to be taken:

The autoclass block supports:

The versioning block supports:

The website block supports the following elements, and requires at least one to be defined:

The cors block supports:

The retention_policy block supports:

The logging block supports:

The encryption block supports:

The custom_placement_config block supports:

The soft_delete_policy block supports:

Attributes Reference

In addition to the arguments listed above, the following computed attributes are exported:

Timeouts

This resource provides the following Timeouts configuration options: configuration options:

Import

Storage buckets can be imported using the name or project/name. If the project is not passed to the import command it will be inferred from the provider block or environment variables. If it cannot be inferred it will be queried from the Compute API (this will fail if the API is not enabled).

In Terraform v1.5.0 and later, use an import block to import Storage buckets using one of the formats above. For example:

import {
  id = "{{project_id}}/{{bucket}}"
  to = google_storage_bucket.default
}

When using the terraform import command, Storage buckets can be imported using one of the formats above. For example:

$ terraform import google_storage_bucket.default {{bucket}}
$ terraform import google_storage_bucket.default {{project_id}}/{{bucket}}