• Stars
    star
    141
  • Rank 259,971 (Top 6 %)
  • Language
    Shell
  • License
    MIT License
  • Created about 4 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A template for maintaining a multiple environments infrastructure with Terraform. This template includes a CI/CD process, that applies the infrastructure in an AWS account.

terraform-multienv

A template for maintaining a multiple environments infrastructure with Terraform. This template includes a CI/CD process, that applies the infrastructure in an AWS account.

environment drone.io GitHub Actions Circle Ci Travis CI
dev
stg
prd

Assumptions

  • Branches names are aligned with environments names, for example dev, stg and prd

  • The CI/CD tool supports the variable ${BRANCH_NAME}, for example ${DRONE_BRANCH}

  • The directory ./live contains infrastructure-as-code files - *.tf, *.tpl, *.json

  • Multiple Environments

    • All environments are maintained in the same git repository
    • Hosting environments in different AWS account is supported (and recommended)
  • Variables

    • ${app_name} = tfmultienv
    • ${environment} = dev or stg or prd

Getting Started

  1. We're going to create the following resources per environment

    • AWS VPC, Subnets, Routes and Routing Tables, Internet Gateway
    • S3 bucket (website) and an S3 object (index.html)
    • Terraform remote backend - S3 bucket and DynamoDB table
  2. Create a new GitHub repository by clicking - Use this template and don't tick Include all branches

  3. AWS Console > Create IAM Users for the CI/CD service per environment

    • Name: ${app_name}-${environment}-cicd
    • Permissions: Allow Programmatic Access and attach the IAM policy AdministratorAccess (See Recommendations)
    • Create AWS Access Keys and save them in a safe place, we'll use them in the next step
  4. GitHub > Create the following repository secrets for basic application details

    • APP_NAME - Application name, such as tfmultienv
    • AWS_REGION - Region to deploy the application, such as eu-west-1 (Ireland)
  5. GitHub > Create the following repository secrets for authenticating with AWS, according to the access keys that were created in previous steps

    • AWS_ACCESS_KEY_ID_DEV
    • AWS_SECRET_ACCESS_KEY_DEV
      IMPORTANT: The names of the secrets are not arbitrary, make sure you set them as shown in the example below github-secrets-example
  6. Deploying the infrastructure - Commit and push changes to your repository

    git checkout dev
    git add .
    git commit -m "deploy dev"
    git push --set-upstream origin dev
    
  7. Results

  8. Create stg branch

    git checkout dev
    git checkout -b stg
    git push --set-upstream origin stg
    
  9. GitHub > Promote dev environment to stg

    • Create a PR from dev to stg
    • The plan to stg is added as a comment by the terraform-plan pipeline
    • Merge the changes to stg, and check the terraform-apply pipeline in the Actions tab
  10. That's it, you've just deployed two identical environments! Go ahead and do the same with prd

  11. How to proceed from here

    1. Make changes in dev - commit and push
    2. Promote dev to stg - create a PR
    3. Promote stg to prd - create a PR
    4. Revert changes in dev - reverting a commit
    5. Revert changes in stg and prd - reverting a PR

Recommendations

Generic

  • Naming Convention should be consistent across your application and infrastructure. Avoid using master for production. A recommended set of names: dev, tst (qa), stg and prd. Using shorter names is preferred, since some AWS resources' names have a character limit
  • Resources Names should contain the environment name, for example tfmultienv-natgateway-prd
  • Terraform remote backend costs are negligible (less than 5$ per month)
  • Using Multiple AWS Accounts for hosting different environments is recommended.
    The way I implement it - dev and stg in the same account and prd in a different account
  • Create a test environment to test new resources or breaking changes, such as migrating from MySQL to Postgres. The main goal is to avoid breaking the dev environment, which means blocking the development team.

Terraform

  • backend.tf.tpl - Terraform Remote Backend settings per environment. The script prepare-files-folders.sh replaces APP_NAME with TF_VARS_app_name and ENVIRONMENT with BRANCH_NAME
  • Remote Backend is deployed with a CloudFormation template to avoid the chicken and the egg situation
  • Locked Terraform tfstate occurs when a CI/CD process is running per environment. Stopping and restarting, or running multiple deployments to the same environment will result in an error. This is the expected behavior, we don't want multiple entities (CI/CD or Users) to deploy to the same environment at the same time
  • Unlock Terraform tfstate by deleting the items from the state-lock DynamoDB table, for example
    • Table Name: ${app_name}-state-lock-${environment}
    • Item Name: ${app_name}-state-${environment}/terraform.tfstate*

Security

  • AdministratorAccess Permission for CI/CD should be used only in early dev stages. After running a few successful deployments, make sure you restrict the permissions per environment and follow the least-previleged best practice. Use CloudTrail to figure out which IAM policies the CI/CD user needs, a great tool for that - trailscraper
  • IAM Roles for self-hosted CI/CD runners (nodes) are preferred over AWS key/secret

Git

  • Default Branch is dev since this is the branch that is mostly used
  • Branches Names per environment makes the whole CI/CD process simpler
  • Feature Branch per environment complicates the whole process, since creating an environment per feature-branch means creating a Terraform Backend per feature-branch.

Repository Structure

  • Modules should be stored in a different repository
  • Infrastructure Repository should be separated from the Frontend and Backend Respositories. There's no need to re-deploy the infrastructure each time the application changes (loosely coupled)

References

Authors

Created and maintained by Meir Gabay

License

This project is licensed under the MIT License - see the LICENSE file for details

More Repositories

1

kubernetes-localdev

Create a local Kubernetes development environment on macOS or Windows and WSL2, including HTTPS/TLS and OAuth2/OIDC authentication.
220
star
2

bargs

A framework for creating a Bash CLI application.
Shell
156
star
3

install-aws-cli-action

Install AWS CLI on a GitHub Actions Linux host
Shell
60
star
4

githubsecrets

Manage your GitHub Actions secrets with a simple CLI
Python
43
star
5

aws-build-badges

Create AWS status/commit-id badges for CodeBuild & CodePipeline automatically
TypeScript
40
star
6

frigga

Scrape only relevant metrics in Prometheus, according to your Grafana dashboards
Python
22
star
7

serverless-template

Boilerplate template for the serverless-framework
Python
15
star
8

tfcoding

Render Terraform's Expressions and Functions locally without any hassle.
Shell
10
star
9

iamlive-docker

The source code for building iamlive Docker image
Shell
9
star
10

replacer

Find and replace multiline strings
Shell
8
star
11

yarser

A CLI for parsing YAML anchors to regular YAML files.
Go
7
star
12

nexus-ops

Provisioning a preconfigured Nexus Repository Manager (NXRM) Docker container.
Shell
7
star
13

docker-cats

A simple web application that serves different content according to a given environment variable.
Python
5
star
14

terraform-aws-ssm-parameters

Create AWS SSM Parameter Store parameters with a Terraform module. The creation/deletion (schema) is managed with Terraform, and the values should be maintained via AWS Console.
HCL
5
star
15

pwa-quasar-local

This project demonstrates how to develop a Progressive Web Application (PWA) locally on an Android device, using the Quasar Framework v2.
JavaScript
4
star
16

parzival

A CLI that can get/set more than 10 SSM Parameters by path in a single command.
Go
4
star
17

bash-logging

A Logging Framework for Bash. This project aims to provide a solution that can be implemented with a single file import `source logging.sh`.
Shell
3
star
18

aws-sdk-golang-examples

How to use AWS SDK in Go for developers who want to get started with Go
Go
3
star
19

aws-webui

A Single Page Application to manage AWS resources efficiently.
Vue
3
star
20

python-project

Python project structure, relative imports, absolute imports, packages, and modules. Let's make it simpler.
Python
3
star
21

replacer-action

Auto-update README.md file according to the source code.
Shell
3
star
22

alpine-ci

Docker image of Linux alpine, mostly suited for simple CI tasks
Dockerfile
2
star
23

modulecost

Calculate Terraform modules cost, implemented with infracost
Shell
2
star
24

csod-automation

Automating adminstrative tasks in CornerStone On-Demand
Python
2
star
25

unfor19

2
star
26

meetup-31-jan-2023

Microsoft Reactor Meetup - https://www.meetup.com/thecircle/events/290984564/
JavaScript
2
star
27

configmap-action

Exports configmap.json as a job-output according to a given key, such as `GIT_BRANCH` or `DEPLOYMENT_ENVIRONMENT`, which is later consumed by other jobs with "needs".
Shell
2
star
28

tf-tutorial-workspaces

Learn how to use Terraform Cloud and Workspaces
HCL
1
star
29

install-aws-cli-action-test

1
star
30

configmap-action-test

1
star
31

terraform-meetup-live

HCL
1
star
32

kubemanny

Multiple packages for managing your AWS EKS cluster, and deploying Kubeless functions
Shell
1
star
33

devops-genin

A DevOps challenge that will grant you the title Genin (Starter).
1
star
34

terraform-cwagent-ecs-instance-metric

Terraform AWS ECS CloudWatch Agent on ECS
HCL
1
star
35

ecs-stop-task

Shell
1
star