Code Monkey home page Code Monkey logo

tf-aws-gandi-static-hosting's Introduction

Terraform static s3 hosting using a custom domain name managed outside aws (gandi)

Why this project?

For a side project, I was expecting to host a static website. I then realized that it wasn't that easy to get it cost effectively. I couldn't found an easy way to tick all the following features:

  • Hosted: I don't want to self-host it at home.
  • low cost: I want it to be cost effective - it's a personal project after all
  • static files: I don't want to fire up a server instance for a couple of pages
  • https support: wake up we're in 2022!
  • custom domain: I owned a domain at gandi.net and I don't want to change
  • no repo access: don't ask me why but I'm reluctant to share my repo access (bye cloudflare)

Then I wrote the following Terraform file to host on AWS using my gandi.net domain. The tricky part here is that as s3 hosting http only, you need to setup Cloudfront which can be quite painful:

  1. I need to declare the subdomain zone in Route53
  2. I get the NS from this new Route53 domain to create them in my domain hosted at gandi.net
  3. Aws Certificate Manager can then issue the certificate by using custom CNAME record check.
  4. Everything is then ready to setup the Cloudfront distribution.
  5. I secure the s3 access by using an OAI (Origin Access Identity) keep it private

Easy ? No The following module automate this task using Terraform

Prerequisites

You need:

  • an AWS account with API keys and check that the free tier cover the costs (I bet)
  • a domain name managed on gandi.net and generate the API key

How to use it?

  • Set up the AWS CLI
  • Write your Gandi API KEY in a file called ~/gandi/API_KEY. Remember to limit the access to this file (600)
  • [Optional] The script ./initialize-terraform.sh automatically set the required env variables for you. It uses the values of the default AWS profile. whether you need to customize your aws config, you should to define them as follow
    • Rename the file terraform.tfvars.example as terraform.tfvars
    • edit it with your custom values_
  • Edit the main.tf file following the template example
  • [The first time only] Run ./initialize-terraform.sh init to download the dependencies
  • Run ./initialize-terraform.sh validate to check the file syntax
  • Run ./initialize-terraform.sh plan to check planned actions
  • Run ./initialize-terraform.sh apply to create the infra

Et voilà!

tf-aws-gandi-static-hosting's People

Contributors

leroyj avatar

Stargazers

 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.