【问题标题】:!ImportValue in Serverless Framework not working!无服务器框架中的 ImportValue 不起作用
【发布时间】:2018-02-10 17:09:39
【问题描述】:

我正在尝试从 CloudFormation 中创建的堆栈导出 DynamoDb StreamArn,然后在 serverless.yml 中使用 !ImportValue 引用导出。

但我收到此错误消息:

unknown tag !<!ImportValue> in "/codebuild/output/src/serverless.yml"

cloudformation 和 serverless.yml 定义如下。任何帮助表示赞赏。

StackA.yml

AWSTemplateFormatVersion: 2010-09-09
Description: Resources for the registration site

Resources:
  ClientTable:
    Type: AWS::DynamoDB::Table
    DeletionPolicy: Retain
    Properties:
      TableName: client
      AttributeDefinitions:
        - AttributeName: id
          AttributeType: S
      KeySchema:
        - AttributeName: id
          KeyType: HASH
      ProvisionedThroughput:
        ReadCapacityUnits: 2
        WriteCapacityUnits: 2
      StreamSpecification:
        StreamViewType: NEW_AND_OLD_IMAGES

Outputs:  
  ClientTableStreamArn:
      Description: The ARN for My ClientTable Stream
      Value: !GetAtt ClientTable.StreamArn
      Export:
        Name: my-client-table-stream-arn

serverless.yml

service: my-service

frameworkVersion: ">=1.1.0 <2.0.0"

provider:
  name: aws
  runtime: nodejs6.10
  iamRoleStatements:
    - Effect: Allow
      Action:
        - dynamodb:DescribeStream
        - dynamodb:GetRecords
        - dynamodb:GetShardIterator
        - dynamodb:ListStreams
        - dynamodb:GetItem
        - dynamodb:PutItem
      Resource: arn:aws:dynamodb:*:*:table/client

functions:

  foo:
    handler: foo.main
    events:
      - stream:
          type: dynamodb
          arn: !ImportValue my-client-table-stream-arn
          batchSize: 1

【问题讨论】:

    标签: amazon-cloudformation serverless-framework


    【解决方案1】:

    使用${cf:stackName.outputKey}解决

    【讨论】:

    • 您还可以使用以下方式引用无服务器 yaml 文件中的导出值:Fn::ImportValue: my-client-table-stream-arn
    • @Luke 在这种情况下 stackName 是什么?我在我的主 serverless.yml 中导入了资源文件(资源以无服务器 YAML 方式编写),在该文件中我有一些输出,我想在主 serverless.yml 中导入,但我总是得到“没有名为 myOutputKey 的导出成立”。我正在通过 Fn::ImportValue 尝试
    【解决方案2】:

    我也为此苦苦挣扎,对我有用的是:

    functions:
      foo:
        handler: foo.main
        events:
          - stream:
             type: dynamodb
             arn: 
              !ImportValue my-client-table-stream-arn
             batchSize: 1
    

    注意,内部函数 ImportValue 位于新行并缩进,否则在生成 cloudformation-template-update-stack.json 时会忽略整个 event

    【讨论】:

      【解决方案3】:

      您似乎在使用 !ImportValue CloudFormation YAML 的简写。我的理解是,当 CloudFormation 解析 YAML 时,!ImportValue 实际上是别名Fn::ImportValue。根据 Serverless Function 文档,它们似乎应该支持 Fn::ImportValue 形式的导入。

      根据Fn::ImportValue 的文档,您应该可以参考您的导出内容

        - stream:
            type: dynamodb
            arn: {"Fn::ImportValue": "my-client-table-stream-arn"}
            batchSize: 1
      

      希望能帮助您解决问题。

      【讨论】:

        猜你喜欢
        • 2021-02-10
        • 2021-05-06
        • 2019-12-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-28
        • 2019-02-21
        • 2020-11-27
        相关资源
        最近更新 更多