AWS SAM 模板中出现 Fn::GetAtt 错误

vin*_*827 2 aws-cloudformation aws-sam aws-cloudformation-custom-resource

我已在我的 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
Run Code Online (Sandbox Code Playgroud)

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

MyLambda:
    Type: AWS::Serverless::Function
    Properties:
      Environment:
        Variables:
          RUNTIME_SNS_TOPIC_ARN: !GetAtt MyTopic.Arn
Run Code Online (Sandbox Code Playgroud)

输出(在 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}"
Run Code Online (Sandbox Code Playgroud)

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

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

tkw*_*rgs 5

当您使用时, AnAWS::SNS:Topic返回 ARNRef

查看有关返回值的文档。

尝试用

MyLambda:
    Type: AWS::Serverless::Function
    Properties:
      Environment:
        Variables:
          RUNTIME_SNS_TOPIC_ARN: !Ref MyTopic  # Using !Ref
Run Code Online (Sandbox Code Playgroud)