To link an API Entrance to an inline Lambda function utilizing CloudFormation, you can follow these actions:
- Specify your API Entrance and Lambda function resources in your CloudFormation design template. Here’s an example:
Resources:
MyApiGateway:
Type: AWS:: ApiGateway:: RestApi.
Characteristic:.
Call: MyApiGateway.
MyApiGatewayResource:.
Type: AWS:: ApiGateway:: Resource.
Characteristic:.
RestApiId:! Ref MyApiGateway.
ParentId:! GetAtt MyApiGateway.RootResourceId.
PathPart: myresource.
MyApiGatewayMethod:.
Type: AWS:: ApiGateway:: Approach.
Characteristic:.
RestApiId:! Ref MyApiGateway.
ResourceId:! Ref MyApiGatewayResource.
HttpMethod: GET.
AuthorizationType: NONE.
Combination:.
Type: AWS_PROXY.
IntegrationHttpMethod: POST.
Uri:! Sub "arn: aws: apigateway:$ {AWS:: Area}: lambda: path/2015 -03 -31/ functions/arn: aws: lambda:$ {AWS:: Area}:$ {AWS:: AccountId}: function: MyLambdaFunction/invocations".
MyApiGatewayDeployment:.
Type: AWS:: ApiGateway:: Release.
Characteristic:.
RestApiId:! Ref MyApiGateway.
MyApiGatewayStage:.
Type: AWS:: ApiGateway:: Phase.
Characteristic:.
StageName: prod.
RestApiId:! Ref MyApiGateway.
DeploymentId:! Ref MyApiGatewayDeployment.
MyLambdaFunction:.
Type: AWS:: Lambda:: Function.
Characteristic:.
FunctionName: MyLambdaFunction.
Runtime: python3.8.
Handler: index.lambda _ handler.
Code:.
ZipFile: |
def lambda_handler( occasion, context):.
return {
' statusCode': 200,.
' body': 'Hey there from Lambda!'.
}
In the above example, the Lambda function is specified inline utilizing the ZipFile
home. The code inside the lambda_handler
function can be tailored according to your requirements.
- Include the needed authorization for API Entrance to conjure up the Lambda function:
MyLambdaPermission:.
Type: AWS:: Lambda:: Consent.
Characteristic:.
FunctionName:! GetAtt MyLambdaFunction.Arn.
Action: lambda: InvokeFunction.
Principal: apigateway.amazonaws.com.
SourceArn:! Sub "arn: aws: execute-api:$ {AWS:: Area}:$ {AWS:: AccountId}:$ {MyApiGateway}/ */ */ *".
- Release your API Entrance:
MyApiGatewayDeployment:.
Type: AWS:: ApiGateway:: Release.
Characteristic:.
RestApiId:! Ref MyApiGateway.
- Partner the release with a phase (e.g., “prod”):
MyApiGatewayStage:.
Type: AWS:: ApiGateway:: Phase.
Characteristic:.
StageName: prod.
RestApiId:! Ref MyApiGateway.
DeploymentId:! Ref MyApiGatewayDeployment.
- After specifying these resources, you can release your CloudFormation stack utilizing the AWS CloudFormation service or the AWS CLI.
These actions will produce an API Entrance that is linked to your inline Lambda function. Demands made to the API Entrance endpoint will be routed to your Lambda function for processing.