1

我已经在我的 AWS 无服务器应用程序模型模板中声明了 SNS 主题和订阅,如下所示:-

MyTopic:
    Type: AWS::SNS::Topic
    Properties:
      DisplayName: !Sub 'test-${Environment}-${AWS::AccountId}-${AWS::Region}'
      Tags:
       - Key: Environment
         Value: !FindInMap [Environment, FullForm, !Ref Environment]
      TopicName: !Sub 'test-${Environment}-${AWS::AccountId}-${AWS::Region}'

  MySubscription:
    Type: AWS::SNS::Subscription
    Properties:
      Endpoint: !Ref SubscriptionEndPoint
      Protocol: !Ref SubscriptionProtocol
      Region: !Ref 'AWS::Region'
      TopicArn: !Ref MyTopic

然后在我的 Lambda 函数环境中使用 SNS 主题 ARN,如下所示在同一个模板文件中:-

MyLambda:
    Type: AWS::Serverless::Function
    Properties:
      Environment:
        Variables:
          RUNTIME_SNS_TOPIC_ARN: !GetAtt MyTopic.Arn

输出(在 SAM 模板中):-

MyTopic:
    Description: SNS Topic for the Ingest to send notification to
    Export:
      Name: !Sub
        - ${ExportPrefix_}:${AWS::Region}:MyTopic
        - ExportPrefix_: !If
          - HasExportPrefix
          - !Join ['-', [!Ref ExportPrefix, !Ref Environment]]
          - !Join ['-', [!Select [0, !Split ["-", !Ref "AWS::StackName"]], !Ref Environment]]
      Value: !Sub "${MyTopic.Arn}:${MyTopic.Version.Version}"
  MySubscription:
    Description: Subscription to get messages from a topic
    Export:
      Name: !Sub
        - ${ExportPrefix_}:${AWS::Region}:MySubscription
        - ExportPrefix_: !If
          - HasExportPrefix
          - !Join ['-', [!Ref ExportPrefix, !Ref Environment]]
          - !Join ['-', [!Select [0, !Split ["-", !Ref "AWS::StackName"]], !Ref Environment]]
      Value: !Sub "${MySubscription.Arn}:${MySubscription.Version.Version}"

但是,我收到以下错误:-

13:30:30 错误:无法为堆栈创建变更集:my-stack,例如:Waiter ChangeSetCreateComplete 失败:Waiter 遇到终端故障状态状态:FAILED。原因:模板错误:资源 MyTopic 不支持 Fn::GetAtt 中的属性类型 Arn

4

2 回答 2

2

AnAWS::SNS:Topic在您使用时返回 ARNRef

查看有关返回值的文档。

尝试

MyLambda:
    Type: AWS::Serverless::Function
    Properties:
      Environment:
        Variables:
          RUNTIME_SNS_TOPIC_ARN: !Ref MyTopic  # Using !Ref
于 2020-09-21T14:47:28.883 回答
1

建议在 VSCode 中尝试CloudFormation Linter在创作模板时内联查看其中一些错误:

Visual Studio 代码屏幕截图

Visual Studio 代码屏幕截图

Visual Studio 代码屏幕截图

[cfn-lint] E1010: Invalid GetAtt MyTopic.Arn for resource MyLambda
[cfn-lint] E1019: Parameter MyTopic.Arn for Fn::Sub not found at Outputs/MyTopic/Value/Fn::Sub
[cfn-lint] E1019: Parameter MyTopic.Version.Version for Fn::Sub not found at Outputs/MyTopic/Value/Fn::Sub
[cfn-lint] E1019: Parameter MySubscription.Arn for Fn::Sub not found at Outputs/MySubscription/Value/Fn::Sub
[cfn-lint] E1019: Parameter MySubscription.Version.Version for Fn::Sub not found at Outputs/MySubscription/Value/Fn::Sub

它还会指出Value需要在Outputs


AWS::SNS::Topic返回值

AWS::SNS::Subscription

于 2020-09-21T17:54:22.510 回答