Skip to content

Latest commit

 

History

56 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Terraform CICD

Introduction

This project is a simple example of how to use Terraform to create a CI/CD pipeline in AWS. The pipeline will be triggered by a push to a GitHub repository, and will deploy a simple web application to an EC2 instance.

Prerequisites

Getting Started

Repository Setup

  1. Create a new GitHub repository
  2. Clone this repository to your local machine
  3. Check out the setup branch
  4. run the cmd rm -rf .git to remove the git history
  5. run the cmd git init to initialize a new git repository
  6. run the cmd git add . to add all files to the git repository
  7. run the cmd git commit -m "initial commit" to commit the files
  8. run the cmd git remote add origin https://your-repo-url-here to add the new repository as the remote origin
  9. DO NOT push the code to the remote repository yet as the remote state resources have not been created

AWS Resources for Remote State

  1. Cd Into the local directory
  2. Set the following variables in the main.tf file in the local folder:
region          = "your-region"
bucket_name     = "your-bucket-name"
bucket_tag_name = "your-bucket-tag-name"
table_name      = "your-table-name"
table_tag_name  = "your-table-tag-name"
  1. Run terraform init to initialize the Terraform configuration
  2. Run terraform apply -auto-approve to create the remote state resources in AWS

GitHub Secrets

  1. In the GitHub repository, go to Settings > Secrets
  2. Add the following secrets:
AWS_ACCESS_KEY_ID     = "your-access-key-id"
AWS_SECRET_ACCESS_KEY = "your-secret
REGION                = "your-region"

Configuring Env Folders

In the main.tf of each environment folder, set your backend configuration:

hcl
terraform {
  backend "s3" {
    bucket         = "your-bucket-name"
    key            = "your-env-specific-key-name"
    region         = "your-region"
    dynamodb_table = "your-table-name"
  }
}
  1. Run terraform init to initialize the Terraform configuration with backend configuration

Deployment Workflow

  1. The production.yml workflow will be triggered by a push to the master branch or pull request to the master branch
  2. The staging.yml workflow will be triggered by a push to the staging branch or pull request to the staging branch
  3. The workflow will:
    1. Checkout the code
    2. Install Terraform
    3. Initialize the Terraform configuration
    4. Plan the Terraform configuration
    5. Apply the Terraform configuration on merge to specific branches

Its best practice to create a new branch for each feature or bug fix and create a pull request to the staging branch. Once the pull request is approved and merged, the staging.yml workflow will be triggered. Once the staging environment deployment is successful, create a pull request to the from the staging branch to the master branch. This will make sure that production is up to date with the staging environment env though no changes were made to the production environment. Once that pull request succeeds, you should now implement the changes to the production environment by adding terraform code to the production environment folder. Once complete, create a pull request to the master branch and, the production.yml workflow will be triggered and on the merge of that branch the changes will be applied to the production environment.

About

No description, website, or topics provided.

Resources

Stars

3 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages