GitHub Actions deploy role for AWS with OIDC, no stored keys
Let one repository's workflows deploy to AWS by assuming a role, so there is no long-lived access key to leak or rotate.
GitHub Actions
OIDC token
IAM role
ECR push
How it works
Why not an access key
A key stored in repository secrets works until it leaks, and then it works for whoever has it. With OpenID Connect, GitHub signs a short-lived token for each workflow run and AWS swaps it for credentials that expire within the hour.
Register GitHub as an identity provider
The account needs the GitHub OIDC provider once. If another project already added it, set create_oidc_provider to false and this pattern reuses it.
Create a role only your repository can assume
The trust policy checks two claims in the token: the audience, and the subject, which names the repository and branch. The subject condition is the part that matters. Leave it out and any repository on GitHub can assume the role.
Give the role only what a deploy needs
This example allows pushing images to one ECR repository. Add the statements your deploy needs, such as updating one service, and nothing wider.
Use it in the workflow
Give the job permission id-token: write, then call aws-actions/configure-aws-credentials with role-to-assume set to the role_arn output. No secrets are involved.
What you set
github_owner
Organisation or user that owns the repository For example acme
github_repo
Repository name For example web-app
branch
Branch allowed to deploy For example main
ecr_repository_arn
The ECR repository the workflow pushes to For example arn:aws:ecr:us-east-1:111122223333:repository/web-app
What it costs to run
No charge: IAM roles and identity providers are free. Estimate.
The configuration
# GitHub Actions deploy role for AWS, using OpenID Connect (no stored AWS keys).
#
# What this creates:
# - the GitHub OIDC identity provider in your AWS account (once per account)
# - an IAM role that only workflows from ONE repository and ONE branch can assume
# - a policy on that role that you fill in with what a deploy needs
terraform {
required_version = ">= 1.6.0"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
provider "aws" {
region = var.region
}
variable "region" {
description = "AWS region to deploy into."
type = string
default = "us-east-1"
}
variable "github_owner" {
description = "GitHub organisation or user that owns the repository."
type = string
}
variable "github_repo" {
description = "Repository name, without the owner."
type = string
}
variable "branch" {
description = "Branch whose workflows may deploy."
type = string
default = "main"
}
variable "create_oidc_provider" {
description = "Set to false if the account already has the GitHub OIDC provider."
type = bool
default = true
}
variable "ecr_repository_arn" {
description = "ARN of the ECR repository the workflow pushes images to."
type = string
}
data "aws_caller_identity" "current" {}
resource "aws_iam_openid_connect_provider" "github" {
count = var.create_oidc_provider ? 1 : 0
url = "https://token.actions.githubusercontent.com"
client_id_list = ["sts.amazonaws.com"]
thumbprint_list = ["6938fd4d98bab03faadb97b34396831e3780aea1"]
}
locals {
oidc_provider_arn = var.create_oidc_provider ? aws_iam_openid_connect_provider.github[0].arn : "arn:aws:iam::${data.aws_caller_identity.current.account_id}:oidc-provider/token.actions.githubusercontent.com"
}
data "aws_iam_policy_document" "trust" {
statement {
effect = "Allow"
actions = ["sts:AssumeRoleWithWebIdentity"]
principals {
type = "Federated"
identifiers = [local.oidc_provider_arn]
}
condition {
test = "StringEquals"
variable = "token.actions.githubusercontent.com:aud"
values = ["sts.amazonaws.com"]
}
# Only this repository, only this branch. Without the sub condition, any
# repository on GitHub could assume the role.
condition {
test = "StringLike"
variable = "token.actions.githubusercontent.com:sub"
values = ["repo:${var.github_owner}/${var.github_repo}:ref:refs/heads/${var.branch}"]
}
}
}
resource "aws_iam_role" "deploy" {
name = "${var.github_repo}-github-deploy"
assume_role_policy = data.aws_iam_policy_document.trust.json
max_session_duration = 3600
}
data "aws_iam_policy_document" "deploy" {
statement {
sid = "EcrLogin"
effect = "Allow"
actions = ["ecr:GetAuthorizationToken"]
resources = ["*"]
}
statement {
sid = "EcrPush"
effect = "Allow"
actions = [
"ecr:BatchCheckLayerAvailability",
"ecr:CompleteLayerUpload",
"ecr:InitiateLayerUpload",
"ecr:PutImage",
"ecr:UploadLayerPart",
]
resources = [var.ecr_repository_arn]
}
}
resource "aws_iam_role_policy" "deploy" {
name = "deploy"
role = aws_iam_role.deploy.id
policy = data.aws_iam_policy_document.deploy.json
}
output "role_arn" {
description = "Put this in the workflow's role-to-assume."
value = aws_iam_role.deploy.arn
}
Works with OpenTofu 1.6 or later, and with Terraform. Read it before you apply it: it creates real resources in your account.
Written by DevOps Hive. Last updated 2026-11-27.
We use analytics cookies and session recordings to find and fix what is confusing on this site. Privacy