How to link an API Entrance to Lambda in CloudFormation

To link an API Entrance to a Lambda function utilizing CloudFormation, you can follow these actions:

  1. Specify your API Entrance and Lambda function resources in your CloudFormation design template. Here’s an example:
 Resources:
MyLambdaFunction:
Type: AWS:: Lambda:: Function.
Residence:.
FunctionName: MyLambdaFunction.
Runtime: python3.8.
Handler: index.handler.
Code:.
S3Bucket: my-lambda-code-bucket.
S3Key: lambda-code. zip.

MyApiGateway:.
Type: AWS:: ApiGateway:: RestApi.
Residence:.
Call: MyApiGateway.
  1. Produce a resource of type AWS:: ApiGateway:: Resource to specify the resource course for your API Entrance:
 MyApiGatewayResource:.
Type: AWS:: ApiGateway:: Resource.
Residence:.
RestApiId:! Ref MyApiGateway.
ParentId:! GetAtt MyApiGateway.RootResourceId.
PathPart: myresource.
  1. Produce an approach on the resource to specify the HTTP technique (e.g., GET, POST) and combination information:
 MyApiGatewayMethod:.
Type: AWS:: ApiGateway:: Technique.
Residence:.
RestApiId:! Ref MyApiGateway.
ResourceId:! Ref MyApiGatewayResource.
HttpMethod: GET.
AuthorizationType: NONE.
Combination:.
Type: AWS.
IntegrationHttpMethod: POST.
Uri:! Sub arn: aws: apigateway:$ {AWS:: Area}: lambda: path/2015 -03 -31/ functions/$ {MyLambdaFunction.Arn}/ invocations.

In the above example, the Uri residential or commercial property recommendations the ARN of the Lambda function.

  1. Include an approval for API Entrance to invoke your Lambda function:
 MyLambdaPermission:.
Type: AWS:: Lambda:: Consent.
Residence:.
FunctionName:! Ref MyLambdaFunction.
Action: lambda: InvokeFunction.
Principal: apigateway.amazonaws.com.
SourceArn:! Sub arn: aws: execute-api:$ {AWS:: Area}:$ {AWS:: AccountId}:$ {MyApiGateway}/ */ */ *.

The SourceArn residential or commercial property recommendations the ARN of your API Entrance.

  1. Release your API Entrance:
 MyApiGatewayDeployment:.
Type: AWS:: ApiGateway:: Implementation.
Residence:.
RestApiId:! Ref MyApiGateway.
  1. Partner the release with a phase (e.g., “prod”):
 MyApiGatewayStage:.
Type: AWS:: ApiGateway:: Phase.
Residence:.
StageName: prod.
RestApiId:! Ref MyApiGateway.
DeploymentId:! Ref MyApiGatewayDeployment.
  1. After specifying these resources, you can release your CloudFormation stack utilizing the AWS CloudFormation service or the AWS CLI.

These actions will develop an API Entrance that is linked to your Lambda function. Demands made to the API Entrance endpoint will be routed to your Lambda function for processing.

Like this post? Please share to your friends:
Leave a Reply

;-) :| :x :twisted: :smile: :shock: :sad: :roll: :razz: :oops: :o :mrgreen: :lol: :idea: :grin: :evil: :cry: :cool: :arrow: :???: :?: :!: