All Projects → unfor19 → Terraform Multienv

unfor19 / Terraform Multienv

Licence: mit
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.

Programming Languages

shell
77523 projects

Projects that are alternatives of or similar to Terraform Multienv

Terrascan
Detect compliance and security violations across Infrastructure as Code to mitigate risk before provisioning cloud native infrastructure.
Stars: ✭ 2,687 (+2411.21%)
Mutual labels:  terraform, devops, infrastructure
Buildpipeline
AWS-powered serverless build, test and deploy pipeline ft. multiple environments
Stars: ✭ 105 (-1.87%)
Mutual labels:  terraform, infrastructure, ci
Env Ci
Get environment variables exposed by CI services
Stars: ✭ 180 (+68.22%)
Mutual labels:  drone, ci, circleci
Toc
A Table of Contents of all Gruntwork Code
Stars: ✭ 111 (+3.74%)
Mutual labels:  terraform, devops, infrastructure
Terraform With Circleci Example
This is an example of automatic deployments of your infrastructure using terraform and CircleCI 2.0 workflows
Stars: ✭ 142 (+32.71%)
Mutual labels:  terraform, infrastructure, circleci
Terrahub
Terraform Automation and Orchestration Tool (Open Source)
Stars: ✭ 148 (+38.32%)
Mutual labels:  terraform, devops, infrastructure
Doact
A Terraform module for hosting your own runner for CI/CD on Digital Ocean to run jobs in your GitHub Actions workflows. 🚀
Stars: ✭ 42 (-60.75%)
Mutual labels:  terraform, devops, ci
Terraform Security Scan
Run a security scan on your terraform with the very nice https://github.com/liamg/tfsec
Stars: ✭ 64 (-40.19%)
Mutual labels:  terraform, ci
Roll
AWS Blue/Green deployment using Clojure flavoured devops
Stars: ✭ 66 (-38.32%)
Mutual labels:  terraform, devops
Sceptre
Build better AWS infrastructure
Stars: ✭ 1,160 (+984.11%)
Mutual labels:  devops, infrastructure
Terraboard
🌍 📋 A web dashboard to inspect Terraform States
Stars: ✭ 1,192 (+1014.02%)
Mutual labels:  terraform, devops
Terraform Modules
Reusable Terraform modules
Stars: ✭ 63 (-41.12%)
Mutual labels:  terraform, devops
Tau
Tau is a thin wrapper on top of terraform to manage multiple deployments, dependencies and secrets.
Stars: ✭ 53 (-50.47%)
Mutual labels:  terraform, devops
Terraform Aws Couchbase
Reusable infrastructure modules for running Couchbase on AWS
Stars: ✭ 73 (-31.78%)
Mutual labels:  terraform, devops
Mikado
🤖💨Mikado helps managing your AWS infrastructure for WordPress sites by defining an out-of-box, highly available, easy-to-deploy setup
Stars: ✭ 80 (-25.23%)
Mutual labels:  terraform, infrastructure
Module Security Public
The public documentation for the gruntwork-io/module-security repo, which contains packages for setting up best practices for managing secrets, credentials, and servers
Stars: ✭ 67 (-37.38%)
Mutual labels:  terraform, devops
Flow Core X
Powerful and user-friendly CI / CD server with high availability, parallel build, agent scaling
Stars: ✭ 1,108 (+935.51%)
Mutual labels:  devops, ci
Cintodeutilidadesdocker
My Docker templates repository 🐳 ☁️ 🐳
Stars: ✭ 74 (-30.84%)
Mutual labels:  devops, infrastructure
Ecs Pipeline
☁️ 🐳 ⚡️ 🚀 Create environment and deployment pipelines to ECS Fargate with CodePipeline, CodeBuild and Github using Terraform
Stars: ✭ 85 (-20.56%)
Mutual labels:  terraform, devops
Terraformize
Apply\Destory Terraform modules via a simple REST API endpoint.
Stars: ✭ 84 (-21.5%)
Mutual labels:  terraform, devops

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].