问题
I'm using SAM to deploy a Lambda function and make it callable over HTTP with via API Gateway using roughly this template snipplet:
MyFunction:
Type: AWS::Serverless::Function
Properties:
…
Events:
MyApi:
Type: Api
Properties:
Path: /
Method: any
This works, but it creates an API stage called "Prod", which has to be used as a prefix for all URLs. I don't want my URLs to be "https://something/Prod/foo", I want them to be "https://something/v1/foo", i.e. something that I choose.
How do I change the name of the stage?
I have tried declaring the API as a separate resource, and used the StageName
property to set the name of the stage, however, that requires me to also set DefinitionBody
, and that seems to be a deep rabbit hole.
MyFunction:
Type: AWS::Serverless::Function
Properties:
…
Events:
MyApi:
Type: Api
Properties:
Path: /
Method: any
RestApiId: !Ref MyApi
MyApi:
Type: AWS::Serverless::Api
Properties:
StageName: v1
DefinitionBody:
???
I know that ???
in the example above is supposed to be Swagger, but I would prefer to not have to write anything there, the template is getting verbose enough at is. Since I don't have to write this part if I can just live with the stage name "Prod" it seems to me that there must be a way to avoid having to write anything there and just set the stage name.
How can I change the name of the stage without having to write a lot of template code and/or Swagger?
回答1:
SAM Version 1.7.0 removed the need to specify a DefinitionBody or DefinitionUri so you should now be able to do exactly what you mention on your second example without having to include a DefinitionBody:
MyFunction:
Type: AWS::Serverless::Function
Properties:
…
Events:
MyApi:
Type: Api
Properties:
Path: /
Method: any
RestApiId: !Ref MyApi
MyApi:
Type: AWS::Serverless::Api
Properties:
StageName: v1
来源:https://stackoverflow.com/questions/48196643/how-can-i-change-the-name-of-the-api-stage-in-a-sam-template