Giter VIP home page Giter VIP logo

s3-static-task's Introduction

Terraform AWS S3 Static Website Deployment

This guide will help you deploy a static website using AWS S3 and Terraform.

Prerequisites

  1. AWS account
  2. IAM user with programmatic access
  3. AWS CLI installed and configured
  4. Terraform installed

Steps

1. Go to AWS Console

Navigate to the AWS Management Console.

2. Create an IAM User for Terraform Actions

Create a new IAM user with programmatic access by following this video tutorial: AWS Made Easy - Create IAM User.

3. Install AWS CLI and Configure

Install the AWS CLI by following the official AWS CLI installation guide.

After installation, configure the AWS CLI using the credentials created in the previous step:

aws configure

Provide the following details:

  • AWS Access Key ID
  • AWS Secret Access Key
  • Default region name (e.g., eu-central-1)
  • Default output format (e.g., json)

4. Edit variables.tf and main.tf

If you wish to change the names of the resources, edit the variables.tf and main.tf files accordingly.

5. Terraform Plan

Initialize Terraform and create an execution plan:

terraform init
terraform plan

6. Terraform Apply

Apply the Terraform configuration to create the resources:

terraform apply

7. Check Your Deployed Webpage

Once the Terraform apply is complete, you can check your deployed static website at the following URLs (replace with your bucket name if you changed it):

File Structure

variables.tf

Define the variables used in the Terraform configuration:

variable "region" {
  description = "The AWS region to create resources in"
  type        = string
  default     = "eu-central-1"
}

variable "s3_bucket_name" {
  description = "The name of the S3 bucket"
  type        = string
  default     = "mik-test1-s3"
}

variable "s3_log_bucket_name" {
  description = "The name of the S3 log bucket"
  type        = string
  default     = "mik-test1-log-bucket"
}

variable "index_html_source" {
  description = "Path to the index.html file"
  type        = string
  default     = "s3_files/html/index.html"
}

variable "error_html_source" {
  description = "Path to the error.html file"
  type        = string
  default     = "s3_files/html/error.html"
}

main.tf

Terraform configuration for creating and configuring the S3 buckets:

provider "aws" {
  region = var.region
}

resource "aws_s3_bucket" "mik-test1-s3" {
  bucket = var.s3_bucket_name

  tags = {
    Name        = "Static Website Bucket"
    Environment = "Test"
  }
}

resource "aws_s3_bucket" "mik-test1-s3-log-bucket" {
  bucket = var.s3_log_bucket_name
}

resource "aws_s3_bucket_ownership_controls" "mik-test1-controls-log" {
  bucket = aws_s3_bucket.mik-test1-s3-log-bucket.id
  rule {
    object_ownership = "BucketOwnerPreferred"
  }
}

resource "aws_s3_bucket_public_access_block" "mik-test1-public_access-log" {
  bucket = aws_s3_bucket.mik-test1-s3-log-bucket.id

  block_public_acls       = false
  block_public_policy     = false
  ignore_public_acls      = false
  restrict_public_buckets = false
}

resource "aws_s3_bucket_acl" "mik-test1-s3-log-bucket_acl" {
  depends_on = [
    aws_s3_bucket_ownership_controls.mik-test1-controls-log,
    aws_s3_bucket_public_access_block.mik-test1-public_access-log,
  ]

  bucket = aws_s3_bucket.mik-test1-s3-log-bucket.id
  acl    = "log-delivery-write"
}

resource "aws_s3_bucket_logging" "mik-tes1-s3-logging" {
  bucket = aws_s3_bucket.mik-test1-s3.id

  target_bucket = aws_s3_bucket.mik-test1-s3-log-bucket.id
  target_prefix = "log/"
}

resource "aws_s3_bucket_versioning" "mik-test1-s3-versioning" {
  bucket = aws_s3_bucket.mik-test1-s3.id
  versioning_configuration {
    status = "Enabled"
  }
}

resource "aws_s3_bucket_ownership_controls" "mik-test1-controls" {
  bucket = aws_s3_bucket.mik-test1-s3.id
  rule {
    object_ownership = "BucketOwnerPreferred"
  }
}

resource "aws_s3_bucket_public_access_block" "mik-test1-public_access" {
  bucket = aws_s3_bucket.mik-test1-s3.id

  block_public_acls       = false
  block_public_policy     = false
  ignore_public_acls      = false
  restrict_public_buckets = false
}

resource "aws_s3_bucket_acl" "mik-test1-acl" {
  depends_on = [
    aws_s3_bucket_ownership_controls.mik-test1-controls,
    aws_s3_bucket_public_access_block.mik-test1-public_access,
  ]

  bucket = aws_s3_bucket.mik-test1-s3.id
  acl    = "public-read"
}

resource "aws_s3_bucket_website_configuration" "website" {
  bucket = aws_s3_bucket.mik-test1-s3.bucket

  index_document {
    suffix = "index.html"
  }

  error_document {
    key = "error.html"
  }
}

resource "aws_s3_bucket_policy" "mik-test1-public_read_policy" {
  bucket = aws_s3_bucket.mik-test1-s3.bucket

  policy = <<POLICY
  {
  "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "PublicReadGetObject",
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::${var.s3_bucket_name}/*"
    }
    ]
  }
  POLICY
}

resource "aws_s3_object" "index_html" {
  bucket       = aws_s3_bucket.mik-test1-s3.bucket
  key          = "index.html"
  source       = var.index_html_source
  content_type = "text/html"
  acl          = "public-read"
}

resource "aws_s3_object" "error_html" {
  bucket       = aws_s3_bucket.mik-test1-s3.bucket
  key          = "error.html"
  source       = var.error_html_source
  content_type = "text/html"
  acl          = "public-read"
}

Conclusion

By following the steps in this guide, you will have a static website hosted on AWS S3 using Terraform. Modify the variables.tf file to change the resource names and other configurations as needed.

s3-static-task's People

Contributors

mikfilldev avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.