如何在SAM模板中更改API阶段的名称? [英] How can I change the name of the API stage in a SAM template?
问题描述
我正在使用SAM部署Lambda函数,并使用大致如下模板片段通过API Gateway通过HTTP对其进行调用:
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
这有效,但是它创建了一个称为"Prod"的API阶段,该阶段必须用作所有URL的前缀.我不希望自己的URL是" https://something/Prod/foo ",我希望它们是" https://something/v1/foo ",即我选择的东西.
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.
如何更改舞台名称?
我尝试将API声明为单独的资源,并使用 StageName
属性设置舞台的名称,但是,这要求我也设置 DefinitionBody
,那似乎是一个深深的兔子洞.
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:
???
我知道上面的示例中的 ???
应该是Swagger,但是我宁愿不必在其中编写任何内容,模板的详细程度已经很高.由于只要我可以使用阶段名称"Prod",就不必编写此部分,因此在我看来,必须有一种方法可以避免必须在此处编写任何内容并只需设置阶段名称.
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.
如何在无需编写大量模板代码和/或Swagger的情况下更改舞台的名称?
How can I change the name of the stage without having to write a lot of template code and/or Swagger?
推荐答案
SAM版本1.7.0不再需要指定DefinitionBody或DefinitionUri,因此您现在可以完全按照第二个示例的说明进行操作,而不必包括一个DefinitionBody:
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
这篇关于如何在SAM模板中更改API阶段的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!