Where BUCKET_NAME is the name you specified for the bucket and AWS_REGION is the region you created the bucket in.
For example, if the bucket was called foo and you deployed it in us-east-1, the URL would be:
foo.s3-website-us-east-1.amazonaws.com
If you set var.create_route53_entry to true, then this module will create a DNS A record in Route
53 for your S3 bucket with the domain name in var.website_domain_name, and you will
be able to use that custom domain name to access your bucket instead of the amazonaws.com domain.
How to configure HTTPS (SSL) or a CDN?
By default, the static content in an S3 bucket is only accessible over HTTP. To be able to access it over HTTPS, you
need to deploy a CloudFront distribution in front of the S3 bucket. This will also act as a Content Distribution
Network (CDN), which will reduce latency for your users. You will need to set the use_with_cloudfront parameter to
true.
If you are using your S3 bucket for both the www. and root domain of a website (e.g. www.foo.com and foo.com),
you need to create two buckets. One of the buckets contains the actual static content. The other sets the
should_redirect_all_requests parameter to true and sets the redirect_all_requests_to parameter to the URL of the
first site. See the Setting Up a Static Website Using a Custom
Domain documentation
for more info.
How do I configure Cross Origin Resource Sharing (CORS)?
NOTE #1: due to a bug in Terraform, you
CANNOT pass multiple origins in the allowed_origins parameter! Instead, add a separate entry to cors_rule for each
origin.
NOTE #2: if you're also using the s3-cloudfront module, you MUST forward the Origin header using the
forward_headers parameter or CORS won't work!
module"s3_cloudfront" {
source = "git::git@github.com:gruntwork-io/terraform-aws-static-assets.git//modules/s3-cloudfront?ref=<VERSION>"# ... other params omitted ...# MUST be specified or CORS won't work
forward_headers = ["Origin"]
}
Questions? Ask away.
We're here to talk about our services, answer any questions, give advice, or just to chat.
{"treedata":{"name":"root","toggled":true,"children":[{"name":".circleci","children":[{"name":"config.yml","path":".circleci/config.yml","sha":"766fd0b4e4ed469daad60e1c87a56738ca26d62b"},{"name":"post-upgrade-test-results.sh","path":".circleci/post-upgrade-test-results.sh","sha":"a4867e8fbdc334b7a90259568ee41ea577fbe764"},{"name":"set-upgrade-test-vars.sh","path":".circleci/set-upgrade-test-vars.sh","sha":"34b835df005388f7a95a0ec8290f9ebad0bac090"}]},{"name":".github","children":[{"name":"ISSUE_TEMPLATE","children":[{"name":"bug_report.md","path":".github/ISSUE_TEMPLATE/bug_report.md","sha":"d2e87e27c601e423865ed660ec697082470ca60f"},{"name":"feature_request.md","path":".github/ISSUE_TEMPLATE/feature_request.md","sha":"023a33099be2336476930c96e17ff1ba5dc55348"}]},{"name":"pull_request_template.md","path":".github/pull_request_template.md","sha":"6b100e40e323b5b07f40ed30616277c51c9f4b9e"}]},{"name":".gitignore","path":".gitignore","sha":"aefaef0f6bfce67d33d546e00f98075aa1c21844"},{"name":".pre-commit-config.yaml","path":".pre-commit-config.yaml","sha":"23068872ca60d7f40ae10c05ea8e5915d04056dc"},{"name":"CODEOWNERS","path":"CODEOWNERS","sha":"5f0e275ee3a38d13d1304c36d5e8640221690376"},{"name":"LICENSE.txt","path":"LICENSE.txt","sha":"f4e3d9bd4717a044ed31ad847a300eee74371a78"},{"name":"README.md","path":"README.md","sha":"d4a23f35f54a390ca83e66646b48101858369896"},{"name":"examples","children":[{"name":"bin","children":[{"name":"s3-upload.sh","path":"examples/bin/s3-upload.sh","sha":"616167707b12a9ed678286f6829d06ae39b8bfb0"}]},{"name":"cloudfront-s3-private-origin-group","children":[{"name":"README.md","path":"examples/cloudfront-s3-private-origin-group/README.md","sha":"eb67a232f842fa217e11124523ec06d3bb2e6b90"},{"name":"main.tf","path":"examples/cloudfront-s3-private-origin-group/main.tf","sha":"239468f5e9186fd8632b934ae4e2b56f8641ad82"},{"name":"outputs.tf","path":"examples/cloudfront-s3-private-origin-group/outputs.tf","sha":"7ad8a7de25248890dd41ec74575fb18e206cf93b"},{"name":"variables.tf","path":"examples/cloudfront-s3-private-origin-group/variables.tf","sha":"461237b7ed653c66d77e0f16ef3a0f904c6c056f"}]},{"name":"cloudfront-s3-private-with-custom-bucket-policy","children":[{"name":"README.md","path":"examples/cloudfront-s3-private-with-custom-bucket-policy/README.md","sha":"9fd41cc48b1b127c68c2bf80bcfb4f77ad4c1498"},{"name":"main.tf","path":"examples/cloudfront-s3-private-with-custom-bucket-policy/main.tf","sha":"9474583d3981bad12c020291ac2d38aa81615c2c"},{"name":"outputs.tf","path":"examples/cloudfront-s3-private-with-custom-bucket-policy/outputs.tf","sha":"4028b7c112eb547208079b450305f8560d451deb"},{"name":"s3-bucket-policies.yaml","path":"examples/cloudfront-s3-private-with-custom-bucket-policy/s3-bucket-policies.yaml","sha":"50f2f62efc50de0f0e5185b1ba975b35cf727f4b"},{"name":"variables.tf","path":"examples/cloudfront-s3-private-with-custom-bucket-policy/variables.tf","sha":"54f69cbdf0ed4798217027c631505bc1b86b02fc"}]},{"name":"cloudfront-s3-private-with-function","children":[{"name":"README.md","path":"examples/cloudfront-s3-private-with-function/README.md","sha":"7af9c93026f727a340aac477c8d83f4a28ec51ce"},{"name":"function","children":[{"name":"index.js","path":"examples/cloudfront-s3-private-with-function/function/index.js","sha":"3e17c169478322745df5b62e61631ab57ceca6a0"}]},{"name":"main.tf","path":"examples/cloudfront-s3-private-with-function/main.tf","sha":"eb7428eb8ecf61debbfbd212ea651e588e586c56"},{"name":"outputs.tf","path":"examples/cloudfront-s3-private-with-function/outputs.tf","sha":"4028b7c112eb547208079b450305f8560d451deb"},{"name":"variables.tf","path":"examples/cloudfront-s3-private-with-function/variables.tf","sha":"1b04323d35a6184e912ed5440ed24955308bfdfe"}]},{"name":"cloudfront-s3-private","children":[{"name":"README.md","path":"examples/cloudfront-s3-private/README.md","sha":"9fd41cc48b1b127c68c2bf80bcfb4f77ad4c1498"},{"name":"main.tf","path":"examples/cloudfront-s3-private/main.tf","sha":"d608a11fcab10fb348cfef4be5167c71816c6420"},{"name":"outputs.tf","path":"examples/cloudfront-s3-private/outputs.tf","sha":"4028b7c112eb547208079b450305f8560d451deb"},{"name":"variables.tf","path":"examples/cloudfront-s3-private/variables.tf","sha":"1b04323d35a6184e912ed5440ed24955308bfdfe"}]},{"name":"cloudfront-s3-public-origin-group","children":[{"name":"README.md","path":"examples/cloudfront-s3-public-origin-group/README.md","sha":"80e5a99578d8c9eba230531ac040a8e82aebfebd"},{"name":"main.tf","path":"examples/cloudfront-s3-public-origin-group/main.tf","sha":"a45031fbba5427bd9d3c163a91fb8354ca47a8f7"},{"name":"outputs.tf","path":"examples/cloudfront-s3-public-origin-group/outputs.tf","sha":"8882441bc7e074e7e77b8e8bf79332bacf7008a6"},{"name":"variables.tf","path":"examples/cloudfront-s3-public-origin-group/variables.tf","sha":"3d74b609cfcc43cceaedcc6cc285c78b51696724"}]},{"name":"cloudfront-s3-public","children":[{"name":"README.md","path":"examples/cloudfront-s3-public/README.md","sha":"aed32ebaa3e145939ee0e027125583200673c104"},{"name":"lambda","children":[{"name":"index.py","path":"examples/cloudfront-s3-public/lambda/index.py","sha":"f9a4c9c0965ed79eb1ad015c7050a0dde2a1ef74"}]},{"name":"main.tf","path":"examples/cloudfront-s3-public/main.tf","sha":"fa16898aa8079a7718c3d75b3fbf72b23c497df0"},{"name":"outputs.tf","path":"examples/cloudfront-s3-public/outputs.tf","sha":"4028b7c112eb547208079b450305f8560d451deb"},{"name":"variables.tf","path":"examples/cloudfront-s3-public/variables.tf","sha":"495258d706da659dbaa1fa914428f9bf7f206da5"}]},{"name":"example-website","children":[{"name":"README.md","path":"examples/example-website/README.md","sha":"359ae6d06f8e77244f18c42704637b8e6ef498c6"},{"name":"error.html","path":"examples/example-website/error.html","sha":"0a959f6cec57577c7c167d362c143e31f215b07c"},{"name":"grunty.png","path":"examples/example-website/grunty.png","sha":"fa4bab8d46d843cd22b20f16bcda72e9d4c86680"},{"name":"index.html","path":"examples/example-website/index.html","sha":"a1457ee18744c564fe0b19b1b1ee1f97434f901d"},{"name":"subfolder","children":[{"name":"index.html","path":"examples/example-website/subfolder/index.html","sha":"496cf61adeb14592511894ceb6250c5b0545b0e7"}]}]},{"name":"s3-static-website","children":[{"name":"README.md","path":"examples/s3-static-website/README.md","sha":"a2d9c34b338551214ccf7df40d63bfa98ae5bae6"},{"name":"main.tf","path":"examples/s3-static-website/main.tf","sha":"ce9d0ff4b919a58103b2383a4ab93879266cc955"},{"name":"outputs.tf","path":"examples/s3-static-website/outputs.tf","sha":"266783fdf9ce2641bb2714ad1e9ada8e7b56f018"},{"name":"variables.tf","path":"examples/s3-static-website/variables.tf","sha":"03f9fb0227c14930cf32e3f38ef0bdf820506a9d"}]}]},{"name":"modules","children":[{"name":"_docs","children":[{"name":"s3-architecture.png","path":"modules/_docs/s3-architecture.png","sha":"24664de39064d5c6767105b75d002bddf763ff82"},{"name":"s3.png","path":"modules/_docs/s3.png","sha":"667a181c90817858914551e9b2fa376fa1c8f177"}]},{"name":"s3-cloudfront","children":[{"name":"README.md","path":"modules/s3-cloudfront/README.md","sha":"ee62c57a95de32148da765bb56217bbe41a9ae5f"},{"name":"main.tf","path":"modules/s3-cloudfront/main.tf","sha":"a9797f2096b46140aace5d85d7ed0c928e653fee"},{"name":"outputs.tf","path":"modules/s3-cloudfront/outputs.tf","sha":"0944924a43237d2cf1faa8ff38311523a1167c7a"},{"name":"variables.tf","path":"modules/s3-cloudfront/variables.tf","sha":"9ab3e54295b98be37171a155100ceca2fabd3205"}]},{"name":"s3-static-website","children":[{"name":"README.adoc","path":"modules/s3-static-website/README.adoc","sha":"3ea276e35d3b46836af9adc09fd40e95105d1ad3"},{"name":"core-concepts.md","path":"modules/s3-static-website/core-concepts.md","sha":"6ae4f6d5a7e9ab999ff4890fba8fec6f2bb5c216","toggled":true},{"name":"main.tf","path":"modules/s3-static-website/main.tf","sha":"71932630d201ae5a79d5fff1b21422792aa0c587"},{"name":"outputs.tf","path":"modules/s3-static-website/outputs.tf","sha":"19d91a00089da131b6f94e5faf03ed49a9d49bfa"},{"name":"variables.tf","path":"modules/s3-static-website/variables.tf","sha":"e96905c21805f4b1f47531f214d75bbcc6c81552"}],"toggled":true}],"toggled":true},{"name":"terraform-cloud-enterprise-private-module-registry-placeholder.tf","path":"terraform-cloud-enterprise-private-module-registry-placeholder.tf","sha":"ae586c0fe830819580e1009d41a9074f16e65bed"},{"name":"test","children":[{"name":"README.md","path":"test/README.md","sha":"2b6dbdbf2af81a41b0b7105894f8ec1746bf8ae8"},{"name":"go.mod","path":"test/go.mod","sha":"7829d4579a6c54d1558a2db134bb980ddc674573"},{"name":"go.sum","path":"test/go.sum","sha":"2fa320c78869cb6fb982638cb8e17463cd4c4fd9"},{"name":"s3_cloudfront_canonical_user_test.go","path":"test/s3_cloudfront_canonical_user_test.go","sha":"83c3998b4d5901a0acb58d04eab18006e8dc3dbf"},{"name":"s3_cloudfront_custom_bucket_policy_test.go","path":"test/s3_cloudfront_custom_bucket_policy_test.go","sha":"702992232bd4b94478818c017c4a907efe00e0ee"},{"name":"s3_cloudfront_function_test.go","path":"test/s3_cloudfront_function_test.go","sha":"eb310d955465b5b0cb3bfc07532dd84ed1efcce2"},{"name":"s3_cloudfront_origin_group_test.go","path":"test/s3_cloudfront_origin_group_test.go","sha":"f6fb71fdc0a5afe17ebfb05df5f7dbc06b1cde5d"},{"name":"s3_cloudfront_test.go","path":"test/s3_cloudfront_test.go","sha":"8bcfe80fb45e14f83a6ef850be7cfc3d0d68b617"},{"name":"s3_static_website_test.go","path":"test/s3_static_website_test.go","sha":"ffc233a8c20259a4665132af20a2b0fbd470c041"},{"name":"test_helpers.go","path":"test/test_helpers.go","sha":"c04949e9f103ae8c63e5d629daccb10d40e28868"},{"name":"upgrades","children":[{"name":"upgrade_test.go","path":"test/upgrades/upgrade_test.go","sha":"a09ac360075309566b4823b516e94c6e8ce8c511"}]},{"name":"validation","children":[{"name":"validate_all_modules_and_examples_test.go","path":"test/validation/validate_all_modules_and_examples_test.go","sha":"33d73c385b64c4fc870033e99427e683c31dc45a"}]}]}]},"detailsContent":"<h2 class=\"preview__body--subtitle\" id=\"quick-start\">Quick Start</h2>\n<ul>\n<li>See the <a href=\"/repos/v0.18.2/package-static-assets/examples/s3-static-website\" class=\"preview__body--description--blue\">s3-static-website example</a> for working sample code.</li>\n<li>Check out <a href=\"/repos/v0.18.2/package-static-assets/modules/s3-static-website/variables.tf\" class=\"preview__body--description--blue\">variables.tf</a> for all parameters you can set for this module.</li>\n</ul>\n<h2 class=\"preview__body--subtitle\" id=\"how-to-test-the-website\">How to test the website?</h2>\n<p>This module outputs the domain name of your website using the <code>website_domain_name</code> output variable.</p>\n<p>By default, the domain name will be of the form:</p>\n<pre><span class=\"hljs-symbol\"><BUCKET_NAME></span>.s3-website-<span class=\"hljs-symbol\"><AWS_REGION></span>.amazonaws.<span class=\"hljs-keyword\">com</span>/\n</pre>\n<p>Where <code>BUCKET_NAME</code> is the name you specified for the bucket and <code>AWS_REGION</code> is the region you created the bucket in.\nFor example, if the bucket was called <code>foo</code> and you deployed it in <code>us-east-1</code>, the URL would be:</p>\n<pre><span class=\"hljs-selector-tag\">foo</span><span class=\"hljs-selector-class\">.s3-website-us-east-1</span><span class=\"hljs-selector-class\">.amazonaws</span><span class=\"hljs-selector-class\">.com</span>\n</pre>\n<p>If you set <code>var.create_route53_entry</code> to true, then this module will create a DNS A record in <a href=\"https://aws.amazon.com/route53/\" class=\"preview__body--description--blue\" target=\"_blank\">Route\n53</a> for your S3 bucket with the domain name in <code>var.website_domain_name</code>, and you will\nbe able to use that custom domain name to access your bucket instead of the <code>amazonaws.com</code> domain.</p>\n<h2 class=\"preview__body--subtitle\" id=\"how-to-configure-https-ssl-or-a-cdn\">How to configure HTTPS (SSL) or a CDN?</h2>\n<p>By default, the static content in an S3 bucket is only accessible over HTTP. To be able to access it over HTTPS, you\nneed to deploy a CloudFront distribution in front of the S3 bucket. This will also act as a Content Distribution\nNetwork (CDN), which will reduce latency for your users. You will need to set the <code>use_with_cloudfront</code> parameter to\n<code>true</code>.</p>\n<p>To set up a CloudFront distribution, see the <a href=\"/repos/v0.18.2/package-static-assets/modules/s3-cloudfront\" class=\"preview__body--description--blue\">s3-cloudfront module</a>.</p>\n<h2 class=\"preview__body--subtitle\" id=\"how-do-i-handle-www-root-domains\">How do I handle www + root domains?</h2>\n<p>If you are using your S3 bucket for both the <code>www.</code> and root domain of a website (e.g. <code>www.foo.com</code> and <code>foo.com</code>),\nyou need to create two buckets. One of the buckets contains the actual static content. The other sets the\n<code>should_redirect_all_requests</code> parameter to <code>true</code> and sets the <code>redirect_all_requests_to</code> parameter to the URL of the\nfirst site. See the <a href=\"http://docs.aws.amazon.com/AmazonS3/latest/dev/website-hosting-custom-domain-walkthrough.html\" class=\"preview__body--description--blue\" target=\"_blank\">Setting Up a Static Website Using a Custom\nDomain</a> documentation\nfor more info.</p>\n<h2 class=\"preview__body--subtitle\" id=\"how-do-i-configure-cross-origin-resource-sharing-cors\">How do I configure Cross Origin Resource Sharing (CORS)?</h2>\n<p>To enable <a href=\"https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS\" class=\"preview__body--description--blue\" target=\"_blank\">Cross Origin Resource Sharing (CORS)</a>, you need to\nset the <code>cors_rule</code> parameter in this module:</p>\n<pre><span class=\"hljs-keyword\">module</span> <span class=\"hljs-string\">\"s3_static_website\"</span> {\n source = <span class=\"hljs-string\">\"git::git@github.com:gruntwork-io/terraform-aws-static-assets.git//modules/s3-static-website?ref=<VERSION>\"</span>\n\n <span class=\"hljs-comment\"># ... other params omitted ...</span>\n\n <span class=\"hljs-comment\"># CORS settings</span>\n cors_rule = [\n {\n allowed_headers = [<span class=\"hljs-string\">\"*\"</span>]\n allowed_methods = [<span class=\"hljs-string\">\"GET\"</span>]\n allowed_origins = [<span class=\"hljs-string\">\"www.your-domain.com\"</span>]\n expose_headers = [<span class=\"hljs-string\">\"ETag\"</span>,<span class=\"hljs-string\">\"Origin\"</span>,<span class=\"hljs-string\">\"Access-Control-Request-Headers\"</span>,<span class=\"hljs-string\">\"Access-Control-Request-Method\"</span>]\n max_age_seconds = <span class=\"hljs-number\">3000</span>\n },\n \n {\n allowed_headers = [<span class=\"hljs-string\">\"*\"</span>]\n allowed_methods = [<span class=\"hljs-string\">\"GET\"</span>]\n allowed_origins = [<span class=\"hljs-string\">\"www.another-domain.com\"</span>]\n expose_headers = [<span class=\"hljs-string\">\"ETag\"</span>,<span class=\"hljs-string\">\"Origin\"</span>,<span class=\"hljs-string\">\"Access-Control-Request-Headers\"</span>,<span class=\"hljs-string\">\"Access-Control-Request-Method\"</span>]\n max_age_seconds = <span class=\"hljs-number\">3000</span>\n }\n ] \n}\n\n</pre>\n<p><strong>NOTE #1</strong>: due to a <a href=\"https://github.com/terraform-providers/terraform-provider-aws/issues/9334\" class=\"preview__body--description--blue\" target=\"_blank\">bug in Terraform</a>, you\nCANNOT pass multiple origins in the <code>allowed_origins</code> parameter! Instead, add a separate entry to <code>cors_rule</code> for each\norigin.</p>\n<p><strong>NOTE #2</strong>: if you're also using the <code>s3-cloudfront</code> module, you MUST forward the <code>Origin</code> header using the\n<code>forward_headers</code> parameter or CORS won't work!</p>\n<pre><span class=\"hljs-keyword\">module</span> <span class=\"hljs-string\">\"s3_cloudfront\"</span> {\n source = <span class=\"hljs-string\">\"git::git@github.com:gruntwork-io/terraform-aws-static-assets.git//modules/s3-cloudfront?ref=<VERSION>\"</span>\n \n <span class=\"hljs-comment\"># ... other params omitted ...</span>\n\n <span class=\"hljs-comment\"># MUST be specified or CORS won't work</span>\n forward_headers = [<span class=\"hljs-string\">\"Origin\"</span>]\n}\n</pre>\n","repoName":"package-static-assets","repoRef":"v0.17.4","serviceDescriptor":{"serviceName":"S3","serviceRepoName":"package-static-assets","serviceRepoOrg":"gruntwork-io","serviceMainReadmePath":"/modules/s3-static-website","cloudProviders":["aws"],"description":"Deploy your static content and static websites on S3. Supports bucket versioning, redirects, and access logging.","imageUrl":"amazon-s3.png","licenseType":"subscriber","technologies":["Terraform"],"compliance":[],"tags":[""]},"serviceCategoryName":"Static content","fileName":"core-concepts.md","filePath":"/modules/s3-static-website/core-concepts.md","title":"Repo Browser: S3","description":"Browse the repos in the Gruntwork Infrastructure as Code Library."}