Created
January 31, 2022 12:34
-
-
Save chiemerieezechukwu/c54f61c2f82fc0fd2c57fd33bb0a98cd to your computer and use it in GitHub Desktop.
CloudFormation ApiGateway and Lambda with PATH_PART
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AWSTemplateFormatVersion: '2010-09-09' | |
Description: Setup AWS Lambda integrated with ApiGateway | |
Parameters: | |
apiGatewayName: | |
Type: String | |
Default: test_rest_api | |
apiGatewayStageName: | |
Type: String | |
Default: test | |
lambdaFunctionName: | |
Type: String | |
Default: test_lambda | |
Resources: | |
testLambdaIAMRole: | |
Type: AWS::IAM::Role | |
Properties: | |
AssumeRolePolicyDocument: | |
Version: 2012-10-17 | |
Statement: | |
- Action: | |
- sts:AssumeRole | |
Effect: Allow | |
Principal: | |
Service: | |
- lambda.amazonaws.com | |
Policies: | |
- PolicyName: lambda | |
PolicyDocument: | |
Version: 2012-10-17 | |
Statement: | |
- Action: | |
- logs:CreateLogGroup | |
- logs:CreateLogStream | |
- logs:PutLogEvents | |
Effect: Allow | |
Resource: | |
- !Sub arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/${lambdaFunctionName}:* | |
testLambda: | |
Type: AWS::Lambda::Function | |
Properties: | |
FunctionName: !Ref lambdaFunctionName | |
Handler: index.lambda_handler | |
MemorySize: 128 | |
Role: !GetAtt testLambdaIAMRole.Arn | |
Runtime: python3.7 | |
Timeout: 3 | |
Code: | |
ZipFile: | | |
import json | |
def lambda_handler(event, context): | |
print("received this event: %s" % event) | |
return { | |
"headers": { | |
'Content-Type': "text/plain" | |
}, | |
"statusCode": 200, | |
"body": json.dumps(event) | |
} | |
testLambdaApiGatewayInvoke: | |
Type: AWS::Lambda::Permission | |
Properties: | |
Action: lambda:InvokeFunction | |
FunctionName: !GetAtt testLambda.Arn | |
Principal: apigateway.amazonaws.com | |
SourceArn: !Sub arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${testRestApi}/*/*/test_api_action | |
testRestApi: | |
Type: AWS::ApiGateway::RestApi | |
Properties: | |
EndpointConfiguration: | |
Types: | |
- EDGE | |
Name: !Ref apiGatewayName | |
testApiPOSTMethodResource: | |
Type: AWS::ApiGateway::Resource | |
Properties: | |
RestApiId: !Ref testRestApi | |
ParentId: !GetAtt testRestApi.RootResourceId | |
PathPart: test_api_action | |
testApiPOSTMethod: | |
Type: AWS::ApiGateway::Method | |
Properties: | |
ResourceId: !Ref testApiPOSTMethodResource | |
RestApiId: !Ref testRestApi | |
AuthorizationType: NONE | |
HttpMethod: POST | |
Integration: | |
IntegrationHttpMethod: POST | |
Type: AWS_PROXY | |
Uri: !Sub | |
- arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${lambdaArn}/invocations | |
- lambdaArn: !GetAtt testLambda.Arn | |
testApiDeployment: | |
Type: AWS::ApiGateway::Deployment | |
DependsOn: | |
- testApiPOSTMethod | |
Properties: | |
RestApiId: !Ref testRestApi | |
StageName: !Ref apiGatewayStageName |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment