首页 > 解决方案 > 在 AWS SAM 模板中出现 Fn::GetAtt 错误

问题描述

我已经在我的 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-cloudformationaws-samaws-cloudformation-custom-resource

解决方案


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

查看有关返回值的文档。

尝试

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

推荐阅读