Setting up the GitHub Actions Workflow
To start, we'll be creating a new YAML file in the workflows folder .github/workflows/myfile.yml
in your repository. This will define the workflow for the GitHub Actions by automating the deployment.
So lets break it down step by step:
name: "Terraform Deployment" # Name of the workflow
on:
push: # Trigger the workflow on push events
branches:
- master # Run the workflow when pushing to the main branch
Workflow Trigger:
- The workflow is triggered on push events to the
master
branch.
1: Setting up the GitHub Actions Workflow
Jobs:
The workflow contains a single job named terraform-plan-and-apply
.
jobs:
terraform-plan-and-apply:
name: "Terraform Init, plan and apply" # Name of the job
runs-on: ubuntu-latest # Run the job on an Ubuntu environment
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} # Set the AWS access key ID from secrets
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} # Set the AWS secret access key from secrets
defaults:
run:
working-directory: CICD-terraform # Set the working directory for all steps in the job to "infra"
Job Configuration:
name
: "Terraform Init, plan and apply" - The name of the job.
runs-on
: ubuntu-latest - Specifies that the job will run on an Ubuntu environment.
env
: Specifies environment variables, in this case, AWS access key ID and secret access key are retrieved from GitHub Secrets.
defaults
: Sets a default configuration for all steps in the job. In this case, it sets the working directory to "CICD-terraform".
Setting up the GitHub Actions Workflow
Steps:
steps: - name: Checkout uses: actions/checkout@v2 # Check out the repository code - name: Setup Terraform uses: hashicorp/setup-terraform@v1 # Set up Terraform CLI - name: Terraform Init id: init run: terraform init # Initialize the Terraform working directory - name: Terraform Validate id: validate run: terraform validate -no-color # Validate the Terraform configuration files - name: Terraform Format run: terraform fmt -check # Check the formatting of Terraform files - name: Terraform Plan id: plan run: terraform plan -no-color # Generate and show the execution plan - name: Terraform Apply run: terraform apply -auto-approve # Apply the changes in the Terraform plan without prompting for confirmation
Checkout
: Checks out the repository code using theactions/checkout
action.Setup Terraform
: Sets up the Terraform CLI using thehashicorp/setup-terraform
action.Terraform Init
: Initializes the Terraform working directory.Terraform Validate
: Validates the Terraform configuration files.Terraform Format
: Checks the formatting of Terraform files.Terraform Plan
: Generates and shows the execution plan for Terraform changes.Terraform Apply
: Applies the changes in the Terraform plan without prompting for confirmation (-auto-approve
).
This workflow automates the process of initializing Terraform, validating configuration files, formatting Terraform files, generating an execution plan, and applying changes to the infrastructure. It's designed to run whenever there's a push to the master
branch, making it a part of a continuous integration and continuous deployment (CI/CD) pipeline for managing infrastructure with Terraform.