From a16bc0e690b036ec4698c4d718afd6815d9ab835 Mon Sep 17 00:00:00 2001 From: Rodrigo Rios Date: Thu, 26 Aug 2021 03:43:18 -0300 Subject: [PATCH 1/3] Adding Static Website Hosting to S3 --- .../s3_static_website/destroy.sh | 2 + aws/aws_s3_bucket/s3_static_website/main.tf | 48 +++++++++++++++++++ .../s3_static_website/policy.json | 16 +++++++ aws/aws_s3_bucket/s3_static_website/run.sh | 2 + 4 files changed, 68 insertions(+) create mode 100755 aws/aws_s3_bucket/s3_static_website/destroy.sh create mode 100644 aws/aws_s3_bucket/s3_static_website/main.tf create mode 100644 aws/aws_s3_bucket/s3_static_website/policy.json create mode 100755 aws/aws_s3_bucket/s3_static_website/run.sh diff --git a/aws/aws_s3_bucket/s3_static_website/destroy.sh b/aws/aws_s3_bucket/s3_static_website/destroy.sh new file mode 100755 index 00000000..0f65744a --- /dev/null +++ b/aws/aws_s3_bucket/s3_static_website/destroy.sh @@ -0,0 +1,2 @@ +#!/bin/bash +../../../bin/destroy.sh aws diff --git a/aws/aws_s3_bucket/s3_static_website/main.tf b/aws/aws_s3_bucket/s3_static_website/main.tf new file mode 100644 index 00000000..9a40fc5d --- /dev/null +++ b/aws/aws_s3_bucket/s3_static_website/main.tf @@ -0,0 +1,48 @@ +# Summary: Setup a static website inside the S3 Bucket + +# Documentation: https://www.terraform.io/docs/language/settings/index.html +terraform { + required_version = ">= 1.0.0" + required_providers { + aws = { + source = "hashicorp/aws" + version = "~> 3.38" + } + } +} + +# Documentation: https://www.terraform.io/docs/language/providers/requirements.html +provider "aws" { + region = "us-east-1" + default_tags { + tags = { + cs_terraform_examples = "aws_s3_bucket/s3_static_website" + } + } +} + + + +# Documentation: https://www.terraform.io/docs/language/providers/requirements.html +# Explanation: Sets a Bucket for Static Website Hosting +# See also: [aws/aws_s3_bucket/simple] (https://github.com/ContainerSolutions/terraform-examples/tree/main/aws/aws_s3_bucket/simple) +resource "aws_s3_bucket" "changeme_aws_static_website" { + bucket = "s3-website-xftcs.example.com" + acl = "public-read" + policy = file("policy.json") + website { + index_document = "index.html" + error_document = "error.html" + + routing_rules = < Date: Thu, 26 Aug 2021 03:48:42 -0300 Subject: [PATCH 2/3] fix index - Adding Static Website Hosting to S3 --- INDEX.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/INDEX.md b/INDEX.md index d4f4f15e..d30a8633 100644 --- a/INDEX.md +++ b/INDEX.md @@ -12,7 +12,7 @@ | `aws_instance` | [aws](aws/aws_instance)

[simple](aws/aws_instance/simple)

[simple_ssh_access](aws/aws_instance/simple_ssh_access) | | `aws_lambda_function` | [aws](aws/aws_lambda_function)

[simple](aws/aws_lambda_function/simple) | | `aws_rds_cluster` | [aws](aws/aws_rds_cluster)

[simple](aws/aws_rds_cluster/simple) | -| `aws_s3_bucket` | [aws, backends, s3](backends/s3/aws_s3_bucket) | +| `aws_s3_bucket` | [aws, backends, s3](backends/s3/aws_s3_bucket)

[s3_static_website](aws/aws_s3_bucket/s3_static_website) | | `aws_security_group` | [aws, ssh](aws/aws_security_group/ssh)

[aws, open](aws/aws_security_group/open) | | `aws_vpc` | [aws](aws/aws_vpc/simple) | | `azuread_application` | [simple](azuread/azuread_application/simple) | From a874305f7684cc40cd525b3a9ad09f11eaa4784b Mon Sep 17 00:00:00 2001 From: Rodrigo Rios Date: Thu, 26 Aug 2021 04:41:28 -0300 Subject: [PATCH 3/3] Adding two outputs --- aws/aws_s3_bucket/s3_static_website/main.tf | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/aws/aws_s3_bucket/s3_static_website/main.tf b/aws/aws_s3_bucket/s3_static_website/main.tf index 9a40fc5d..f78c7dee 100644 --- a/aws/aws_s3_bucket/s3_static_website/main.tf +++ b/aws/aws_s3_bucket/s3_static_website/main.tf @@ -24,8 +24,12 @@ provider "aws" { # Documentation: https://www.terraform.io/docs/language/providers/requirements.html -# Explanation: Sets a Bucket for Static Website Hosting # See also: [aws/aws_s3_bucket/simple] (https://github.com/ContainerSolutions/terraform-examples/tree/main/aws/aws_s3_bucket/simple) +# Explanation: Sets a Bucket for Static Website Hosting +# Now you have to add your files to the bucket index.html and also acces with this naming scheme , dependig on your region: +# s3-website dash (-) Region ‐ http://bucket-name.s3-website-Region.amazonaws.com +# s3-website dot (.) Region ‐ http://bucket-name.s3-website.Region.amazonaws.com + resource "aws_s3_bucket" "changeme_aws_static_website" { bucket = "s3-website-xftcs.example.com" acl = "public-read" @@ -45,4 +49,13 @@ resource "aws_s3_bucket" "changeme_aws_static_website" { }] EOF } -} \ No newline at end of file +} + + +output "Website-Endpoint" { + value = aws_s3_bucket.changeme_aws_static_website.website_endpoint +} +output "Website-Domain" { + value = aws_s3_bucket.changeme_aws_static_website.website_domain +} +