Skip to content

Instantly share code, notes, and snippets.

@haranjackson
Created August 11, 2019 16:48
Show Gist options
  • Save haranjackson/eff0253c50465375103b08682e85113b to your computer and use it in GitHub Desktop.
Save haranjackson/eff0253c50465375103b08682e85113b to your computer and use it in GitHub Desktop.
An AWS CloudFormation template for a 2-stage (prod/dev) deployment on an API using API Gateway, with optional API key
AWSTemplateFormatVersion: 2010-09-09
Parameters:
ApiId:
Type: String
UsagePlanName:
Type: String
Description: >-
The name of the API key usage plan.
If left empty, no API key and usage plan will be created.
Default: ""
Conditions:
USE_API_KEY:
!Not [!Equals ["", !Ref UsagePlanName]]
Resources:
################################
# API
################################
Deployment:
Type: AWS::ApiGateway::Deployment
Properties:
RestApiId: !Ref ApiId
ProdStage:
Type: AWS::ApiGateway::Stage
Properties:
DeploymentId: !Ref Deployment
RestApiId: !Ref ApiId
StageName: prod
TracingEnabled: true
Variables:
lambdaAlias : prod
DevStage:
Type: AWS::ApiGateway::Stage
Properties:
DeploymentId: !Ref Deployment
RestApiId: !Ref ApiId
StageName: dev
TracingEnabled: true
Variables:
lambdaAlias : dev
################################
# API AUTH
################################
UsagePlan:
Condition: USE_API_KEY
DependsOn:
- ProdStage
- DevStage
Type: AWS::ApiGateway::UsagePlan
Properties:
ApiStages:
- ApiId: !Ref ApiId
Stage: prod
- ApiId: !Ref ApiId
Stage: dev
UsagePlanName: !Ref UsagePlanName
ApiKey:
Condition: USE_API_KEY
Type: AWS::ApiGateway::ApiKey
Properties:
Enabled: true
Name: !Sub "${UsagePlanName}Key"
UsagePlanKey:
Condition: USE_API_KEY
Type: AWS::ApiGateway::UsagePlanKey
Properties:
KeyId: !Ref ApiKey
KeyType: API_KEY
UsagePlanId: !Ref UsagePlan
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment