Skip to content

github-oidc

Creates the GitHub Actions OIDC identity provider (optional) and an IAM role that GitHub Actions workflows can assume with sts:AssumeRoleWithWebIdentity, without long-lived AWS keys.

The trust policy requires aud = sts.amazonaws.com and matches the token sub claim with StringLike against subjects. Open trust patterns (*, repo:*, wildcard owners) are rejected at plan time.

Usage

module "github_oidc" {
  source = "github.com/rafaelcarvajal05/terraform-stacks//modules/github-oidc?ref=v0.1.0"

  role_name = "github-actions-deploy"
  subjects = [
    "repo:my-org/my-app:ref:refs/heads/main",
    "repo:my-org/my-app:environment:production",
  ]

  managed_policy_arns = ["arn:aws:iam::aws:policy/ReadOnlyAccess"]

  tags = {
    Project = "my-app"
  }
}

Only one GitHub OIDC provider can exist per AWS account. If it already exists, reuse it:

module "github_oidc" {
  source = "github.com/rafaelcarvajal05/terraform-stacks//modules/github-oidc?ref=v0.1.0"

  create_oidc_provider = false
  oidc_provider_arn    = "arn:aws:iam::123456789012:oidc-provider/token.actions.githubusercontent.com"

  role_name = "github-actions-plan"
  subjects  = ["repo:my-org/my-app:ref:refs/heads/main"]
}

GitHub Actions workflow

name: deploy

on:
  push:
    branches: [main]

permissions:
  id-token: write # required to request the OIDC token
  contents: read

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: aws-actions/configure-aws-credentials@v4
        with:
          role-to-assume: ${{ vars.AWS_ROLE_ARN }} # the module's role_arn output
          aws-region: ${{ vars.AWS_REGION }}
      - run: aws sts get-caller-identity

For jobs that use a GitHub environment (environment: production), the token sub becomes repo:<owner>/<repo>:environment:production, so add that pattern to subjects.

Requirements

Name Version
terraform >= 1.6.0
aws >= 5.0

Providers

Name Version
aws >= 5.0

Modules

No modules.

Resources

Name Type
aws_iam_openid_connect_provider.this resource
aws_iam_role.this resource
aws_iam_role_policy.inline resource
aws_iam_role_policy_attachment.managed resource
aws_iam_policy_document.assume_role data source

Inputs

Name Description Type Default Required
role_name Name of the IAM role assumed by GitHub Actions. string n/a yes
subjects GitHub OIDC sub patterns allowed to assume the role, e.g. repo:<owner>/<repo>:ref:refs/heads/<branch> or repo:<owner>/<repo>:environment:<env>. Matched with StringLike; a global * or a wildcard owner (repo:*) is rejected. list(string) n/a yes
create_oidc_provider Whether to create the GitHub Actions OIDC provider. Set to false to reuse an existing one via oidc_provider_arn (only one provider per URL can exist per account). bool true no
inline_policy_json Optional inline IAM policy document (JSON) attached to the role. string null no
managed_policy_arns ARNs of managed IAM policies to attach to the role. Values must be known at plan time. list(string) [] no
max_session_duration Maximum session duration in seconds for the role (3600-43200). number 3600 no
oidc_provider_arn ARN of an existing GitHub Actions OIDC provider. Required when create_oidc_provider is false; ignored otherwise. string null no
permissions_boundary_arn Optional ARN of a permissions boundary policy for the role. string null no
role_description Description of the IAM role. string "Role assumed by GitHub Actions via OIDC." no
role_path Path of the IAM role. string "/" no
tags Tags applied to all resources. A Name tag is added per resource. map(string) {} no
thumbprint_list Server certificate thumbprints for the OIDC provider. AWS no longer validates them for GitHub, but older AWS provider versions require the argument. list(string)
[
"6938fd4d98bab03faadb97b34396831e3780aea1",
"1c58a3a8518e8759bf075b76b750d4f2df264fcd"
]
no

Outputs

Name Description
oidc_provider_arn ARN of the GitHub Actions OIDC provider (created or reused).
role_arn ARN of the IAM role assumed by GitHub Actions.
role_name Name of the IAM role assumed by GitHub Actions.