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