To attach an API Gateway to an inline Lambda perform utilizing CloudFormation, you may comply with these steps:
- Outline your API Gateway and Lambda perform assets in your CloudFormation template. Right here’s an instance:
Sources:
MyApiGateway:
Sort: AWS::ApiGateway::RestApi
Properties:
Identify: MyApiGateway
MyApiGatewayResource:
Sort: AWS::ApiGateway::Useful resource
Properties:
RestApiId: !Ref MyApiGateway
ParentId: !GetAtt MyApiGateway.RootResourceId
PathPart: myresource
MyApiGatewayMethod:
Sort: AWS::ApiGateway::Methodology
Properties:
RestApiId: !Ref MyApiGateway
ResourceId: !Ref MyApiGatewayResource
HttpMethod: GET
AuthorizationType: NONE
Integration:
Sort: AWS_PROXY
IntegrationHttpMethod: POST
Uri: !Sub "arn:aws:apigateway:${AWS::Area}:lambda:path/2015-03-31/capabilities/arn:aws:lambda:${AWS::Area}:${AWS::AccountId}:perform:MyLambdaFunction/invocations"
MyApiGatewayDeployment:
Sort: AWS::ApiGateway::Deployment
Properties:
RestApiId: !Ref MyApiGateway
MyApiGatewayStage:
Sort: AWS::ApiGateway::Stage
Properties:
StageName: prod
RestApiId: !Ref MyApiGateway
DeploymentId: !Ref MyApiGatewayDeployment
MyLambdaFunction:
Sort: AWS::Lambda::Perform
Properties:
FunctionName: MyLambdaFunction
Runtime: python3.8
Handler: index.lambda_handler
Code:
ZipFile: |
def lambda_handler(occasion, context):
return {
'statusCode': 200,
'physique': 'Hey from Lambda!'
}
Within the above instance, the Lambda perform is outlined inline utilizing the ZipFile
property. The code contained in the lambda_handler
perform will be personalized in accordance with your necessities.
- Add the mandatory permission for API Gateway to invoke the Lambda perform:
MyLambdaPermission:
Sort: AWS::Lambda::Permission
Properties:
FunctionName: !GetAtt MyLambdaFunction.Arn
Motion: lambda:InvokeFunction
Principal: apigateway.amazonaws.com
SourceArn: !Sub "arn:aws:execute-api:${AWS::Area}:${AWS::AccountId}:${MyApiGateway}/*/*/*"
- Deploy your API Gateway:
MyApiGatewayDeployment:
Sort: AWS::ApiGateway::Deployment
Properties:
RestApiId: !Ref MyApiGateway
- Affiliate the deployment with a stage (e.g., “prod”):
MyApiGatewayStage:
Sort: AWS::ApiGateway::Stage
Properties:
StageName: prod
RestApiId: !Ref MyApiGateway
DeploymentId: !Ref MyApiGatewayDeployment
- After defining these assets, you may deploy your CloudFormation stack utilizing the AWS CloudFormation service or the AWS CLI.
These steps will create an API Gateway that’s linked to your inline Lambda perform. Requests made to the API Gateway endpoint can be routed to your Lambda perform for processing.