Automate Terraform with GitHub Actions
GitHub Actions add continuous integration to GitHub repositories to automate your software builds, tests, and deployments. Automating Terraform with CI/CD enforces configuration best practices, promotes collaboration, and automates the Terraform workflow.
HashiCorp provides GitHub Actions that integrate with the Terraform Cloud API. These actions let you create your own custom CI/CD workflows to meet the needs of your organization.
In this tutorial, you will use HashiCorp's Terraform Cloud GitHub Actions to create a complete Actions workflow to deploy a publicly accessible web server within a Terraform Cloud workspace.
The workflow will:
- Generate a plan for every commit to a pull request branch, which you can review in Terraform Cloud.
- Apply the configuration when you update the
main
branch.
After configuring the GitHub Action, you will create and merge a pull request to test the workflow.
Terraform Cloud's built-in support for GitHub webhooks can accomplish this generic workflow. However, by using HashiCorp's Terraform Cloud GitHub Actions, you can create a custom workflow with additional steps before or after your Terraform operations.
Prerequisites
This tutorial assumes that you are familiar with the Terraform and Terraform Cloud workflows. If you are new to Terraform, complete the Get Started tutorials first. If you are new to Terraform Cloud, complete the Terraform Cloud Get Started tutorials first.
For this tutorial, you will need:
Set up Terraform Cloud
The GitHub Action you create will connect to Terraform Cloud to plan and apply your configuration. Before you set up the Actions workflow, you must create a workspace, add your AWS credentials to your Terraform Cloud workspace, and generate a Terraform Cloud user API token.
First, create a new Terraform Cloud workspace named learn-terraform-github-actions
.
Go to the Create a new Workspace page and select API-driven workflow.
Name your workspace learn-terraform-github-actions
and click Create workspace.
Now, find the AWS credentials you want to use for the workspace, or create a new key pair in the
Type | Variable name | Description | Sensitive |
---|---|---|---|
Environment variable | AWS_ACCESS_KEY_ID | The access key ID from your AWS key pair | No |
Environment variable | AWS_SECRET_ACCESS_KEY | The secret access key from your AWS key pair | Yes |
Terraform Cloud will use these credentials to authenticate to AWS.
Finally, go to the Tokens pagein your Terraform Cloud User Settings. Click on Create an API token, enter GitHub Actions for the Description, then click Generate token.
Save this token in a safe place. You will add it to GitHub later as a secret, so the Actions workflow can authenticate to Terraform Cloud.
Set up a GitHub repository
In your browser, navigate to the Learn Terraform GitHub Actions template repository.
Select Use this template, then select Create a new repository.
In the Owner dropdown, select your personal GitHub account.
Next, enter learn-terraform-github-actions
as the Repository name.
Finally, select Public and click Create repository from template.
In your new repository, navigate to the Settings page. Open the Secrets and variables menu, then select Actions.
Now, select New repository secret. Create a secret named TF_API_TOKEN
, setting the Terraform Cloud API token you created in the previous step as the value.
Then, clone your forked repository to your local machine. Remember to replace YOUR-USER-NAME
with your GitHub username if you are using the command below.
Review Actions workflows
There are several files in your local repository.
main.tf
contains Terraform configuration to deploy a publicly accessible EC2 instance..github/workflows/terraform-plan.yml
defines the Actions workflow that runs Terraform plan..github/workflows/terraform-apply.yml
defines the Actions workflow that runs Terraform apply.
Review Terraform plan workflow
In your editor, open .github/workflows/terraform-plan.yml
.
The first line defines the name of the Actions workflow.
Next, the configuration states that this workflow should only run on pull requests. It also defines environment variables used by the workflow.
Replace YOUR-ORGANIZATION-HERE
with the name of your Terraform Cloud organization and save the file.
Then, the configuration defines a terraform
job, and grants the workflow permission to read the repository contents and write to pull requests.
The workflow defines several steps.
Checkout checks out the repository. Uses defines the GitHub action or Docker image to run that specific step. The checkout step uses GitHub's
actions/checkout@v3
action..github/workflows/terraform-plan.ymlUpload Configuration uploads the Terraform configuration to Terraform Cloud and marks it as speculative so it cannot modify infrastructure.
.github/workflows/terraform-plan.ymlCreate Plan Run creates a speculative plan run in Terraform Cloud using the configuration uploaded in the previous step.
.github/workflows/terraform-plan.ymlGet Plan Output extracts the plan output from the speculative run.
.github/workflows/terraform-plan.ymlUpdate PR adds a comment to the pull request with a link to the run in Terraform Cloud. It also removes any previous comments added by this workflow.
.github/workflows/terraform-plan.yml
Review Terraform apply workflow
In your editor, open .github/workflows/terraform-apply.yml
.
The first line defines the name of the Actions workflow.
Next, the configuration states that this workflow should only run on pushes to the main
branch, which includes Pull Request merges to main
. It also defines environment variables used by the workflow.
Replace YOUR-ORGANIZATION-HERE
with the name of your Terraform Cloud organization and save the file.
Then, the configuration defines a terraform
job, and grants the workflow permission to read the repository contents.
The workflow defines several steps.
Checkout checks out the current configuration. Uses defines the action/Docker image to run that specific step. The checkout step uses GitHub's
actions/checkout@v3
action..github/workflows/terraform-apply.ymlUpload Configuration uploads the Terraform configuration to Terraform Cloud.
.github/workflows/terraform-apply.ymlCreate Apply Run creates a Terraform apply run using the configuration uploaded in the previous step.
.github/workflows/terraform-apply.ymlApply confirms and applies the run.
.github/workflows/terraform-apply.yml
Create pull request
Create a new branch in your forked repository named update-tfc-org
.
Now commit the org name changes you made to the workflow files.
Commit these changes with a message.
Push these changes.
Next, open a pull request from the update-tfc-org
branch. From the base
drop-down, choose the main
branch.
Review and merge pull request
Navigate to your pull request. Your PR will trigger the Terraform Plan
Actions workflow. When the workflow completes, it will add a comment with a link to the speculative plan.
Click the Terraform Cloud Plan link to view the plan in Terraform Cloud.
Terraform plans to create three resources, matching the comment in the pull request.
Merge the pull request.
Verify EC2 instance provisioned
In GitHub, go to Actions, then select the pull request you just merged.
Then, click on the Terraform Apply workflow.
Wait for the workflow to complete.
Then, expand the Apply step, scroll to the bottom, and click the link next to View Run in Terraform Cloud.
In Terraform Cloud, expand the Apply finished section. Terraform Cloud shows the resources it created and the EC2 instance's web address.
Copy the web-address
output.
Finally, verify that the EC2 instance is publicly available. Use the curl command below with the web-address
output value.
You have successfully set up a complete GitHub Actions workflow to deploy a publicly accessible web server within a Terraform Cloud workspace.
Destroy resources
Remember to destroy the resources and Terraform Cloud workspace you created for this tutorial.
Go to the learn-terraform-github-actions
workspace, queue a destroy plan, and apply it. Then, delete the workspace from Terraform Cloud.
Next steps
In this tutorial, you deployed a publicly available web server by automating your Terraform Cloud workflow with GitHub Actions. The resources below will help you customize the Actions workflow to fit your real-world use cases.
- Terraform Cloud Workflows for GitHub
- Continuous Integration for Terraform Modules with GitHub Actions
- Terraform and CircleCI tutorial guides you through building an automated Terraform workflow using AWS S3 as a backend.
- Running Terraform in Automation
Comments
Post a Comment