Sub topic 16
Managing the provider and role as code
This chapter assumes you have read the earlier chapters on the IAM OIDC provider and the role trust policy. You should also know basic CloudFormation syntax. Creating the provider and role with CLI commands works, but it leaves no record of what exists. Defining both as infrastructure as code (IaC) makes the setup repeatable and reviewable.
Why define the setup as code
Console clicks and one-off CLI commands are hard to review and hard to reproduce in a second account. A template in the repository gives you three things:
- Changes to the trust policy go through a pull request, so a widened
subcondition gets noticed. - The same template can be deployed to a test account and a production account.
- Deleting the stack removes the provider and role together.
A CloudFormation template
The template below creates the OIDC provider and the deployment role. The role uses the same trust policy you wrote by hand earlier.
Resources:
GitHubOidcProvider:
Type: AWS::IAM::OIDCProvider
Properties:
Url: https://token.actions.githubusercontent.com
ClientIdList:
- sts.amazonaws.com
GitHubDeployRole:
Type: AWS::IAM::Role
Properties:
RoleName: github-deploy
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Federated: !Ref GitHubOidcProvider
Action: sts:AssumeRoleWithWebIdentity
Condition:
StringEquals:
token.actions.githubusercontent.com:aud: sts.amazonaws.com
StringLike:
token.actions.githubusercontent.com:sub: repo:my-org/my-repo:ref:refs/heads/mainTwo details matter here:
- For an
AWS::IAM::OIDCProviderresource,!Refreturns the provider's ARN. That is why it works directly as theFederatedprincipal. - The
audandsubconditions are part of the template. Anyone who edits them changes who can assume the role.
Deploying the template
Save the template as github-oidc.yaml and deploy it with the AWS CLI. The --capabilities CAPABILITY_NAMED_IAM flag is required because the template sets a custom role name.
aws cloudformation deploy \
--template-file github-oidc.yaml \
--stack-name github-oidc \
--capabilities CAPABILITY_NAMED_IAMThe role has no permissions policy yet. Add one in the same template, or attach a managed policy, so the role can do what the workflow needs and nothing more.
One provider per account
An AWS account can hold only one IAM OIDC provider for a given URL. If the provider for token.actions.githubusercontent.com already exists, creating it again fails and the stack rolls back.
In accounts with several repositories, create the provider once in a shared stack. Each repository's role then lives in its own stack and refers to the provider by ARN.
Common mistakes
- Putting the
subvalue inStringEqualswith a wildcard. Wildcards only work withStringLike. - Forgetting
CAPABILITY_NAMED_IAM, which makes the deploy fail before any resource is created. - Hard-coding the account ID in the provider ARN instead of using
!Refor a parameter. The template then breaks in other accounts.
Exercise
Your account already has the GitHub OIDC provider, created by hand. A teammate adds the full template from this chapter and the stack fails and rolls back. What is the cause, and what is one way to fix it?
Show answer
An account can have only one OIDC provider per URL, so creating a second one fails. Fix it by removing the provider resource from the template and passing the existing provider's ARN as a parameter, or by importing the existing provider into the stack.