🐹AWS with OpenID Connect
This guide demonstrates how to configure AWS with OpenID Connect role, create permissions, a GitHub Actions workflow, and deploy Terraform code to AWS.
OpenID Connect serves as an identity layer built on the OAuth 2.0 protocol, allowing third-party applications to verify the identity of end-users or, in this context, an AWS role.
AWS role
In AWS, you need to create an identity provider for GitHub and create a role for web identity or OIDC, associating it with the created identity provider.
Creating an Identity Provider for GitHub in AWS
Go to the IAM section -> select Identity Providers
, and then create a new one. Choose OpenID Connect
as the type and enter the following details:
Provider URL:
https://token.actions.githubusercontent.com
Audience:
sts.amazonaws.com
in Provider URL > click on Get thumbprint
.
Configuring the AWS Role
Next, create a role by navigating to IAM -> Roles and opting for a new role creation. Choose "Web Identity" for the role type and select the previously created GitHub identity provider. Assign the role the desired permissions. Give the role a name e.g github_action_role
.
After the role's creation, add the GitHub repository into the role's trust relationships. This integration is done by modifying the trust relationship's JSON configuration to resemble the following, where the GitHub repository and conditions for its access are specified: Go to Roles > click on the role name github_action_role
-> Click on add permissions > Create inline policy -> Select JSON and past the below content. Make sure to replace StringLike
with your repo information.
This setup specifies which GitHub repositories are permitted to assume this AWS role, using a wildcard to include all branches of the example repository.
Setting Up GitHub Actions
To deploy Terraform code to AWS via GitHub Actions, you need to:
Assign Necessary Permissions: The job must have permissions to request the JWT and read the contents for actions like checkout. This is specified in the YAML configuration as:
Configure AWS Credentials in GitHub Actions: Add the
aws-actions/configure-aws-credentials
action to your GitHub job, specifying the role to assume and the AWS region.Incorporate Terraform Operations: Include steps for setting up Terraform, initializing, formatting, and deploying the Terraform code within the GitHub Action workflow.
The complete workflow for deploying Terraform code to AWS using OpenID Connect is detailed in a YAML configuration below, including steps for AWS credential configuration, Terraform setup, deployment commands, and Resourcely CLI setup structured to trigger on specific GitHub events.
Working with multiple AWS accounts
Optionally, If you are working with resource across AWS accounts, Use the aws provider block to provide credentials to each account. AWS Provider credential Instructions
One way to configure this is to use the assume role
block of the AWS provider. In each AWS account create a role for Terraform to plan and apply with. This should include any permissions that Terraform will need to create/modify/delete your resources. Additionally add the trust relationship policy so that the github action role can assume the Terraform per-account role.
Last updated