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.
- An AWS account
- A GitHub account
- Git installed on your local machine
- AWS CLI installed on your local machine
- Terraform installed on your local machine
Repository Setup
- Create a new GitHub repository
- Clone this repository to your local machine
- Check out the
setupbranch - run the cmd
rm -rf .gitto remove the git history - run the cmd
git initto initialize a new git repository - run the cmd
git add .to add all files to the git repository - run the cmd
git commit -m "initial commit"to commit the files - run the cmd
git remote add origin https://your-repo-url-hereto add the new repository as the remote origin - DO NOT push the code to the remote repository yet as the remote state resources have not been created
AWS Resources for Remote State
- Cd Into the local directory
- Set the following variables in the
main.tffile in thelocalfolder:
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"
- Run
terraform initto initialize the Terraform configuration - Run
terraform apply -auto-approveto create the remote state resources in AWS
GitHub Secrets
- In the GitHub repository, go to Settings > Secrets
- 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"
}
}
- Run
terraform initto initialize the Terraform configuration with backend configuration
- The production.yml workflow will be triggered by a push to the master branch or pull request to the master branch
- The staging.yml workflow will be triggered by a push to the staging branch or pull request to the staging branch
- The workflow will:
- Checkout the code
- Install Terraform
- Initialize the Terraform configuration
- Plan the Terraform configuration
- 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.