Code Monkey home page Code Monkey logo

terraform-s3-staticwebsite's Introduction

Terraform-S3-Static-Website

Objective

This tells you how to provision a static website on AWS S3 using Terraform

Prerequisites

How to configure

  1. To begin with, we need an S3 bucket defined in our terraform project. The code block includes website block that sets up web hosting for the bucket. You need to enter the region, aws access key and secret key in the provider.tf file, bucket name and website file location in the s3.tf file.
resource "null_resource" "remove_and_upload_to_s3" {
  provisioner "local-exec" {
    command = "aws s3 sync ${path.module}/website s3://${aws_s3_bucket.bucket.id}"
  }
}
resource "aws_s3_bucket" "bucket" {
  bucket = "s3web.devopsstudy.tech"
  acl    = "public-read"
  policy = file("policy.json")

   website {
    index_document = "index.html"
    error_document = "error.html"
}

  tags = {
    Name        = "s3web.devopsstudy.tech"
  }

output "site-url" {
    value = aws_s3_bucket.bucket.website_endpoint
}
  1. A bucket policy with necessary previlege is attached to the bucket to grant access permissions.
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:GetObject",
	           "Action": "s3:PutObject",
            "Resource": "arn:aws:s3:::s3web.devopsstudy.tech/*"
        }
    ]
}

Usage

  • Clone the repository
$ git clone https://github.com/Swathikarun/terraform-s3-staticwebsite.git

$ cd terraform-s3-staticwebsite

$ terraform init

$ terraform apply
  • Also you can invoke awscli commands using null_resource provisioner to upload the contents in a folder, as suggested here.
resource "null_resource" "remove_and_upload_to_s3" {
  provisioner "local-exec" {
    command = "aws s3 sync ${path.module}/s3Contents s3://${aws_s3_bucket.site.id}"
  }
}

Result

A static website is hosted in S3 bucket and outputs the S3 bucket url.

terraform-s3-staticwebsite's People

Contributors

swathikarun 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.