|
# 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 |