github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/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  
    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  ```
    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  ```
    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  ```
    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  ```
   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  ```
   174  provider "aws" {
   175    alias  = "west"
   176    region = "eu-west-1"
   177  }
   178  
   179  provider "aws" {
   180    alias  = "central"
   181    region = "eu-central-1"
   182  }
   183  
   184  resource "aws_iam_role" "replication" {
   185    name = "tf-iam-role-replication-12345"
   186  
   187    assume_role_policy = <<POLICY
   188  {
   189    "Version": "2012-10-17",
   190    "Statement": [
   191      {
   192        "Action": "sts:AssumeRole",
   193        "Principal": {
   194          "Service": "s3.amazonaws.com"
   195        },
   196        "Effect": "Allow",
   197        "Sid": ""
   198      }
   199    ]
   200  }
   201  POLICY
   202  }
   203  
   204  resource "aws_iam_policy" "replication" {
   205    name = "tf-iam-role-policy-replication-12345"
   206  
   207    policy = <<POLICY
   208  {
   209    "Version": "2012-10-17",
   210    "Statement": [
   211      {
   212        "Action": [
   213          "s3:GetReplicationConfiguration",
   214          "s3:ListBucket"
   215        ],
   216        "Effect": "Allow",
   217        "Resource": [
   218          "${aws_s3_bucket.bucket.arn}"
   219        ]
   220      },
   221      {
   222        "Action": [
   223          "s3:GetObjectVersion",
   224          "s3:GetObjectVersionAcl"
   225        ],
   226        "Effect": "Allow",
   227        "Resource": [
   228          "${aws_s3_bucket.bucket.arn}/*"
   229        ]
   230      },
   231      {
   232        "Action": [
   233          "s3:ReplicateObject",
   234          "s3:ReplicateDelete"
   235        ],
   236        "Effect": "Allow",
   237        "Resource": "${aws_s3_bucket.destination.arn}/*"
   238      }
   239    ]
   240  }
   241  POLICY
   242  }
   243  
   244  resource "aws_iam_policy_attachment" "replication" {
   245    name       = "tf-iam-role-attachment-replication-12345"
   246    roles      = ["${aws_iam_role.replication.name}"]
   247    policy_arn = "${aws_iam_policy.replication.arn}"
   248  }
   249  
   250  resource "aws_s3_bucket" "destination" {
   251    provider = "aws.west"
   252    bucket   = "tf-test-bucket-destination-12345"
   253    region   = "eu-west-1"
   254  
   255    versioning {
   256      enabled = true
   257    }
   258  }
   259  
   260  resource "aws_s3_bucket" "bucket" {
   261    provider = "aws.central"
   262    bucket   = "tf-test-bucket-12345"
   263    acl      = "private"
   264    region   = "eu-central-1"
   265  
   266    versioning {
   267      enabled = true
   268    }
   269  
   270    replication_configuration {
   271      role = "${aws_iam_role.replication.arn}"
   272  
   273      rules {
   274        id     = "foobar"
   275        prefix = "foo"
   276        status = "Enabled"
   277  
   278        destination {
   279          bucket        = "${aws_s3_bucket.destination.arn}"
   280          storage_class = "STANDARD"
   281        }
   282      }
   283    }
   284  }
   285  ```
   286  
   287  ## Argument Reference
   288  
   289  The following arguments are supported:
   290  
   291  * `bucket` - (Required) The name of the bucket.
   292  * `acl` - (Optional) The [canned ACL](https://docs.aws.amazon.com/AmazonS3/latest/dev/acl-overview.html#canned-acl) to apply. Defaults to "private".
   293  * `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.
   294  
   295  * `tags` - (Optional) A mapping of tags to assign to the bucket.
   296  * `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.
   297  * `website` - (Optional) A website object (documented below).
   298  * `cors_rule` - (Optional) A rule of [Cross-Origin Resource Sharing](https://docs.aws.amazon.com/AmazonS3/latest/dev/cors.html) (documented below).
   299  * `versioning` - (Optional) A state of [versioning](https://docs.aws.amazon.com/AmazonS3/latest/dev/Versioning.html) (documented below)
   300  * `logging` - (Optional) A settings of [bucket logging](https://docs.aws.amazon.com/AmazonS3/latest/UG/ManagingBucketLogging.html) (documented below).
   301  * `lifecycle_rule` - (Optional) A configuration of [object lifecycle management](http://docs.aws.amazon.com/AmazonS3/latest/dev/object-lifecycle-mgmt.html) (documented below).
   302  * `acceleration_status` - (Optional) Sets the accelerate configuration of an existing bucket. Can be `Enabled` or `Suspended`.
   303  * `region` - (Optional) If specified, the AWS region this bucket should reside in. Otherwise, the region used by the callee.
   304  * `request_payer` - (Optional) Specifies who should bear the cost of Amazon S3 data transfer.
   305  Can be either `BucketOwner` or `Requester`. By default, the owner of the S3 bucket would incur
   306  the costs of any data transfer. See [Requester Pays Buckets](http://docs.aws.amazon.com/AmazonS3/latest/dev/RequesterPaysBuckets.html)
   307  developer guide for more information.
   308  * `replication_configuration` - (Optional) A configuration of [replication configuration](http://docs.aws.amazon.com/AmazonS3/latest/dev/crr.html) (documented below).
   309  
   310  ~> **NOTE:** You cannot use `acceleration_status` in `cn-north-1` or `us-gov-west-1`
   311  
   312  The `website` object supports the following:
   313  
   314  * `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.
   315  * `error_document` - (Optional) An absolute path to the document to return in case of a 4XX error.
   316  * `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.
   317  * `routing_rules` - (Optional) A json array containing [routing rules](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-s3-websiteconfiguration-routingrules.html)
   318  describing redirect behavior and when redirects are applied.
   319  
   320  The `CORS` object supports the following:
   321  
   322  * `allowed_headers` (Optional) Specifies which headers are allowed.
   323  * `allowed_methods` (Required) Specifies which methods are allowed. Can be `GET`, `PUT`, `POST`, `DELETE` or `HEAD`.
   324  * `allowed_origins` (Required) Specifies which origins are allowed.
   325  * `expose_headers` (Optional) Specifies expose header in the response.
   326  * `max_age_seconds` (Optional) Specifies time in seconds that browser can cache the response for a preflight request.
   327  
   328  The `versioning` object supports the following:
   329  
   330  * `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.
   331  * `mfa_delete` - (Optional) Enable MFA delete for either `Change the versioning state of your bucket` or `Permanently delete an object version`. Default is `false`.
   332  
   333  The `logging` object supports the following:
   334  
   335  * `target_bucket` - (Required) The name of the bucket that will receive the log objects.
   336  * `target_prefix` - (Optional) To specify a key prefix for log objects.
   337  
   338  The `lifecycle_rule` object supports the following:
   339  
   340  * `id` - (Optional) Unique identifier for the rule.
   341  * `prefix` - (Required) Object key prefix identifying one or more objects to which the rule applies.
   342  * `enabled` - (Required) Specifies lifecycle rule status.
   343  * `abort_incomplete_multipart_upload_days` (Optional) Specifies the number of days after initiating a multipart upload when the multipart upload must be completed.
   344  * `expiration` - (Optional) Specifies a period in the object's expire (documented below).
   345  * `transition` - (Optional) Specifies a period in the object's transitions (documented below).
   346  * `noncurrent_version_expiration` - (Optional) Specifies when noncurrent object versions expire (documented below).
   347  * `noncurrent_version_transition` - (Optional) Specifies when noncurrent object versions transitions (documented below).
   348  
   349  At least one of `expiration`, `transition`, `noncurrent_version_expiration`, `noncurrent_version_transition` must be specified.
   350  
   351  The `expiration` object supports the following
   352  
   353  * `date` (Optional) Specifies the date after which you want the corresponding action to take effect.
   354  * `days` (Optional) Specifies the number of days after object creation when the specific rule action takes effect.
   355  * `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.
   356  
   357  The `transition` object supports the following
   358  
   359  * `date` (Optional) Specifies the date after which you want the corresponding action to take effect.
   360  * `days` (Optional) Specifies the number of days after object creation when the specific rule action takes effect.
   361  * `storage_class` (Required) Specifies the Amazon S3 storage class to which you want the object to transition. Can be `STANDARD_IA` or `GLACIER`.
   362  
   363  The `noncurrent_version_expiration` object supports the following
   364  
   365  * `days` (Required) Specifies the number of days an object is noncurrent object versions expire.
   366  
   367  The `noncurrent_version_transition` object supports the following
   368  
   369  * `days` (Required) Specifies the number of days an object is noncurrent object versions expire.
   370  * `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`.
   371  
   372  The `replication_configuration` object supports the following:
   373  
   374  * `role` - (Required) The ARN of the IAM role for Amazon S3 to assume when replicating the objects.
   375  * `rules` - (Required) Specifies the rules managing the replication (documented below).
   376  
   377  The `rules` object supports the following:
   378  
   379  * `id` - (Optional) Unique identifier for the rule.
   380  * `destination` - (Required) Specifies the destination for the rule (documented below).
   381  * `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.
   382  * `status` - (Required) The status of the rule. Either `Enabled` or `Disabled`. The rule is ignored if status is not Enabled.
   383  
   384  The `destination` object supports the following:
   385  
   386  * `bucket` - (Required) The ARN of the S3 bucket where you want Amazon S3 to store replicas of the object identified by the rule.
   387  * `storage_class` - (Optional) The class of storage used to store the object.
   388  
   389  ## Attributes Reference
   390  
   391  The following attributes are exported:
   392  
   393  * `id` - The name of the bucket.
   394  * `arn` - The ARN of the bucket. Will be of format `arn:aws:s3:::bucketname`.
   395  * `bucket_domain_name` - The bucket domain name. Will be of format `bucketname.s3.amazonaws.com`.
   396  * `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.
   397  * `region` - The AWS region this bucket resides in.
   398  * `website_endpoint` - The website endpoint, if the bucket is configured with a website. If not, this will be an empty string.
   399  * `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.
   400  
   401  ## Import
   402  
   403  S3 bucket can be imported using the `bucket`, e.g.
   404  
   405  ```
   406  $ terraform import aws_s3_bucket.bucket bucket-name
   407  ```