├── output.tf ├── variables.tf ├── main.tf ├── iam_policy.tf ├── route53.tf ├── staticSite └── index.html └── README.md /output.tf: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /variables.tf: -------------------------------------------------------------------------------- 1 | variable "domain_name" { 2 | type = string 3 | } -------------------------------------------------------------------------------- /main.tf: -------------------------------------------------------------------------------- 1 | 2 | #//////////////////////////////// 3 | # aws_s3_bucket 4 | #//////////////////////////////// 5 | resource "aws_s3_bucket" "website_bucket" { 6 | bucket = var.domain_name 7 | acl = "public-read" 8 | policy = data.aws_iam_policy_document.website_policy.json 9 | website { 10 | index_document = "index.html" 11 | error_document = "index.html" 12 | } 13 | } 14 | 15 | 16 | -------------------------------------------------------------------------------- /iam_policy.tf: -------------------------------------------------------------------------------- 1 | 2 | #//////////////////////////////// 3 | # IAM policy for S3 4 | #//////////////////////////////// 5 | data "aws_iam_policy_document" "website_policy" { 6 | statement { 7 | actions = [ 8 | "s3:GetObject" 9 | ] 10 | principals { 11 | identifiers = ["*"] 12 | type = "AWS" 13 | } 14 | resources = [ 15 | "arn:aws:s3:::${var.domain_name}/*" 16 | ] 17 | } 18 | } -------------------------------------------------------------------------------- /route53.tf: -------------------------------------------------------------------------------- 1 | 2 | #//////////////////////////////// 3 | # Route 53 4 | #//////////////////////////////// 5 | resource "aws_route53_zone" "primary" { 6 | name = var.domain_name 7 | } 8 | 9 | resource "aws_route53_record" "www" { 10 | zone_id = aws_route53_zone.primary.zone_id 11 | name = var.domain_name 12 | type = "A" 13 | alias { 14 | name = aws_s3_bucket.website_bucket.website_domain 15 | zone_id = aws_s3_bucket.website_bucket.hosted_zone_id 16 | evaluate_target_health = false 17 | } 18 | } -------------------------------------------------------------------------------- /staticSite/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Document 8 | 9 | 44 | 45 |
46 |

Viva Terraform

47 |

you can replace your code!

48 |
49 | 50 | 51 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Agenda 2 | 3 | if you want to create a static web site with s3 you have to create some resources 4 | 5 | 1. bucket 6 | 2. route53 7 | 3. iam 8 | > you have to use a domain for this configuration 9 | > I've used `babak.local`. you can use anything you want.don't worry you don't need to buy a domain name for that 10 | 11 | after you apply terraform configuration on you aws you can copy your static website into the bucket 12 | 13 | ## AWS CLI Command 14 | 15 | `aws s3 sync staticSite s3://babak.local` 16 | 17 | --- 18 | 19 | with this command you copy all the files within the staticSite folder 20 | 21 | 22 | ## What is your web site url? 23 | 24 | you can find it with this formula below 25 | 26 | `http://bucket-name.s3-website-Region.amazonaws.com` 27 | 28 | for example here bucket-name is `babak.local` and region is `eu-west-1` 29 | so the url is 30 | `http://babak.local.s3-website-eu-west-1.amazonaws.com` 31 | 32 | 33 | ## How to use this module? 34 | 35 | 36 | this is very easy! just do the following steps 37 | 38 | `mkdir testFolder` 39 | 40 | `cd testFolder` 41 | 42 | `testFolder> touch main.tf` 43 | 44 | and add the following configuration to the main.tf 45 | 46 | ```terraform 47 | provider "aws" { 48 | profile = "default" 49 | region = "eu-west-1" 50 | } 51 | 52 | variable "domain_name" { 53 | type = string 54 | } 55 | 56 | module "bucket-website" { 57 | source = "babakDoraniArab/bucket-website/aws" 58 | version = "1.0.3" 59 | domain_name = var.domain_name 60 | # please check the link below and use the latest version 61 | # https://registry.terraform.io/modules/babakDoraniArab/bucket-website/aws/latest 62 | } 63 | ``` 64 | 65 | 66 | ### final codes 67 | 68 | `terraform init` 69 | 70 | `terraform fmt` 71 | 72 | `terraform validate` 73 | 74 | `terraform apply` 75 | 76 | 77 | 78 |

congratulation you maid it!

79 | 80 | --------------------------------------------------------------------------------