[ad_1]
To attach an API Gateway to a Lambda operate utilizing CloudFormation, you may observe these steps:
- Outline your API Gateway and Lambda operate sources in your CloudFormation template. Right here’s an instance:
Sources:
MyLambdaFunction:
Sort: AWS::Lambda::Perform
Properties:
FunctionName: MyLambdaFunction
Runtime: python3.8
Handler: index.handler
Code:
S3Bucket: my-lambda-code-bucket
S3Key: lambda-code.zip
MyApiGateway:
Sort: AWS::ApiGateway::RestApi
Properties:
Identify: MyApiGateway
- Create a useful resource of sort
AWS::ApiGateway::Useful resource
to outline the useful resource path in your API Gateway:
MyApiGatewayResource:
Sort: AWS::ApiGateway::Useful resource
Properties:
RestApiId: !Ref MyApiGateway
ParentId: !GetAtt MyApiGateway.RootResourceId
PathPart: myresource
- Create a way on the useful resource to outline the HTTP technique (e.g., GET, POST) and integration particulars:
MyApiGatewayMethod:
Sort: AWS::ApiGateway::Methodology
Properties:
RestApiId: !Ref MyApiGateway
ResourceId: !Ref MyApiGatewayResource
HttpMethod: GET
AuthorizationType: NONE
Integration:
Sort: AWS
IntegrationHttpMethod: POST
Uri: !Sub arn:aws:apigateway:${AWS::Area}:lambda:path/2015-03-31/features/${MyLambdaFunction.Arn}/invocations
Within the above instance, the Uri
property references the ARN of the Lambda operate.
- Add a permission for API Gateway to invoke your Lambda operate:
MyLambdaPermission:
Sort: AWS::Lambda::Permission
Properties:
FunctionName: !Ref MyLambdaFunction
Motion: lambda:InvokeFunction
Principal: apigateway.amazonaws.com
SourceArn: !Sub arn:aws:execute-api:${AWS::Area}:${AWS::AccountId}:${MyApiGateway}/*/*/*
The SourceArn
property references the ARN of your API Gateway.
- 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 sources, 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 Lambda operate. Requests made to the API Gateway endpoint can be routed to your Lambda operate for processing.
[ad_2]