Secure Terraform Deployments on AWS with GitHub Actions and OIDC
In the world of DevOps, automating infrastructure deployment securely is crucial. This blog post will guide you through setting up GitHub Actions to automatically deploy your Terraform configurations to AWS using OpenID Connect (OIDC) for enhanced security.
Prerequisites
- A GitHub repository with your Terraform configuration
- An AWS account
- Basic knowledge of Terraform and GitHub Actions
Understanding OIDC in GitHub Actions and AWS
Before we dive into the setup, let's understand how OIDC works in this context:
- OpenID Connect (OIDC) is an authentication protocol that allows GitHub Actions to obtain temporary AWS credentials without storing long-term access keys.
- AWS is configured to trust GitHub as an identity provider.
- When a GitHub Action runs, it requests a short-lived OIDC token from GitHub, which includes claims about the workflow's identity.
- This token is sent to AWS's Security Token Service (STS), which verifies the token and issues temporary AWS credentials.
- The GitHub Action then uses these temporary credentials to perform AWS operations.
This process enhances security by eliminating the need for long-term credential storage and providing a dynamic, identity-based access model.
Step 1: Set Up AWS IAM OIDC Provider and Role
First, we need to configure AWS to trust GitHub's OIDC provider and create an IAM role:
- Create an IAM OIDC Provider
- Sign in to the AWS Management Console and navigate to the IAM dashboard.
- In the left navigation pane, click on "Identity providers" under "Access management".
- Click the "Add provider" button.
- For the provider type, select "OpenID Connect".
- For the provider URL, enter:
https://token.actions.githubusercontent.com
- AWS will fetch the OpenID Connect discovery document from this URL to obtain the public keys and verify the OIDC token from GitHub Actions.
- For the "Audience", enter:
sts.amazonaws.com
- This is the intended audience of the OIDC token, which for GitHub Actions is the AWS Security Token Service.
- Click "Get thumbprint" to fetch the server certificate thumbprint.
- Verify the thumbprint and click "Add provider"
- Create an IAM role with the necessary permissions for your Terraform operations.
- Configure the role's trust relationship:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Federated": "arn:aws:iam::YOUR_ACCOUNT_ID:oidc-provider/token.actions.githubusercontent.com"
},
"Action": "sts:AssumeRoleWithWebIdentity",
"Condition": {
"StringLike": {
"token.actions.githubusercontent.com:sub": "repo:YOUR_GITHUB_ORG/YOUR_REPO:*"
}
}
}
]
}
Replace YOUR_ACCOUNT_ID
, YOUR_GITHUB_ORG
, and YOUR_REPO
with your specific values.
Step 2: Create the GitHub Actions Workflow
Create a new file in your repository at .github/workflows/terraform-deploy.yml
:
name: 'Terraform Deploy'
on:
push:
branches:
- main
permissions:
id-token: write
contents: read
jobs:
terraform:
name: 'Terraform'
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v1
with:
role-to-assume: arn:aws:iam::YOUR_ACCOUNT_ID:role/YOUR_ROLE_NAME
aws-region: YOUR_AWS_REGION
- name: Setup Terraform
uses: hashicorp/setup-terraform@v1
- name: Terraform Init
run: terraform init
- name: Terraform Plan
run: terraform plan
- name: Terraform Apply
run: terraform apply -auto-approve
Replace YOUR_ACCOUNT_ID
, YOUR_ROLE_NAME
, and YOUR_AWS_REGION
with your specific values.
How It Works
- When the workflow runs, GitHub generates an OIDC token containing claims about the workflow's identity.
- The
aws-actions/configure-aws-credentials
action exchanges this OIDC token for temporary AWS credentials:- It sends the token to AWS STS.
- AWS verifies the token's signature using GitHub's public keys.
- If valid and meeting the role's trust conditions, AWS issues temporary credentials.
- These temporary credentials are then used for subsequent AWS operations in the workflow.
- Terraform commands use these credentials to authenticate with AWS and perform the necessary operations.
Security Benefits of OIDC
- No long-term AWS access keys stored in GitHub secrets.
- Temporary credentials reduce risk if exposed.
- Easy to audit and rotate permissions by modifying the IAM role.
- Follows the principle of least privilege.
- Trust is based on the GitHub workflow's identity, not a shared secret.
Best Practices
- Use workspaces to manage multiple environments.
- Consider adding a manual approval step before applying changes.
- Implement state locking to prevent concurrent modifications.
- Use Terraform modules to organize and reuse your code.
- Regularly review and update the IAM role's permissions.
- Use condition keys in the trust policy to restrict access to specific repositories or branches.
Conclusion
Implementing this GitHub Actions workflow with OIDC has automated your Terraform deployments to AWS with significantly enhanced security. This setup improves consistency, reduces human error, speeds up your infrastructure deployment process, and adheres to AWS security best practices.
Always review the planned changes before they are applied to your production environment. Happy secure automating!