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:
- AWS_ACCESS_KEY_ID
- AWS_SECRET_ACCESS_KEY
- 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
Explanation of Steps
Trigger Conditions: The workflow runs on a push to the dev branch or when manually triggered.
Checkout Repository: Clones the repository so we can access the OpenAPI file.
Configure AWS Credentials: Uses the GitHub Secrets to authenticate with AWS.
Update API Gateway: Uploads the latest OpenAPI 3.0 definition to API Gateway.
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.