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.
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
}
}
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"
}
}
}
resource "google_storage_bucket" "auto-expire" {
name = "no-public-access-bucket"
location = "US"
force_destroy = true
public_access_prevention = "enforced"
}
The following arguments are supported:
name
- (Required) The name of the bucket.
location
- (Required) The GCS location.
force_destroy
- (Optional, Default: false) When deleting a bucket, this
boolean option will delete all contained objects. If you try to delete a
bucket that contains objects, Terraform will fail that run.
project
- (Optional) The ID of the project in which the resource belongs. If it
is not provided, the provider project is used.
storage_class
- (Optional, Default: 'STANDARD') The Storage Class of the new bucket. Supported values include: STANDARD
, MULTI_REGIONAL
, REGIONAL
, NEARLINE
, COLDLINE
, ARCHIVE
.
autoclass
- (Optional) The bucket's Autoclass configuration. Structure is documented below.
lifecycle_rule
- (Optional) The bucket's Lifecycle Rules configuration. Multiple blocks of this type are permitted. Structure is documented below.
versioning
- (Optional) The bucket's Versioning configuration. Structure is documented below.
website
- (Optional) Configuration if the bucket acts as a website. Structure is documented below.
cors
- (Optional) The bucket's Cross-Origin Resource Sharing (CORS) configuration. Multiple blocks of this type are permitted. Structure is documented below.
default_event_based_hold
- (Optional) Whether or not to automatically apply an eventBasedHold to new objects added to the bucket.
retention_policy
- (Optional) Configuration of the bucket's data retention policy for how long objects in the bucket should be retained. Structure is documented below.
labels
- (Optional) A map of key/value label pairs to assign to the bucket.
logging
- (Optional) The bucket's Access & Storage Logs configuration. Structure is documented below.
encryption
- (Optional) The bucket's encryption configuration. Structure is documented below.
enable_object_retention
- (Optional, Default: false) Enables object retention on a storage bucket.
requester_pays
- (Optional, Default: false) Enables Requester Pays on a storage bucket.
rpo
- (Optional) The recovery point objective for cross-region replication of the bucket. Applicable only for dual and multi-region buckets. "DEFAULT"
sets default replication. "ASYNC_TURBO"
value enables turbo replication, valid for dual-region buckets only. See Turbo Replication for more information. If rpo is not specified at bucket creation, it defaults to "DEFAULT"
for dual and multi-region buckets. NOTE If used with single-region bucket, It will throw an error.
uniform_bucket_level_access
- (Optional, Default: false) Enables Uniform bucket-level access access to a bucket.
public_access_prevention
- (Optional) Prevents public access to a bucket. Acceptable values are "inherited" or "enforced". If "inherited", the bucket uses public access prevention. only if the bucket is subject to the public access prevention organization policy constraint. Defaults to "inherited".
custom_placement_config
- (Optional) The bucket's custom location configuration, which specifies the individual regions that comprise a dual-region bucket. If the bucket is designated a single or multi-region, the parameters are empty. Structure is documented below.
soft_delete_policy
- (Optional, Computed) The bucket's soft delete policy, which defines the period of time that soft-deleted objects will be retained, and cannot be permanently deleted. If the block is not provided, Server side value will be kept which means removal of block won't generate any terraform change. Structure is documented below.
The lifecycle_rule
block supports:
action
- (Required) The Lifecycle Rule's action configuration. A single block of this type is supported. Structure is documented below.
condition
- (Required) The Lifecycle Rule's condition configuration. A single block of this type is supported. Structure is documented below.
type
- The type of the action of this Lifecycle Rule. Supported values include: Delete
, SetStorageClass
and AbortIncompleteMultipartUpload
.
storage_class
- (Required if action type is SetStorageClass
) The target Storage Class of objects affected by this Lifecycle Rule. Supported values include: STANDARD
, MULTI_REGIONAL
, REGIONAL
, NEARLINE
, COLDLINE
, ARCHIVE
.
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:
age
- (Optional) Minimum age of an object in days to satisfy this condition.
no_age
- (Optional) While set true
, age
value will be omitted. Note Required to set true
when age
is unset in the config file.
created_before
- (Optional) A date in the RFC 3339 format YYYY-MM-DD. This condition is satisfied when an object is created before midnight of the specified date in UTC.
with_state
- (Optional) Match to live and/or archived objects. Unversioned buckets have only live objects. Supported values include: "LIVE"
, "ARCHIVED"
, "ANY"
.
matches_storage_class
- (Optional) Storage Class of objects to satisfy this condition. Supported values include: STANDARD
, MULTI_REGIONAL
, REGIONAL
, NEARLINE
, COLDLINE
, ARCHIVE
, DURABLE_REDUCED_AVAILABILITY
.
matches_prefix
- (Optional) One or more matching name prefixes to satisfy this condition.
matches_suffix
- (Optional) One or more matching name suffixes to satisfy this condition.
num_newer_versions
- (Optional) Relevant only for versioned objects. The number of newer versions of an object to satisfy this condition.
custom_time_before
- (Optional) A date in the RFC 3339 format YYYY-MM-DD. This condition is satisfied when the customTime metadata for the object is set to an earlier date than the date used in this lifecycle condition.
days_since_custom_time
- (Optional) Days since the date set in the customTime
metadata for the object. This condition is satisfied when the current date and time is at least the specified number of days after the customTime
.
days_since_noncurrent_time
- (Optional) Relevant only for versioned objects. Number of days elapsed since the noncurrent timestamp of an object.
noncurrent_time_before
- (Optional) Relevant only for versioned objects. The date in RFC 3339 (e.g. 2017-06-13
) when the object became nonconcurrent.
enabled
- (Required) While set to true
, autoclass automatically transitions objects in your bucket to appropriate storage classes based on each object's access pattern.
terminal_storage_class
- (Optional) The storage class that objects in the bucket eventually transition to if they are not read for a certain length of time. Supported values include: NEARLINE
, ARCHIVE
.
The versioning
block supports:
enabled
- (Required) While set to true
, versioning is fully enabled for this bucket.The website
block supports the following elements, and requires at least one to be defined:
main_page_suffix
- (Optional) Behaves as the bucket's directory index where
missing objects are treated as potential directories.
not_found_page
- (Optional) The custom object to return when a requested
resource is not found.
origin
- (Optional) The list of Origins eligible to receive CORS response headers. Note: "*" is permitted in the list of origins, and means "any Origin".
method
- (Optional) The list of HTTP methods on which to include CORS response headers, (GET, OPTIONS, POST, etc) Note: "*" is permitted in the list of methods, and means "any method".
response_header
- (Optional) The list of HTTP headers other than the simple response headers to give permission for the user-agent to share across domains.
max_age_seconds
- (Optional) The value, in seconds, to return in the Access-Control-Max-Age header used in preflight responses.
The retention_policy
block supports:
is_locked
- (Optional) If set to true
, the bucket will be locked and permanently restrict edits to the bucket's retention policy. Caution: Locking a bucket is an irreversible action.
retention_period
- (Required) The period of time, in seconds, that objects in the bucket must be retained and cannot be deleted, overwritten, or archived. The value must be less than 2,147,483,647 seconds.
log_bucket
- (Required) The bucket that will receive log objects.
log_object_prefix
- (Optional, Computed) The object prefix for log objects. If it's not provided,
by default GCS sets this to this bucket's name.
The encryption
block supports:
default_kms_key_name
: The id
of a Cloud KMS key that will be used to encrypt objects inserted into this bucket, if no encryption method is specified.
You must pay attention to whether the crypto key is available in the location that this bucket is created in.
See the docs for more details.The custom_placement_config
block supports:
data_locations
- (Required) The list of individual regions that comprise a dual-region bucket. See Cloud Storage bucket locations for a list of acceptable regions. Note: If any of the data_locations changes, it will recreate the bucket.The soft_delete_policy
block supports:
retention_duration_seconds
- (Optional, Default: 604800) The duration in seconds that soft-deleted objects in the bucket will be retained and cannot be permanently deleted. Default value is 604800. The value must be in between 604800(7 days) and 7776000(90 days). Note: To disable the soft delete policy on a bucket, This field must be set to 0.
effective_time
- (Computed) Server-determined value that indicates the time from which the policy, or one with a greater retention, was effective. This value is in RFC 3339 format.
In addition to the arguments listed above, the following computed attributes are exported:
self_link
- The URI of the created resource.
url
- The base URL of the bucket, in the format gs://<bucket-name>
.
This resource provides the following Timeouts configuration options: configuration options:
create
- Default is 4 minutes.update
- Default is 4 minutes.read
- Default is 4 minutes.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).
{{project_id}}/{{bucket}}
{{bucket}}
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}}