【问题标题】:Getting Fn::GetAtt error in the AWS SAM template在 AWS SAM 模板中出现 Fn::GetAtt 错误
【发布时间】:2021-01-07 15:30:05
【问题描述】:

我已经在我的 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

【问题讨论】:

    标签: amazon-cloudformation aws-sam aws-cloudformation-custom-resource


    【解决方案1】:

    当您使用 Ref 时,AWS::SNS:Topic 返回 ARN

    查看返回值上的Docs

    试试

    MyLambda:
        Type: AWS::Serverless::Function
        Properties:
          Environment:
            Variables:
              RUNTIME_SNS_TOPIC_ARN: !Ref MyTopic  # Using !Ref
    

    【讨论】:

    • 我收到此错误:- TopicArn 原因:ARN 必须至少有 6 个元素,而不是 1 个
    • 这是 Cloudformation 错误,还是您的 Lambda 函数中的错误?
    • 对不起。我的错。你的解决方案很好。我过度阅读了您的解决方案并引用了这样的主题!Ref MyTopic.Arn 而不仅仅是!Ref MyTopic。 .Arn 不起作用,错误是由此引起的。你的解决方案奏效了。谢谢你:)
    【解决方案2】:

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

    [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
    

    还要指出ValueOutputs中需要少缩进


    AWS::SNS::Topic return values

    AWS::SNS::Subscription

    【讨论】:

      猜你喜欢
      • 2018-05-09
      • 2019-04-26
      • 2022-01-14
      • 1970-01-01
      • 2019-09-20
      • 2017-06-10
      • 2019-03-18
      • 2021-04-02
      • 2020-06-13
      相关资源
      最近更新 更多