Created
January 23, 2021 14:28
-
-
Save toddlers/5e584caa71d7c7d1a5b78b105f9bbe04 to your computer and use it in GitHub Desktop.
sample api gateway cfn template
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: My API Gateway and Lambda function | |
Parameters: | |
apiGatewayName: | |
Type: String | |
Default: my-api | |
apiGatewayStageName: | |
Type: String | |
AllowedPattern: "[a-z0-9]+" | |
Default: call | |
apiGatewayHTTPMethod: | |
Type: String | |
Default: POST | |
lambdaFunctionName: | |
Type: String | |
AllowedPattern: "[a-zA-Z0-9]+[a-zA-Z0-9-]+[a-zA-Z0-9]+" | |
Default: my-function | |
Resources: | |
apiGateway: | |
Type: AWS::ApiGateway::RestApi | |
Properties: | |
Description: Example API Gateway | |
EndpointConfiguration: | |
Types: | |
- REGIONAL | |
Name: !Ref apiGatewayName | |
apiGatewayRootMethod: | |
Type: AWS::ApiGateway::Method | |
Properties: | |
AuthorizationType: NONE | |
HttpMethod: !Ref apiGatewayHTTPMethod | |
Integration: | |
IntegrationHttpMethod: POST | |
Type: AWS_PROXY | |
Uri: !Sub | |
- arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${lambdaArn}/invocations | |
- lambdaArn: !GetAtt lambdaFunction.Arn | |
ResourceId: !GetAtt apiGateway.RootResourceId | |
RestApiId: !Ref apiGateway | |
lambdaFunction: | |
Type: AWS::Lambda::Function | |
Properties: | |
Code: | |
ZipFile: | | |
def handler(event,context): | |
return { | |
'body': 'Hello there {0}'.format(event['requestContext']['identity']['sourceIp']), | |
'headers': { | |
'Content-Type': 'text/plain' | |
}, | |
'statusCode': 200 | |
} | |
Description: Example Lambda function | |
FunctionName: !Ref lambdaFunctionName | |
Handler: index.handler | |
MemorySize: 128 | |
Role: !GetAtt lambdaIAMRole.Arn | |
Runtime: python3.8 | |
lambdaApiGatewayInvoke: | |
Type: AWS::Lambda::Permission | |
Properties: | |
Action: lambda:InvokeFunction | |
FunctionName: !GetAtt lambdaFunction.Arn | |
Principal: apigateway.amazonaws.com | |
# note: if route *not* at API Gateway root, `SourceArn` would take the form of: | |
# arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${apiGateway}/${apiGatewayStageName}/${apiGatewayHTTPMethod}/PATH_PART | |
SourceArn: !Sub arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${apiGateway}/${apiGatewayStageName}/${apiGatewayHTTPMethod}/ | |
lambdaIAMRole: | |
Type: AWS::IAM::Role | |
Properties: | |
AssumeRolePolicyDocument: | |
Version: 2012-10-17 | |
Statement: | |
- Action: | |
- sts:AssumeRole | |
Effect: Allow | |
Principal: | |
Service: | |
- lambda.amazonaws.com | |
Policies: | |
- 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}:* | |
PolicyName: lambda | |
lambdaLogGroup: | |
Type: AWS::Logs::LogGroup | |
Properties: | |
LogGroupName: !Sub /aws/lambda/${lambdaFunctionName} | |
RetentionInDays: 90 | |
apiGatewayDeployment: | |
Type: AWS::ApiGateway::Deployment | |
DependsOn: | |
- apiGatewayRootMethod | |
Properties: | |
RestApiId: !Ref apiGateway | |
ApiGatewayStage: | |
Type: 'AWS::ApiGateway::Stage' | |
Properties: | |
StageName: !Ref apiGatewayStageName | |
DeploymentId: !Ref apiGatewayDeployment | |
RestApiId: | |
Ref: apiGateway | |
MethodSettings: | |
- | |
ResourcePath: / | |
HttpMethod: 'POST' | |
LoggingLevel: INFO | |
DataTraceEnabled: true | |
MetricsEnabled: true | |
Outputs: | |
apiGatewayInvokeURL: | |
Value: !Sub https://${apiGateway}.execute-api.${AWS::Region}.amazonaws.com/${apiGatewayStageName} | |
lambdaArn: | |
Value: !GetAtt lambdaFunction.Arn |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment