Skip to content
Latchkey

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 serverless CLI.
  • 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

Run this faster and cheaper on Latchkey managed runners. Start free →