Git Product home page Git Product logo

terraform-packer-docker-ec2's Introduction

Terraform Packer Docker EC2

About

Learning experiment leveraging Terraform to automate the infrastructure deployments & Packer to automate baking both the EC2 AMI image and Docker images. In additon pushing the docker image to ECR (AWS Elastic Container Registry) via Packer post-processor.

Setup (local)

*Prerequisites:

  • Have Terraform, Packer, & Docker installed on your machine.*
  • Have an AWS account.

1. Initial Provisioning

Deploy Shared Terraform Config (./infrastructure/terraform/shared)

Directory: ./infrastructure/terraform/shared

cd ./infrastructure/terraform/shared && \
terraform init && \
terraform apply --auto-approve

2. Build AWS EC2 Machine Image with Packer (./infrastructure/packer/images/)

Create an ./infrastructure/packer/images/variables.auto.pkrvars.hcl file

aws_account_id = "<YOUR AWS ACCOUNT ID>"
ecr_repository = "terraform-packer-docker-project"
region = "us-east-1"

Directory: ./infrastructure/packer/images

packer build -var-file="./variables.auto.pkrvars.hcl" ec2.pkr.hcl

3. Build Docker Image with packer

You can alternatively build with regular Dockerfile(s) and script to tag and push to ECR. This is automated with Packer post-processors

Directory: ./infrastructure/packer/images

packer build -var-file="./variables.auto.pkrvars.hcl" docker.pkr.hcl

4. Deploy Application Infrastructure

Since the AWS AMI is built and the Docker image has been pushed to ECR, can deploy EC2 application servers

This terraform configuration is setup for blue/green deployments. To start out only need one of the blue/green servers.

./infrastructure/terraform/application

terraform init && \
terraform apply \
-var "traffic_distribution=blue" \
-var "enable_green_env=false" \
--auto-approve

The blue-green-deployment.sh file is setup to script toggling between blue and green deployments, can reference the traffic_distribution output value and determine which was previously promoted as production servers.

Setup (Github Actions)

  • Fork the repository
  • Create a free Terraform Cloud Account
    • Generate a TFE User Token
    • Create two CLI powered workspaces for terraform-shared & terraform-application
    • Update both Cloud Blocks with your own Terraform Cloud Organization name, current placeholder: <TFC ORGANIZATION HERE>
  • Have an AWS Account and AWS credentials handy
  • Add the following Github Actions Environment Secrets
    • AWS_ACCESS_KEY_ID
    • AWS_SECRET_ACCESS_KEY
    • AWS_ACCOUNT_ID (AWS Account ID)
    • TF_API_TOKEN (From Terraform Cloud)

terraform-packer-docker-ec2's People

Contributors

mjyocca avatar

Stargazers

 avatar

Watchers

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