Skip to content

Instantly share code, notes, and snippets.

@calvernaz
Created September 12, 2023 08:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save calvernaz/d8352e1c73e1b89f380263e411fd20a5 to your computer and use it in GitHub Desktop.
Save calvernaz/d8352e1c73e1b89f380263e411fd20a5 to your computer and use it in GitHub Desktop.
sam apigateway v2
# 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::ApiGatewayV2::Api
Properties:
ProtocolType: HTTP
Description: An API Gateway with a Lambda Integration
Name: lambda-api
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"
ApiGatewayStage:
Type: AWS::ApiGatewayV2::Stage
Properties:
DeploymentId: !Ref ApiGatewayDeployment
Description: Lambda API Stage v0
ApiId: !Ref ApiGatewayRestApi
StageName: "v0"
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::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