如何导出 SNS 主题以在 Cloudformation 中的不同堆栈中使用

How to export SNS topic to be used in different stacks in Cloudformation

在一个 Cloudformation 模板中,我创建了一个 SNS 主题并将其导出。请注意,您无法导出 SNS 主题的 Arn,因为该属性对 docs 上的 GetAtt 不可用。

BaseStack

Outputs:
  AlarmSNSTopic: 
    Description: Arn for SNS topic related to alarms
    Export:
      Name: AlarmSNSTopic
    Value: { "Fn::GetAtt": ["MyAlarmSNSTopic", "TopicName"] }

然后在另一个模板中,我尝试使用类似以下的内容来引用该导出:

函数堆栈 1

InputQueueNoMessages:
  Type: AWS::CloudWatch::Alarm
  Properties:
    AlarmDescription: Some Alarm
  ...
  AlarmActions:
    Fn::ImportValue: AlarmSNSTopic

当我这样做时,Cloudformation 告诉我它需要一个 ARN,而不是主题名称。

Invalid arn syntax: Blah-AlarmSNSTopic-random

这可能吗?我错过了什么吗?

只要做一个参考,它就会 return 主题 ARN。

Value: !Ref MyAlarmSNSTopic

Ref

For the AWS::SNS::Topic resource, the Ref intrinsic function returns the topic ARN, for example: arn:aws:sns:us-east-1:123456789012:mystack-mytopic-NZJ5JSMVGFIE.

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-sns-topic.html#w2ab2c21c10d983c11

AWS::CloudWatch::Alarm 需要 AlarmActions 的 ARN,但您导出了主题名称。您的输出值应该是 ARN。

Outputs:
  AlarmSNSTopic: 
    Description: Arn for SNS topic related to alarms
    Export:
      Name: AlarmSNSTopic
    Value: !Ref MyAlarmSNSTopic