Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save aquiseb/a8b19e0d0a4f78abab26e191b6f8adcf to your computer and use it in GitHub Desktop.
Save aquiseb/a8b19e0d0a4f78abab26e191b6f8adcf to your computer and use it in GitHub Desktop.

AWS Cloudformation ApiGateway vs ApiGatewayV2 with Lambda

This shows how to convert a basic REST ApiGateway template to its equivalent HTTP ApiGatewayV2.

The original code before refactoring to ApiGatewayV2 comes from this article

Deployment

Replace MY_PROFILE, MY_REGION and MY_STACK_NAME

aws cloudformation deploy --profile MY_PROFILE --region MY_REGION --stack-name MY_STACK_NAME --template-file template.yml --capabilities CAPABILITY_IAM

Call the Api

After it deployed successfully, run this command to call your API (replace MY_API_GATEWAY_ID and MY_REGION).

curl -X POST https://MY_API_GATEWAY_ID.execute-api.MY_REGION.amazonaws.com/v0/lambda
# This is a refactor that uses ApiGatewayV2
# It was based on this tutorial which uses V1 - https://nickolaskraus.org/articles/creating-an-amazon-api-gateway-with-a-lambda-integration-using-cloudformation/#Output%20Format%20of%20a%20Lambda%20Function%20for%20Proxy%20Integration
# Run this command to deploy
# Don't forget to replace "my-profile", "my-region" and "my-api"
# aws cloudformation deploy --profile my-profile --region my-region --stack-name my-api --template-file template.yml --capabilities CAPABILITY_IAM
# Run this command when it's deployed
# http -v POST \
# https://MY_API_GATEWAY_ID.execute-api.MY_REGION.amazonaws.com/v0/lambda
AWSTemplateFormatVersion: "2010-09-09"
Description: AWS API Gateway with a Lambda Integration
Resources:
# ApiGatewayRestApi:
# Type: AWS::ApiGateway::RestApi
# Properties:
# ApiKeySourceType: HEADER
# Description: An API Gateway with a Lambda Integration
# EndpointConfiguration:
# Types:
# - EDGE
# Name: lambda-api
ApiGatewayRestApi:
Type: AWS::ApiGatewayV2::Api
Properties:
ProtocolType: HTTP
Description: An API Gateway with a Lambda Integration
Name: lambda-api
# ApiGatewayMethod:
# Type: AWS::ApiGateway::Method
# Properties:
# ApiKeyRequired: false
# AuthorizationType: NONE
# HttpMethod: POST
# Integration:
# ConnectionType: INTERNET
# Credentials: !GetAtt ApiGatewayIamRole.Arn
# IntegrationHttpMethod: POST
# PassthroughBehavior: WHEN_NO_MATCH
# TimeoutInMillis: 29000
# Type: AWS_PROXY
# Uri: !Sub "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${LambdaFunction.Arn}/invocations"
# OperationName: "lambda"
# ResourceId: !Ref ApiGatewayResource
# RestApiId: !Ref ApiGatewayRestApi
ApiGatewayMethod:
Type: AWS::ApiGatewayV2::Integration
Properties:
ApiId: !Ref ApiGatewayRestApi
Description: Test Integration
ConnectionType: INTERNET
CredentialsArn: !GetAtt ApiGatewayIamRole.Arn
PassthroughBehavior: WHEN_NO_MATCH
TimeoutInMillis: 29000
IntegrationMethod: POST
IntegrationType: AWS_PROXY
PayloadFormatVersion: "2.0"
IntegrationUri: !Sub "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${LambdaFunction.Arn}/invocations"
# ApiGatewayModel:
# Type: AWS::ApiGateway::Model
# Properties:
# ContentType: "application/json"
# RestApiId: !Ref ApiGatewayRestApi
# Schema: {}
# ! Only for websocket in ApiGatewayV2 (currently)
# ApiGatewayModel:
# Type: AWS::ApiGatewayV2::Model
# Properties:
# Name: "ApiGatewayModel"
# ContentType: "application/json"
# ApiId: !Ref ApiGatewayRestApi
# Schema: {}
# ApiGatewayStage:
# Type: AWS::ApiGateway::Stage
# Properties:
# DeploymentId: !Ref ApiGatewayDeployment
# Description: Lambda API Stage v0
# RestApiId: !Ref ApiGatewayRestApi
# StageName: "v0"
ApiGatewayStage:
Type: AWS::ApiGatewayV2::Stage
Properties:
DeploymentId: !Ref ApiGatewayDeployment
Description: Lambda API Stage v0
ApiId: !Ref ApiGatewayRestApi
StageName: "v0"
# ApiGatewayDeployment:
# Type: AWS::ApiGateway::Deployment
# DependsOn: ApiGatewayMethod
# Properties:
# Description: Lambda API Deployment
# RestApiId: !Ref ApiGatewayRestApi
ApiGatewayDeployment:
Type: AWS::ApiGatewayV2::Deployment
DependsOn: ApiGatewayMethod
Properties:
Description: Lambda API Deployment
ApiId: !Ref ApiGatewayRestApi
ApiGatewayIamRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Sid: ""
Effect: "Allow"
Principal:
Service:
- "apigateway.amazonaws.com"
Action:
- "sts:AssumeRole"
Path: "/"
Policies:
- PolicyName: LambdaAccess
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Action: "lambda:*"
Resource: !GetAtt LambdaFunction.Arn
LambdaFunction:
Type: AWS::Lambda::Function
Properties:
Code:
ZipFile: |
def handler(event, context):
response = {
'isBase64Encoded': False,
'statusCode': 200,
'headers': {},
'multiValueHeaders': {},
'body': 'Hello, World!'
}
return response
Description: AWS Lambda function
FunctionName: "lambda-function"
Handler: index.handler
MemorySize: 256
Role: !GetAtt LambdaIamRole.Arn
Runtime: python3.7
Timeout: 60
LambdaIamRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Principal:
Service:
- "lambda.amazonaws.com"
Action:
- "sts:AssumeRole"
Path: "/"
# ApiGatewayResource:
# Type: AWS::ApiGateway::Resource
# Properties:
# ParentId: !GetAtt ApiGatewayRestApi.RootResourceId
# PathPart: "lambda"
# RestApiId: !Ref ApiGatewayRestApi
ApiGatewayResource:
Type: AWS::ApiGatewayV2::Route
DependsOn:
- ApiGatewayRestApi
- LambdaFunction
- ApiGatewayMethod
Properties:
ApiId: !Ref ApiGatewayRestApi
RouteKey: POST /lambda
Target: !Join
- /
- - integrations
- !Ref ApiGatewayMethod
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment