获取对触发 CloudFormation 中的 AWS::Serverless::Function 的 Api 的引用
Get a reference to the Api that triggers an AWS::Serverless::Function in CloudFormation
在 CloudFormation 模板中,我定义了一个具有由 API 网关触发的 lambda 函数的无服务器应用程序,如下所示:
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
MyFunction:
Type: AWS::Serverless::Function
Properties:
# ...
Events:
GetStuff:
Type: Api
Properties:
Path: /stuff
Method: get
这会生成一个 API 网关资源,该资源被设置为接收 GET
请求并转发到我的 lambda,它按我想要的方式工作。
但是,我不知道如何在模板的 Output
部分引用 API 实例:
Output:
MyGatewayId:
Description: Id of the auto-generated API Gateway resource
Value: # what do I put here?
我已经按照 here 的建议尝试了 !GetAtt MyFunction.RootResourceId
,但是当我尝试部署堆栈时失败了:
Failed to create the changeset: Waiter ChangeSetCreateComplete failed: Waiter encountered a terminal failure state Status: FAILED. Reason: Invalid template resource property 'MyGatewayId'
如果您在模板中指定 API 网关,则可以将其作为输出。
Resources:
MyAPI
Type AWS::Serverless::Api
Properties:
DefinitionUri: s3://<bucket>/swagger.yaml
通过这种方法,您可以在输出中使用资源。但是,这也需要您使用 swagger,因为 DefinitionUri
是必需的属性。
您始终可以使用以下命令提取 ID:
aws cloudformation describe-stack-resources --stack-name <your-stack> \
--query "StackResources[?ResourceType == 'AWS::ApiGateway::RestApi'].PhysicalResourceId" \
--output text
这意味着您还可以通过以下方式轻松地将 url 提取到您的 API:
aws cloudformation describe-stack-resources --stack-name <your-stack> \
--query "StackResources[?ResourceType == 'AWS::ApiGateway::RestApi'].PhysicalResourceId" \
--output text \
| awk '{print "https://"".execute-api.eu-west-1.amazonaws.com/Prod"}'
如果你真的希望能够输出关键是理解无服务器转换为你做了什么,根据你的规范生成一系列资源。
您可以检查您的 CloudFormations 资源以确保确定,但要根据您的规范
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
MyFunction:
Type: AWS::Serverless::Function
Properties:
# ...
Events:
GetStuff:
Type: Api
Properties:
Path: /stuff
Method: get
它应该会为您生成一些资源。根据您的 Events
属性,以及您没有指定 RestApiId 的事实,它将为您生成一个默认的 API Gateway Rest API,并且给它 ServerlessRestApi
的逻辑 ID。所以回答你原来关于 Outputs
的问题
Output:
MyGatewayId:
Description: Id of the auto-generated API Gateway resource
Value: !Ref ServerlessRestApi
在 CloudFormation 模板中,我定义了一个具有由 API 网关触发的 lambda 函数的无服务器应用程序,如下所示:
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
MyFunction:
Type: AWS::Serverless::Function
Properties:
# ...
Events:
GetStuff:
Type: Api
Properties:
Path: /stuff
Method: get
这会生成一个 API 网关资源,该资源被设置为接收 GET
请求并转发到我的 lambda,它按我想要的方式工作。
但是,我不知道如何在模板的 Output
部分引用 API 实例:
Output:
MyGatewayId:
Description: Id of the auto-generated API Gateway resource
Value: # what do I put here?
我已经按照 here 的建议尝试了 !GetAtt MyFunction.RootResourceId
,但是当我尝试部署堆栈时失败了:
Failed to create the changeset: Waiter ChangeSetCreateComplete failed: Waiter encountered a terminal failure state Status: FAILED. Reason: Invalid template resource property 'MyGatewayId'
如果您在模板中指定 API 网关,则可以将其作为输出。
Resources:
MyAPI
Type AWS::Serverless::Api
Properties:
DefinitionUri: s3://<bucket>/swagger.yaml
通过这种方法,您可以在输出中使用资源。但是,这也需要您使用 swagger,因为 DefinitionUri
是必需的属性。
您始终可以使用以下命令提取 ID:
aws cloudformation describe-stack-resources --stack-name <your-stack> \
--query "StackResources[?ResourceType == 'AWS::ApiGateway::RestApi'].PhysicalResourceId" \
--output text
这意味着您还可以通过以下方式轻松地将 url 提取到您的 API:
aws cloudformation describe-stack-resources --stack-name <your-stack> \
--query "StackResources[?ResourceType == 'AWS::ApiGateway::RestApi'].PhysicalResourceId" \
--output text \
| awk '{print "https://"".execute-api.eu-west-1.amazonaws.com/Prod"}'
如果你真的希望能够输出关键是理解无服务器转换为你做了什么,根据你的规范生成一系列资源。
您可以检查您的 CloudFormations 资源以确保确定,但要根据您的规范
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
MyFunction:
Type: AWS::Serverless::Function
Properties:
# ...
Events:
GetStuff:
Type: Api
Properties:
Path: /stuff
Method: get
它应该会为您生成一些资源。根据您的 Events
属性,以及您没有指定 RestApiId 的事实,它将为您生成一个默认的 API Gateway Rest API,并且给它 ServerlessRestApi
的逻辑 ID。所以回答你原来关于 Outputs
Output:
MyGatewayId:
Description: Id of the auto-generated API Gateway resource
Value: !Ref ServerlessRestApi