Share API Gateway and API Resources
When using serverless framework, we are able to use shared api gatewate and resources to avoid creating multiple rest api for micro-services. Basically, we have another CF template to create a stack that create Rest API and export ID and root resources Id so that we can reference in other stacks. I reviewed SAM documentation but cannot find any reference. I would be great if SAM supports this feature.
Actually, the API event of the Lambda function supports the RestApiId property, but it must is a reference not a plaintext RestApiId. When I try with a hardcode, I got below error
Failed to create the changeset: Waiter ChangeSetCreateComplete failed: Waiter encountered a terminal failure state Status: FAILED. Reason: Transform AWS::Serverless-2016-10-31 failed with: Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [HelloWorldFunction] is invalid. Event with id [HelloWorld] is invalid. RestApiId must be a valid reference to an 'AWS::Serverless::Api' resource in same template.
We can either: Support global of the API to define the API gateway we will create sub-resources
Globals:
Function:
Timeout: 30
Api:
RestApiId: xxxxxxxxxx # REST API resource ID. Default is generated by the framework
RestApiRootResourceId: xxxxxxxxxx # Root resource, represent as / path
or support in the individual event
HelloWorldFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: hello-world/
Handler: index.handler
Runtime: nodejs8.10
Events:
HelloWorld:
Type: Api
Properties:
Path: /service-a/hello
RestApiId: xxxxxxxxxx # REST API resource ID. Default is generated by the framework
RestApiRootResourceId: xxxxxxxxxx # Root resource, represent as / path
Method: get
One more thing I wanted to note here is SAM does not support create explicitly API, SAM creates an API based on the events of the Lambda function, but for our root stack, we only want to create an API. We can use native CloudFormation resources, but if SAM supports that feature, it would be better, we can reduce the number of resources defined in the template.
Here is the screenshot that shows my setup

Reference: https://serverless.com/framework/docs/providers/aws/events/apigateway/
@hoang-innomizetech I believe this use case would be covered by #866. The key difference is that you would specify the API events within the same template as the API, rather than in the template containing the function. Can you take a look at that issue and confirm if it would be sufficient to solve this issue?
I read comments in #866 but cannot find out any idea to implement our requirement, currently, SAM does not provide any way to add resources to API Gateway created by another stack (or another serverless application). RestApiId is only allowed in the same template.
Correct; #866 hasn't been implemented yet so those features aren't available. Please +1 that issue to help prioritize it 👍
Hi! I was wondering if there is a way to get the ID of the default REST API generated by the framework when I integrate an HTTP event within a Lambda Function like this:
myFunction:
handler: example.handler
events:
- http:
path: execution
method: get
I need to obtain the API GW endpoint because I'm implementing a Callback Pattern in the AWS StepFunctions service. I imagine that obtaining at least its ID from the template will be enough. Can someone point me in the right direction? Thanks
Hi! I was wondering if there is a way to get the ID of the default REST API generated by the framework when I integrate an HTTP event within a Lambda Function like this:
myFunction: handler: example.handler events: - http: path: execution method: getI need to obtain the API GW endpoint because I'm implementing a Callback Pattern in the AWS StepFunctions service. I imagine that obtaining at least its ID from the template will be enough. Can someone point me in the right direction? Thanks
I've just found it in a comment here based on this. It would be great if this was better documented.
The ID of the default REST API generated by Serverless can be referenced by:
!Ref ApiGatewayRestApi
Hi! It would be really great if we could have this feature. Any updates on this?
Thank you!