Automating AWS API Gateway Deployment with GitHub Actions and OpenAPI 3.0

Rodrigo Burgos - Feb 14 - - Dev Community

In this guide, we'll walk through setting up a CI/CD pipeline using GitHub Actions to deploy an AWS API Gateway with an OpenAPI 3.0 specification. This automation will streamline updates to your API Gateway whenever changes are pushed to the dev branch.

Prerequisites

  • Before proceeding, ensure you have:

  • An AWS account with API Gateway permissions.

  • An API Gateway REST API already created.

  • An OpenAPI 3.0 definition file (openapi.json) in your repository.

  • GitHub Secrets configured with:

  1. AWS_ACCESS_KEY_ID
  2. AWS_SECRET_ACCESS_KEY
  3. AWS_REGION

GitHub Actions Workflow

Create a new workflow file in your repository at .github/workflows/deploy-api-gateway.yml and add the following configuration:

name: Deploy API Gateway

on:
  push:
    branches:
      - dev
  workflow_dispatch:

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

      - name: Configure AWS credentials
        uses: aws-actions/configure-aws-credentials@v3
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          aws-region: ${{ secrets.AWS_REGION }}

      - name: Update API Gateway with OpenAPI definition
        run: |
          aws apigateway put-rest-api \
            --rest-api-id ${{ secrets.API_GATEWAY_ID }} \
            --body "$(cat openapi.json | base64 --wrap=0)"

      - name: Deploy API Gateway
        run: |
          aws apigateway create-deployment \
            --rest-api-id ${{ secrets.API_GATEWAY_ID }} \
            --stage-name dev
Enter fullscreen mode Exit fullscreen mode

Explanation of Steps

  1. Trigger Conditions: The workflow runs on a push to the dev branch or when manually triggered.

  2. Checkout Repository: Clones the repository so we can access the OpenAPI file.

  3. Configure AWS Credentials: Uses the GitHub Secrets to authenticate with AWS.

  4. Update API Gateway: Uploads the latest OpenAPI 3.0 definition to API Gateway.

  5. Deploy API Gateway: Creates a new deployment in the dev stage.

Benefits of Automation

  • Ensures API Gateway stays in sync with the OpenAPI definition.
  • Eliminates manual updates, reducing errors.
  • Provides a clear version history through GitHub Actions logs.
  • By implementing this workflow, your API updates will be automatically deployed, allowing for a more efficient development process.
. . . . . . . . . . . . . . . . . . . . . . .