Skip to content

Resource: awsS3BucketWebsiteConfiguration

Provides an S3 bucket website configuration resource. For more information, see Hosting Websites on S3.

Example Usage

With routingRule configured

/*Provider bindings are generated by running cdktf get.
See https://cdk.tf/provider-generation for more details.*/
import * as aws from "./.gen/providers/aws";
new aws.s3BucketWebsiteConfiguration.S3BucketWebsiteConfiguration(
  this,
  "example",
  {
    bucket: "${aws_s3_bucket.example.id}",
    errorDocument: {
      key: "error.html",
    },
    indexDocument: {
      suffix: "index.html",
    },
    routingRule: [
      {
        condition: {
          keyPrefixEquals: "docs/",
        },
        redirect: {
          replaceKeyPrefixWith: "documents/",
        },
      },
    ],
  }
);

With routingRules configured

/*Provider bindings are generated by running cdktf get.
See https://cdk.tf/provider-generation for more details.*/
import * as aws from "./.gen/providers/aws";
new aws.s3BucketWebsiteConfiguration.S3BucketWebsiteConfiguration(
  this,
  "example",
  {
    bucket: "${aws_s3_bucket.example.id}",
    errorDocument: {
      key: "error.html",
    },
    indexDocument: {
      suffix: "index.html",
    },
    routingRules:
      '[{\n    "Condition": {\n        "KeyPrefixEquals": "docs/"\n    },\n    "Redirect": {\n        "ReplaceKeyPrefixWith": ""\n    }\n}]\n',
  }
);

Argument Reference

The following arguments are supported:

  • bucket - (Required, Forces new resource) Name of the bucket.
  • errorDocument - (Optional, Conflicts with redirectAllRequestsTo) Name of the error document for the website. See below.
  • expectedBucketOwner - (Optional, Forces new resource) Account ID of the expected bucket owner.
  • indexDocument - (Optional, Required if redirectAllRequestsTo is not specified) Name of the index document for the website. See below.
  • redirectAllRequestsTo - (Optional, Required if indexDocument is not specified) Redirect behavior for every request to this bucket's website endpoint. See below. Conflicts with errorDocument, indexDocument, and routingRule.
  • routingRule - (Optional, Conflicts with redirectAllRequestsTo and routingRules) List of rules that define when a redirect is applied and the redirect behavior. See below.
  • routingRules - (Optional, Conflicts with routingRule and redirectAllRequestsTo) 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.

errorDocument

The errorDocument configuration block supports the following arguments:

  • key - (Required) Object key name to use when a 4XX class error occurs.

indexDocument

The indexDocument 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 indexHtml and you make a request to samplebucket/images/, the data that is returned will be for the object with the key name images/indexHtml. The suffix must not be empty and must not include a slash character.

redirectAllRequestsTo

The redirectAllRequestsTo configuration block supports the following arguments:

  • hostName - (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.

routingRule

The routingRule 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.

condition

The condition configuration block supports the following arguments:

  • httpErrorCodeReturnedEquals - (Optional, Required if keyPrefixEquals is not specified) HTTP error code when the redirect is applied. If specified with keyPrefixEquals, then both must be true for the redirect to be applied.
  • keyPrefixEquals - (Optional, Required if httpErrorCodeReturnedEquals is not specified) Object key name prefix when the redirect is applied. If specified with httpErrorCodeReturnedEquals, then both must be true for the redirect to be applied.

redirect

The redirect configuration block supports the following arguments:

  • hostName - (Optional) Host name to use in the redirect request.
  • httpRedirectCode - (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.
  • replaceKeyPrefixWith - (Optional, Conflicts with replaceKeyWith) 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 keyPrefixEquals set to docs/ and in the redirect set replaceKeyPrefixWith to /documents.
  • replaceKeyWith - (Optional, Conflicts with replaceKeyPrefixWith) Specific object key to use in the redirect request. For example, redirect request to errorHtml.

Attributes Reference

In addition to all arguments above, the following attributes are exported:

  • id - The bucket or bucket and expectedBucketOwner separated by a comma (,) if the latter is provided.
  • websiteDomain - Domain of the website endpoint. This is used to create Route 53 alias records.
  • websiteEndpoint - Website endpoint.

Import

S3 bucket website configuration can be imported in one of two ways.

If the owner (account ID) of the source bucket is the same account used to configure the Terraform AWS Provider, the S3 bucket website configuration resource should be imported using the bucket e.g.,

$ 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, the S3 bucket website configuration resource should be imported using the bucket and expectedBucketOwner separated by a comma (,) e.g.,

$ terraform import aws_s3_bucket_website_configuration.example bucket-name,123456789012