代码之家  ›  专栏  ›  技术社区  ›  Tomas Aschan

获取对在CloudFormation中触发AWS::Serverless::函数的Api的引用

  •  0
  • Tomas Aschan  · 技术社区  · 9 年前

    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 模板部分:

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

    我试过了 !GetAtt MyFunction.RootResourceId here ,但当我尝试部署堆栈时,这导致了失败:

    未能创建变更集:服务员变更集创建完成失败:服务员遇到终端故障状态:失败。原因:模板资源属性“MyGatewayId”无效

    2 回复  |  直到 9 年前
        1
  •  4
  •   Jamie Starke    9 年前

    如果您真的想输出这方面的关键是了解无服务器转换为您做了什么,根据您的规范生成一系列资源。

    您可以检查您的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网关Rest API,并为其提供 ServerlessRestApi Outputs

    Output:
      MyGatewayId:
        Description: Id of the auto-generated API Gateway resource
        Value: !Ref ServerlessRestApi
    
        2
  •  1
  •   Pelto    9 年前

    如果在模板中指定API网关,则可以将其作为输出。

    Resources:
      MyAPI
        Type AWS::Serverless::Api
        Properties:
          DefinitionUri: s3://<bucket>/swagger.yaml
    

    通过这种方法,您可以在输出中使用资源。但是,这也要求您使用招摇,因为 DefinitionUri 是必需属性。

    aws cloudformation describe-stack-resources --stack-name <your-stack> \
        --query "StackResources[?ResourceType == 'AWS::ApiGateway::RestApi'].PhysicalResourceId" \
        --output text
    

    aws cloudformation describe-stack-resources --stack-name <your-stack> \
        --query "StackResources[?ResourceType == 'AWS::ApiGateway::RestApi'].PhysicalResourceId" \
        --output text \
        | awk '{print "https://"$1".execute-api.eu-west-1.amazonaws.com/Prod"}'