FEATURES PRICING ABOUT DOCS LOGIN

aws_s3_bucket

Provides a S3 bucket resource.

Example Usage

Private Bucket w/ Tags

resource "aws_s3_bucket" "b" {
    bucket = "my_tf_test_bucket"
    acl = "private"

    tags {
        Name = "My bucket"
        Environment = "Dev"
    }
}

Static Website Hosting

resource "aws_s3_bucket" "b" {
    bucket = "s3-website-test.hashicorp.com"
    acl = "public-read"
    policy = "${file("policy.json")}"

    website {
        index_document = "index.html"
        error_document = "error.html"
        routing_rules = <<EOF
[{
    "Condition": {
        "KeyPrefixEquals": "docs/"
    },
    "Redirect": {
        "ReplaceKeyPrefixWith": "documents/"
    }
}]
EOF
    }
}

Using CORS

resource "aws_s3_bucket" "b" {
    bucket = "s3-website-test.hashicorp.com"
    acl = "public-read"

    cors_rule {
        allowed_headers = ["*"]
        allowed_methods = ["PUT","POST"]
        allowed_origins = ["https://s3-website-test.hashicorp.com"]
        expose_headers = ["ETag"]
        max_age_seconds = 3000
    }
}

Using versioning

resource "aws_s3_bucket" "b" {
    bucket = "my_tf_test_bucket"
    acl = "private"
    versioning {
        enabled = true
    }
}

Enable Logging

resource "aws_s3_bucket" "log_bucket" {
   bucket = "my_tf_log_bucket"
   acl = "log-delivery-write"
}
resource "aws_s3_bucket" "b" {
   bucket = "my_tf_test_bucket"
   acl = "private"
   logging {
	   target_bucket = "${aws_s3_bucket.log_bucket.id}"
	   target_prefix = "log/"
   }
}

Using object lifecycle

resource "aws_s3_bucket" "bucket" {
	bucket = "my-bucket"
	acl = "private"

	lifecycle_rule {
		id = "log"
		prefix = "log/"
		enabled = true

		transition {
			days = 30
			storage_class = "STANDARD_IA"
		}
		transition {
			days = 60
			storage_class = "GLACIER"
		}
		expiration {
			days = 90
		}
	}

	lifecycle_rule {
		id = "tmp"
		prefix = "tmp/"
		enabled = true

		expiration {
			date = "2016-01-12"
		}
	}
}

resource "aws_s3_bucket" "versioning_bucket" {
	bucket = "my-versioning-bucket"
	acl = "private"
	versioning {
	  enabled = false
	}
	lifecycle_rule {
		prefix = "config/"
		enabled = true

		noncurrent_version_transition {
			days = 30
			storage_class = "STANDARD_IA"
		}
		noncurrent_version_transition {
			days = 60
			storage_class = "GLACIER"
		}
		noncurrent_version_expiration {
			days = 90
		}
	}
}

Argument Reference

The following arguments are supported:

~> NOTE: You cannot use acceleration_status in cn-north-1 or us-gov-west-1

The website object supports the following:

The CORS object supports the following:

The versioning object supports the following:

The logging object supports the following:

The ‘lifecycle_rule’ object supports the following:

At least one of expiration, transition, noncurrent_version_expiration, noncurrent_version_transition must be specified.

The expiration object supports the following

The transition object supports the following

The noncurrent_version_expiration object supports the following

The noncurrent_version_transition object supports the following

Attributes Reference

The following attributes are exported:

Import

S3 bucket can be imported using the bucket, e.g.

$ terraform import aws_s3_bucket.bucket bucket-name

See the source of this document at Terraform.io

REQUEST A DEMO