Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,16 @@ sam deploy \
--capabilities CAPABILITY_IAM CAPABILITY_NAMED_IAM \
--parameter-overrides EnvironmentType="$STAGE"

# Deploy the API key and usage plan
echo "Deploying the API key and usage plan..."
API_ID=$(aws cloudformation list-exports \
--query "Exports[?Name=='InsuranceApiGatewayId-${STAGE}'].Value" \
--output text)

sam deploy \
--stack-name insurance-api-key-usageplan-$STAGE \
--template-file post_deploy_api_key.yaml \
--capabilities CAPABILITY_IAM \
--parameter-overrides ApiId="$API_ID" StageName="$STAGE"

echo "=== Deployment Complete ==="
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
AWSTemplateFormatVersion: '2010-09-09'
Description: Post-deploy stack to create API Key and Usage Plan for existing SAM-managed API Gateway

Parameters:
ApiId:
Type: String
Description: The physical ID of the API Gateway created by the SAM stack
StageName:
Type: String
Default: dev
Description: The stage of the API to attach the usage plan to
AllowedValues:
- dev
- test
- prod

Resources:
# ------------------------
# API Key
# ------------------------
InsuranceApiKey:
Type: AWS::ApiGateway::ApiKey
Properties:
Name: !Sub "insurance-api-key-${StageName}"
Enabled: true
StageKeys:
- RestApiId: !Ref ApiId
StageName: !Ref StageName

# ------------------------
# Usage Plan
# ------------------------
InsuranceApiUsagePlan:
Type: AWS::ApiGateway::UsagePlan
Properties:
UsagePlanName: !Sub "insurance-api-usageplan-${StageName}"
Description: !Sub "Usage plan for insurance API (${StageName})"
ApiStages:
- ApiId: !Ref ApiId
Stage: !Ref StageName
Throttle:
RateLimit: 50
BurstLimit: 100
Quota:
Limit: 1000
Period: MONTH

# ------------------------
# Usage Plan Key
# ------------------------
InsuranceApiUsagePlanKey:
Type: AWS::ApiGateway::UsagePlanKey
Properties:
KeyId: !Ref InsuranceApiKey
KeyType: API_KEY
UsagePlanId: !Ref InsuranceApiUsagePlan
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ Resources:
AllowMethods: "'*'"
AllowHeaders: "'Content-Type,Authorization'"
AllowOrigin: "'*'"
Auth:
ApiKeyRequired: true

Outputs:
InsuranceApiFunction:
Expand All @@ -56,4 +58,10 @@ Outputs:

InsuranceApiGatewayConsole:
Description: API Gateway console URL
Value: !Sub https://console.aws.amazon.com/apigateway/home?region=${AWS::Region}#/apis/${InsuranceApiGateway}/resources
Value: !Sub https://console.aws.amazon.com/apigateway/home?region=${AWS::Region}#/apis/${InsuranceApiGateway}/resources

InsuranceApiGatewayId:
Description: API Gateway ID
Value: !Ref InsuranceApiGateway
Export:
Name: !Sub "InsuranceApiGatewayId-${EnvironmentType}"
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ CREDENTIAL_LOCATION=HEADER
# CREDENTIAL_PARAMETER_NAME: The name of the header/parameter for the API key
# For headers, common names: X-API-Key, X-Subscription-Token, Authorization
# For query parameters: api_key, token
CREDENTIAL_PARAMETER_NAME=X-Subscription-Token
CREDENTIAL_PARAMETER_NAME=X-API-Key

# =============================================================================
# Output Configuration
Expand Down