Jiew Meng
Jiew Meng

Reputation: 88317

How to get the auto generated RestApi from my AWS SAM template? To use in another SAM template

I used AWS SAM to generate my Lambda/APIs. But I want to be able to get this RestApi so I can use it in another SAM template.

The idea is to have 1 base infra CloudFormation/SAM template that creates the network, ALB, API Gateway things

Then each "micro-service" will have its own SAM template and it will create API endpoints referencing this "root" RestApi by specifying the RestApiId attribute

Is this a correct approach? Wonder if when I deploy each service, will it remove the APIs for the other services?

Upvotes: 2

Views: 524

Answers (1)

Shinji Matsumoto
Shinji Matsumoto

Reputation: 193

You can access default auto generated RestApi as ServerlessRestApi. This is logical resource id for auto generated RestApi resource.

ServerlessRestApi access example in template.yaml is as follows.

Outputs:
  ApiRootURL:
    Description: API Root URL
    Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/${ServerlessRestApi.Stage}"

You can see ServerlessRestApi in the resource list of you CloudFormation stack. ServerlessRestApi is not documented, so it might be changed in the future version.

Upvotes: 1

Related Questions