I have two lambda functions .
Now I want to use one api for these two.
Then my code is like this
const api = new apigateway.RestApi(this, 'ServerlessRestApi', {
restApiName: `AWSCDKTest-${systemEnv}`,
cloudWatchRole: false
});
api.root.addMethod('GET', new apigateway.LambdaIntegration(helloLambda));
api.root.addMethod('GET', new apigateway.LambdaIntegration(happyLambda));
Howeber it says GET is doubled.
So I made two API
const api = new apigateway.RestApi(this, 'ServerlessRestApi_hello', {
restApiName: `AWSCDK-Viral-${systemEnv}`,
cloudWatchRole: false
});
api.root.addMethod('GET', new apigateway.LambdaIntegration(helloLambda));
const api2 = new apigateway.RestApi(this, 'ServerlessRestApi_happy', { cloudWatchRole: false });
api2.root.addMethod('GET', new apigateway.LambdaIntegration(happyLambda));
It works, but it makes two API.
What is the best practice to use one API for two lambda??
CodePudding user response:
API root:
GET "https://example_api_endpoint/" invokes helloLambda then inside this lambda function call the AWS-SDK lambda API method invoke() to trigger the execution of happyLambda within the first invocation in sequence. Otherwise, you cannot have two lambda functions for a single API resource. Further reading: AWS Lambda Fanout pattern.
CodePudding user response:
One lambda cannot be linked to exact same API path Http verb. Here are some alternatives -
a) have different api paths, each calling different lambda
b) have one lambda triggered by API gateway, which triggers other lambda that you need (within it's code)
c) have the lambda integration to the endpoint that sends a message to SNS, and have multiple lambda subscribe to the SNS via SQS.
