How to Deploy with the Serverless Framework in GitHub Actions
The Serverless Framework maps cleanly to stages; CI just needs to pick the right stage and assume the right role.
Install the serverless CLI, assume an AWS role over OIDC, and run serverless deploy --stage <stage> driven by the branch.
Steps
- Create per-stage IAM roles trusting GitHub OIDC.
- Derive the stage from the branch (e.g. main -> prod).
- Install dependencies and the
serverlessCLI. - Run
serverless deploy --stage <stage>.
Workflow
.github/workflows/serverless.yml
name: Serverless Deploy
on:
push:
branches: [main, dev]
permissions:
id-token: write
contents: read
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.AWS_ROLE_ARN }}
aws-region: us-east-1
- uses: actions/setup-node@v4
with: { node-version: '20' }
- run: npm ci
- run: |
STAGE=$([ "${{ github.ref_name }}" = "main" ] && echo prod || echo dev)
npx serverless deploy --stage "$STAGE"Related guides
How to Deploy an AWS Lambda with SAM in GitHub ActionsBuild and deploy a serverless app with AWS SAM from GitHub Actions, authenticating to AWS via OIDC so no long…
How to Authenticate to AWS With OIDC in GitHub ActionsGet short-lived AWS credentials in GitHub Actions via OIDC and aws-actions/configure-aws-credentials, removin…