Provides an S3 bucket website configuration resource. For more information, see Hosting Websites on S3.
routing_rule
configuredresource "aws_s3_bucket_website_configuration" "example" {
bucket = aws_s3_bucket.example.id
index_document {
suffix = "index.html"
}
error_document {
key = "error.html"
}
routing_rule {
condition {
key_prefix_equals = "docs/"
}
redirect {
replace_key_prefix_with = "documents/"
}
}
}
routing_rules
configuredresource "aws_s3_bucket_website_configuration" "example" {
bucket = aws_s3_bucket.example.id
index_document {
suffix = "index.html"
}
error_document {
key = "error.html"
}
routing_rules = <<EOF
[{
"Condition": {
"KeyPrefixEquals": "docs/"
},
"Redirect": {
"ReplaceKeyPrefixWith": ""
}
}]
EOF
}
This resource supports the following arguments:
bucket
- (Required, Forces new resource) Name of the bucket.error_document
- (Optional, Conflicts with redirect_all_requests_to
) Name of the error document for the website. See below.expected_bucket_owner
- (Optional, Forces new resource) Account ID of the expected bucket owner.index_document
- (Optional, Required if redirect_all_requests_to
is not specified) Name of the index document for the website. See below.redirect_all_requests_to
- (Optional, Required if index_document
is not specified) Redirect behavior for every request to this bucket's website endpoint. See below. Conflicts with error_document
, index_document
, and routing_rule
.routing_rule
- (Optional, Conflicts with redirect_all_requests_to
and routing_rules
) List of rules that define when a redirect is applied and the redirect behavior. See below.routing_rules
- (Optional, Conflicts with routing_rule
and redirect_all_requests_to
) JSON array containing routing rules
describing redirect behavior and when redirects are applied. Use this parameter when your routing rules contain empty String values (""
) as seen in the example above.The error_document
configuration block supports the following arguments:
key
- (Required) Object key name to use when a 4XX class error occurs.The index_document
configuration block supports the following arguments:
suffix
- (Required) Suffix that is appended to a request that is for a directory on the website endpoint.
For example, if the suffix is index.html
and you make a request to samplebucket/images/
, the data that is returned will be for the object with the key name images/index.html
.
The suffix must not be empty and must not include a slash character.The redirect_all_requests_to
configuration block supports the following arguments:
host_name
- (Required) Name of the host where requests are redirected.protocol
- (Optional) Protocol to use when redirecting requests. The default is the protocol that is used in the original request. Valid values: http
, https
.The routing_rule
configuration block supports the following arguments:
condition
- (Optional) Configuration block for describing a condition that must be met for the specified redirect to apply. See below.redirect
- (Required) Configuration block for redirect information. See below.The condition
configuration block supports the following arguments:
http_error_code_returned_equals
- (Optional, Required if key_prefix_equals
is not specified) HTTP error code when the redirect is applied. If specified with key_prefix_equals
, then both must be true for the redirect to be applied.key_prefix_equals
- (Optional, Required if http_error_code_returned_equals
is not specified) Object key name prefix when the redirect is applied. If specified with http_error_code_returned_equals
, then both must be true for the redirect to be applied.The redirect
configuration block supports the following arguments:
host_name
- (Optional) Host name to use in the redirect request.http_redirect_code
- (Optional) HTTP redirect code to use on the response.protocol
- (Optional) Protocol to use when redirecting requests. The default is the protocol that is used in the original request. Valid values: http
, https
.replace_key_prefix_with
- (Optional, Conflicts with replace_key_with
) Object key prefix to use in the redirect request. For example, to redirect requests for all pages with prefix docs/
(objects in the docs/
folder) to documents/
, you can set a condition
block with key_prefix_equals
set to docs/
and in the redirect
set replace_key_prefix_with
to /documents
.replace_key_with
- (Optional, Conflicts with replace_key_prefix_with
) Specific object key to use in the redirect request. For example, redirect request to error.html
.This resource exports the following attributes in addition to the arguments above:
id
- The bucket
or bucket
and expected_bucket_owner
separated by a comma (,
) if the latter is provided.website_domain
- Domain of the website endpoint. This is used to create Route 53 alias records.website_endpoint
- Website endpoint.In Terraform v1.5.0 and later, use an import
block to import S3 bucket website configuration using the bucket
or using the bucket
and expected_bucket_owner
separated by a comma (,
). For example:
If the owner (account ID) of the source bucket is the same account used to configure the Terraform AWS Provider, import using the bucket
:
import {
to = aws_s3_bucket_website_configuration.example
id = "bucket-name"
}
If the owner (account ID) of the source bucket differs from the account used to configure the Terraform AWS Provider, import using the bucket
and expected_bucket_owner
separated by a comma (,
):
import {
to = aws_s3_bucket_website_configuration.example
id = "bucket-name,123456789012"
}
Using terraform import
to import S3 bucket website configuration using the bucket
or using the bucket
and expected_bucket_owner
separated by a comma (,
). For example:
If the owner (account ID) of the source bucket is the same account used to configure the Terraform AWS Provider, import using the bucket
:
% terraform import aws_s3_bucket_website_configuration.example bucket-name
If the owner (account ID) of the source bucket differs from the account used to configure the Terraform AWS Provider, import using the bucket
and expected_bucket_owner
separated by a comma (,
):
% terraform import aws_s3_bucket_website_configuration.example bucket-name,123456789012