本文介绍了获取对在CloudFormation中触发AWS :: Serverless :: Function的Api的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在CloudFormation模板中,我定义了一个无服务器应用程序,该应用程序具有由API网关触发的lambda函数,如下所示:

In a CloudFormation template, I'm defining a serverless application with a lambda function triggered by an API gateway, as follows:

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,并且可以按照我的需要进行工作.

This generates an API Gateway resource that is set up to recieve GET requests and forward to my lambda, and it works just as I want it.

但是,我无法在模板的 Output 部分中弄清楚如何引用该API实例:

However, I can't figure out how to reference that API instance in the Output section of the template:

Output:
  MyGatewayId:
    Description: Id of the auto-generated API Gateway resource
    Value: # what do I put here?

我已经按照建议在这里尝试过!GetAtt MyFunction.RootResourceId 在尝试部署堆栈时失败:

I've tried !GetAtt MyFunction.RootResourceId as suggested here, but that resulted in a failure when I tried to deploy the stack:

推荐答案

如果您真的希望能够输出,那么关键是了解无服务器转换对您的作用,并根据您的规范生成一系列资源

If you really want to be able to output The key to this is understanding what the Serverless transform is doing for you, generating a series of resources based on your specification.

您可以确定自己的CloudFormations资源,但要根据您的规范

You can check your CloudFormations resources to be sure, but based on your spec

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,并将其逻辑ID设置为 ServerlessRestApi .因此,请回答关于输出

It should generate you a few resources. Based on your Events property, and the fact that you did not specify a RestApiId, it will generate a default API Gateway Rest API for you, and give it the Logical id of ServerlessRestApi. So to answer your original question regarding Outputs

Output:
  MyGatewayId:
    Description: Id of the auto-generated API Gateway resource
    Value: !Ref ServerlessRestApi

这篇关于获取对在CloudFormation中触发AWS :: Serverless :: Function的Api的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 07:01