AWS Builder Center
Securing Serverless CI/CD: Deploying AWS SAM Apps with GitHub Actions and OIDC

Securing Serverless CI/CD: Deploying AWS SAM Apps with GitHub Actions and OIDC

Learn how to eliminate static AWS access keys in your CI/CD pipelines by integrating OpenID Connect (OIDC) between GitHub Actions and AWS IAM. This step-by-step guide walks you through setting up secretless authentication for deploying AWS SAM serverless applications, enforcing least-privilege security, and automating deployments with dynamic, short-lived credentials.

Series: The Shift-Left Serverless Pipeline. (5 articles)

  1. 1
    Securing Serverless CI/CD: Deploying AWS SAM Apps with GitHub Actions and OIDC This article
Backend Developer aiming for DevSecOps
Hardcoding AWS Access Keys in CI/CD pipeline secrets is a significant security risk. If a secret leaks or a repository is compromised, static credentials give attackers persistent access until manually revoked.
By leveraging OpenID Connect (OIDC) between GitHub Actions and AWS Identity and Access Management (IAM), you eliminate static credentials entirely. Your deployment pipelines request short-lived, temporary security credentials dynamically scoped to a specific repository and branch.
Here is how to set up a complete secretless deployment pipeline for an AWS Serverless Application Model (SAM) project from scratch using the AWS Management Console and GitHub Actions.



Step 1: Register GitHub as an IAM OIDC Identity Provider via AWS Console

Before AWS IAM can issue temporary credentials to GitHub Actions, you must perform a one-time setup to establish trust between AWS and GitHub in your AWS account.
  1. Open the AWS Management Console and navigate to IAM (Identity and Access Management).
  1. In the left navigation pane, click Identity providers, then click Add provider.
  1. For Provider type, select OpenID Connect.
  1. For Provider URL, enter:  https://token.actions.githubusercontent.com 
  1. Click Get thumbprint to verify the provider URL.
  1. For Audience, enter:  sts.amazonaws.com 
  1. Click Add provider.
Note: Alternatively, if you prefer the AWS CLI on your local machine:
aws iam create-open-id-connect-provider \
--url https://token.actions.githubusercontent.com \
--client-id-list sts.amazonaws.com



Step 2: Create the IAM Role for GitHub Actions

Next, create the IAM role that GitHub Actions will assume when running your deployment pipeline.
  1. In the IAM Console, navigate to Roles and click Create role.
  1. Under Trusted entity type, select Web Identity.
  1. Under Identity provider, select  token.actions.githubusercontent.com .
  1. Under Audience, select  sts.amazonaws.com .
  1. Enter your GitHub organization/username and Repository name.
  1. Select your Branch (e.g.,  main ).
  1. Click Next.
  1. Attach the required deployment permissions for your SAM application (e.g., policies granting access to AWS CloudFormation, AWS Lambda, Amazon S3, Amazon ECR, and IAM role creation for serverless resources).
  1. Name the role  GitHubActionsSAMDeployRole  and click Create role.

Verifying the Trust Policy

After creating the role, open it in the IAM Console and review the Trust relationships tab. It should look like this:
{
"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": {
"StringEquals": {
"token.actions.githubusercontent.com:aud": "sts.amazonaws.com"
},
"StringLike": {
"token.actions.githubusercontent.com:sub": "repo:YOUR_GITHUB_HANDLE/YOUR_REPO_NAME:ref:refs/heads/main"
}
}
}
]
}



Step 3: Configure the GitHub Actions Deployment Workflow

In your project repository, grant the workflow  id-token: write  permissions. This allows GitHub to request an OIDC JWT token from GitHub's OIDC provider and exchange it for short-lived AWS credentials using  aws-actions/configure-aws-credentials .
Create  .github/workflows/deploy.yml :
name: Deploy AWS SAM Application

on:
push:
branches:
- main

permissions:
id-token: write
contents: read

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v6

- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: '3.12'

- name: Setup AWS SAM CLI
uses: aws-actions/setup-sam@v2

- name: Configure AWS Credentials via OIDC
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::YOUR_ACCOUNT_ID:role/GitHubActionsSAMDeployRole
aws-region: us-east-1

- name: SAM Build
run: sam build

- name: SAM Deploy
run: |
sam deploy --no-confirm-changeset \
--no-fail-on-empty-changeset \
--stack-name my-serverless-app \
--capabilities CAPABILITY_IAM



DevSecOps Takeaways

  • Zero Static Keys: No  AWS_ACCESS_KEY_ID  or  AWS_SECRET_ACCESS_KEY  stored in GitHub Repository Secrets.
  • Least Privilege Scope: The  sub  condition ensures that only executions originating from your specific repository and target branch ( main ) can assume the role.
  • Automatic Expiration: Credentials are short-lived session tokens generated dynamically per workflow run, completely neutralizing credential leaks.
Adopting OIDC shifts security to the left, ensuring your serverless infrastructure remains secure and compliant from the ground up!

Series: The Shift-Left Serverless Pipeline. (5 articles)

  1. 1
    Securing Serverless CI/CD: Deploying AWS SAM Apps with GitHub Actions and OIDC This article
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article