github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/website/source/docs/providers/aws/r/s3_bucket.html.markdown (about) 1 --- 2 layout: "aws" 3 page_title: "AWS: aws_s3_bucket" 4 sidebar_current: "docs-aws-resource-s3-bucket" 5 description: |- 6 Provides a S3 bucket resource. 7 --- 8 9 # aws\_s3\_bucket 10 11 Provides a S3 bucket resource. 12 13 ## Example Usage 14 15 ### Private Bucket w/ Tags 16 17 ```hcl 18 resource "aws_s3_bucket" "b" { 19 bucket = "my_tf_test_bucket" 20 acl = "private" 21 22 tags { 23 Name = "My bucket" 24 Environment = "Dev" 25 } 26 } 27 ``` 28 29 ### Static Website Hosting 30 31 ```hcl 32 resource "aws_s3_bucket" "b" { 33 bucket = "s3-website-test.hashicorp.com" 34 acl = "public-read" 35 policy = "${file("policy.json")}" 36 37 website { 38 index_document = "index.html" 39 error_document = "error.html" 40 41 routing_rules = <<EOF 42 [{ 43 "Condition": { 44 "KeyPrefixEquals": "docs/" 45 }, 46 "Redirect": { 47 "ReplaceKeyPrefixWith": "documents/" 48 } 49 }] 50 EOF 51 } 52 } 53 ``` 54 55 ### Using CORS 56 57 ```hcl 58 resource "aws_s3_bucket" "b" { 59 bucket = "s3-website-test.hashicorp.com" 60 acl = "public-read" 61 62 cors_rule { 63 allowed_headers = ["*"] 64 allowed_methods = ["PUT", "POST"] 65 allowed_origins = ["https://s3-website-test.hashicorp.com"] 66 expose_headers = ["ETag"] 67 max_age_seconds = 3000 68 } 69 } 70 ``` 71 72 ### Using versioning 73 74 ```hcl 75 resource "aws_s3_bucket" "b" { 76 bucket = "my_tf_test_bucket" 77 acl = "private" 78 79 versioning { 80 enabled = true 81 } 82 } 83 ``` 84 85 ### Enable Logging 86 87 ```hcl 88 resource "aws_s3_bucket" "log_bucket" { 89 bucket = "my_tf_log_bucket" 90 acl = "log-delivery-write" 91 } 92 93 resource "aws_s3_bucket" "b" { 94 bucket = "my_tf_test_bucket" 95 acl = "private" 96 97 logging { 98 target_bucket = "${aws_s3_bucket.log_bucket.id}" 99 target_prefix = "log/" 100 } 101 } 102 ``` 103 104 ### Using object lifecycle 105 106 ```hcl 107 resource "aws_s3_bucket" "bucket" { 108 bucket = "my-bucket" 109 acl = "private" 110 111 lifecycle_rule { 112 id = "log" 113 prefix = "log/" 114 enabled = true 115 116 transition { 117 days = 30 118 storage_class = "STANDARD_IA" 119 } 120 121 transition { 122 days = 60 123 storage_class = "GLACIER" 124 } 125 126 expiration { 127 days = 90 128 } 129 } 130 131 lifecycle_rule { 132 id = "tmp" 133 prefix = "tmp/" 134 enabled = true 135 136 expiration { 137 date = "2016-01-12" 138 } 139 } 140 } 141 142 resource "aws_s3_bucket" "versioning_bucket" { 143 bucket = "my-versioning-bucket" 144 acl = "private" 145 146 versioning { 147 enabled = true 148 } 149 150 lifecycle_rule { 151 prefix = "config/" 152 enabled = true 153 154 noncurrent_version_transition { 155 days = 30 156 storage_class = "STANDARD_IA" 157 } 158 159 noncurrent_version_transition { 160 days = 60 161 storage_class = "GLACIER" 162 } 163 164 noncurrent_version_expiration { 165 days = 90 166 } 167 } 168 } 169 ``` 170 171 ### Using replication configuration 172 173 ```hcl 174 provider "aws" { 175 region = "eu-west-1" 176 } 177 178 provider "aws" { 179 alias = "central" 180 region = "eu-central-1" 181 } 182 183 resource "aws_iam_role" "replication" { 184 name = "tf-iam-role-replication-12345" 185 186 assume_role_policy = <<POLICY 187 { 188 "Version": "2012-10-17", 189 "Statement": [ 190 { 191 "Action": "sts:AssumeRole", 192 "Principal": { 193 "Service": "s3.amazonaws.com" 194 }, 195 "Effect": "Allow", 196 "Sid": "" 197 } 198 ] 199 } 200 POLICY 201 } 202 203 resource "aws_iam_policy" "replication" { 204 name = "tf-iam-role-policy-replication-12345" 205 206 policy = <<POLICY 207 { 208 "Version": "2012-10-17", 209 "Statement": [ 210 { 211 "Action": [ 212 "s3:GetReplicationConfiguration", 213 "s3:ListBucket" 214 ], 215 "Effect": "Allow", 216 "Resource": [ 217 "${aws_s3_bucket.bucket.arn}" 218 ] 219 }, 220 { 221 "Action": [ 222 "s3:GetObjectVersion", 223 "s3:GetObjectVersionAcl" 224 ], 225 "Effect": "Allow", 226 "Resource": [ 227 "${aws_s3_bucket.bucket.arn}/*" 228 ] 229 }, 230 { 231 "Action": [ 232 "s3:ReplicateObject", 233 "s3:ReplicateDelete" 234 ], 235 "Effect": "Allow", 236 "Resource": "${aws_s3_bucket.destination.arn}/*" 237 } 238 ] 239 } 240 POLICY 241 } 242 243 resource "aws_iam_policy_attachment" "replication" { 244 name = "tf-iam-role-attachment-replication-12345" 245 roles = ["${aws_iam_role.replication.name}"] 246 policy_arn = "${aws_iam_policy.replication.arn}" 247 } 248 249 resource "aws_s3_bucket" "destination" { 250 bucket = "tf-test-bucket-destination-12345" 251 region = "eu-west-1" 252 253 versioning { 254 enabled = true 255 } 256 } 257 258 resource "aws_s3_bucket" "bucket" { 259 provider = "aws.central" 260 bucket = "tf-test-bucket-12345" 261 acl = "private" 262 region = "eu-central-1" 263 264 versioning { 265 enabled = true 266 } 267 268 replication_configuration { 269 role = "${aws_iam_role.replication.arn}" 270 271 rules { 272 id = "foobar" 273 prefix = "foo" 274 status = "Enabled" 275 276 destination { 277 bucket = "${aws_s3_bucket.destination.arn}" 278 storage_class = "STANDARD" 279 } 280 } 281 } 282 } 283 ``` 284 285 ## Argument Reference 286 287 The following arguments are supported: 288 289 * `bucket` - (Optional, Forces new resource) The name of the bucket. If omitted, Terraform will assign a random, unique name. 290 * `bucket_prefix` - (Optional, Forces new resource) Creates a unique bucket name beginning with the specified prefix. Conflicts with `name`. 291 * `acl` - (Optional) The [canned ACL](https://docs.aws.amazon.com/AmazonS3/latest/dev/acl-overview.html#canned-acl) to apply. Defaults to "private". 292 * `policy` - (Optional) A valid [bucket policy](https://docs.aws.amazon.com/AmazonS3/latest/dev/example-bucket-policies.html) JSON document. Note that if the policy document is not specific enough (but still valid), Terraform may view the policy as constantly changing in a `terraform plan`. In this case, please make sure you use the verbose/specific version of the policy. 293 294 * `tags` - (Optional) A mapping of tags to assign to the bucket. 295 * `force_destroy` - (Optional, Default:false ) A boolean that indicates all objects should be deleted from the bucket so that the bucket can be destroyed without error. These objects are *not* recoverable. 296 * `website` - (Optional) A website object (documented below). 297 * `cors_rule` - (Optional) A rule of [Cross-Origin Resource Sharing](https://docs.aws.amazon.com/AmazonS3/latest/dev/cors.html) (documented below). 298 * `versioning` - (Optional) A state of [versioning](https://docs.aws.amazon.com/AmazonS3/latest/dev/Versioning.html) (documented below) 299 * `logging` - (Optional) A settings of [bucket logging](https://docs.aws.amazon.com/AmazonS3/latest/UG/ManagingBucketLogging.html) (documented below). 300 * `lifecycle_rule` - (Optional) A configuration of [object lifecycle management](http://docs.aws.amazon.com/AmazonS3/latest/dev/object-lifecycle-mgmt.html) (documented below). 301 * `acceleration_status` - (Optional) Sets the accelerate configuration of an existing bucket. Can be `Enabled` or `Suspended`. 302 * `region` - (Optional) If specified, the AWS region this bucket should reside in. Otherwise, the region used by the callee. 303 * `request_payer` - (Optional) Specifies who should bear the cost of Amazon S3 data transfer. 304 Can be either `BucketOwner` or `Requester`. By default, the owner of the S3 bucket would incur 305 the costs of any data transfer. See [Requester Pays Buckets](http://docs.aws.amazon.com/AmazonS3/latest/dev/RequesterPaysBuckets.html) 306 developer guide for more information. 307 * `replication_configuration` - (Optional) A configuration of [replication configuration](http://docs.aws.amazon.com/AmazonS3/latest/dev/crr.html) (documented below). 308 309 ~> **NOTE:** You cannot use `acceleration_status` in `cn-north-1` or `us-gov-west-1` 310 311 The `website` object supports the following: 312 313 * `index_document` - (Required, unless using `redirect_all_requests_to`) Amazon S3 returns this index document when requests are made to the root domain or any of the subfolders. 314 * `error_document` - (Optional) An absolute path to the document to return in case of a 4XX error. 315 * `redirect_all_requests_to` - (Optional) A hostname to redirect all website requests for this bucket to. Hostname can optionally be prefixed with a protocol (`http://` or `https://`) to use when redirecting requests. The default is the protocol that is used in the original request. 316 * `routing_rules` - (Optional) A json array containing [routing rules](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-s3-websiteconfiguration-routingrules.html) 317 describing redirect behavior and when redirects are applied. 318 319 The `CORS` object supports the following: 320 321 * `allowed_headers` (Optional) Specifies which headers are allowed. 322 * `allowed_methods` (Required) Specifies which methods are allowed. Can be `GET`, `PUT`, `POST`, `DELETE` or `HEAD`. 323 * `allowed_origins` (Required) Specifies which origins are allowed. 324 * `expose_headers` (Optional) Specifies expose header in the response. 325 * `max_age_seconds` (Optional) Specifies time in seconds that browser can cache the response for a preflight request. 326 327 The `versioning` object supports the following: 328 329 * `enabled` - (Optional) Enable versioning. Once you version-enable a bucket, it can never return to an unversioned state. You can, however, suspend versioning on that bucket. 330 * `mfa_delete` - (Optional) Enable MFA delete for either `Change the versioning state of your bucket` or `Permanently delete an object version`. Default is `false`. 331 332 The `logging` object supports the following: 333 334 * `target_bucket` - (Required) The name of the bucket that will receive the log objects. 335 * `target_prefix` - (Optional) To specify a key prefix for log objects. 336 337 The `lifecycle_rule` object supports the following: 338 339 * `id` - (Optional) Unique identifier for the rule. 340 * `prefix` - (Required) Object key prefix identifying one or more objects to which the rule applies. 341 * `enabled` - (Required) Specifies lifecycle rule status. 342 * `abort_incomplete_multipart_upload_days` (Optional) Specifies the number of days after initiating a multipart upload when the multipart upload must be completed. 343 * `expiration` - (Optional) Specifies a period in the object's expire (documented below). 344 * `transition` - (Optional) Specifies a period in the object's transitions (documented below). 345 * `noncurrent_version_expiration` - (Optional) Specifies when noncurrent object versions expire (documented below). 346 * `noncurrent_version_transition` - (Optional) Specifies when noncurrent object versions transitions (documented below). 347 348 At least one of `expiration`, `transition`, `noncurrent_version_expiration`, `noncurrent_version_transition` must be specified. 349 350 The `expiration` object supports the following 351 352 * `date` (Optional) Specifies the date after which you want the corresponding action to take effect. 353 * `days` (Optional) Specifies the number of days after object creation when the specific rule action takes effect. 354 * `expired_object_delete_marker` (Optional) On a versioned bucket (versioning-enabled or versioning-suspended bucket), you can add this element in the lifecycle configuration to direct Amazon S3 to delete expired object delete markers. 355 356 The `transition` object supports the following 357 358 * `date` (Optional) Specifies the date after which you want the corresponding action to take effect. 359 * `days` (Optional) Specifies the number of days after object creation when the specific rule action takes effect. 360 * `storage_class` (Required) Specifies the Amazon S3 storage class to which you want the object to transition. Can be `STANDARD_IA` or `GLACIER`. 361 362 The `noncurrent_version_expiration` object supports the following 363 364 * `days` (Required) Specifies the number of days an object is noncurrent object versions expire. 365 366 The `noncurrent_version_transition` object supports the following 367 368 * `days` (Required) Specifies the number of days an object is noncurrent object versions expire. 369 * `storage_class` (Required) Specifies the Amazon S3 storage class to which you want the noncurrent versions object to transition. Can be `STANDARD_IA` or `GLACIER`. 370 371 The `replication_configuration` object supports the following: 372 373 * `role` - (Required) The ARN of the IAM role for Amazon S3 to assume when replicating the objects. 374 * `rules` - (Required) Specifies the rules managing the replication (documented below). 375 376 The `rules` object supports the following: 377 378 * `id` - (Optional) Unique identifier for the rule. 379 * `destination` - (Required) Specifies the destination for the rule (documented below). 380 * `prefix` - (Required) Object keyname prefix identifying one or more objects to which the rule applies. Set as an empty string to replicate the whole bucket. 381 * `status` - (Required) The status of the rule. Either `Enabled` or `Disabled`. The rule is ignored if status is not Enabled. 382 383 The `destination` object supports the following: 384 385 * `bucket` - (Required) The ARN of the S3 bucket where you want Amazon S3 to store replicas of the object identified by the rule. 386 * `storage_class` - (Optional) The class of storage used to store the object. 387 388 ## Attributes Reference 389 390 The following attributes are exported: 391 392 * `id` - The name of the bucket. 393 * `arn` - The ARN of the bucket. Will be of format `arn:aws:s3:::bucketname`. 394 * `bucket_domain_name` - The bucket domain name. Will be of format `bucketname.s3.amazonaws.com`. 395 * `hosted_zone_id` - The [Route 53 Hosted Zone ID](https://docs.aws.amazon.com/general/latest/gr/rande.html#s3_website_region_endpoints) for this bucket's region. 396 * `region` - The AWS region this bucket resides in. 397 * `website_endpoint` - The website endpoint, if the bucket is configured with a website. If not, this will be an empty string. 398 * `website_domain` - The domain of the website endpoint, if the bucket is configured with a website. If not, this will be an empty string. This is used to create Route 53 alias records. 399 400 ## Import 401 402 S3 bucket can be imported using the `bucket`, e.g. 403 404 ``` 405 $ terraform import aws_s3_bucket.bucket bucket-name 406 ```