Simon Taylor
Simon Taylor

Reputation: 617

Cloudformation template - how do i get the value of a computed property

In the following Cloudformation template i am creating an SES receipt rule. In it i calculate the SNS TopicArn to be notified. I want to output the value but i cant hit on the syntax to get at it. All other outputs are template parameters.

ANSWER :- setting this on the calling template and passing it in as parameter:-

Resources:
  ReceiptRule:
    Type: 'AWS::SES::ReceiptRule'
    Properties:
      RuleSetName: !Ref ReceiptRuleSetName
      Rule:
        Name: !Ref RuleName
        Enabled: !Ref RuleEnabled
        ScanEnabled: !Ref RuleScanEnabled
        TlsPolicy: !Ref RuleTLSPolicy
        Recipients:
          - !Ref RuleRecipients
        Actions:
          - S3Action:
              ObjectKeyPrefix: !Ref RuleS3ActionObjectKeyPrefix
              BucketName: !Ref RuleS3ActionObjectBucketName
              TopicArn: !Join [ '',
               [
               !Sub 'arn:aws:sns:${AWS::Region}:${AWS::AccountId}:',
               !Ref RuleS3ActionObjectSNSTopic
               ]
               ]
Outputs:
  Recipients:
    Value: !Sub ${RuleRecipients}
  S3Bucket:
    Value: !Sub ${RuleS3ActionObjectBucketName}
  S3Prefix:
    Value: !Sub ${RuleS3ActionObjectKeyPrefix}
  SNSTopicArn:
    Value: >--What do i put here<--

ANSWER:-

RuleS3ActionObjectSNSTopic: !Join [ '',
               [
               !Sub 'arn:aws:sns:${AWS::Region}:${AWS::AccountId}:',
               !FindInMap [ SourceMap, !Ref rr5 , snstopic ]
               ]
               ]

Template then becomes

Resources:
      ReceiptRule:
        Type: 'AWS::SES::ReceiptRule'
        Properties:
          RuleSetName: !Ref ReceiptRuleSetName
          Rule:
            Name: !Ref RuleName
            Enabled: !Ref RuleEnabled
            ScanEnabled: !Ref RuleScanEnabled
            TlsPolicy: !Ref RuleTLSPolicy
            Recipients:
              - !Ref RuleRecipients
            Actions:
              - S3Action:
                  ObjectKeyPrefix: !Ref RuleS3ActionObjectKeyPrefix
                  BucketName: !Ref RuleS3ActionObjectBucketName
                  TopicArn: !Ref RuleS3ActionObjectSNSTopic
    Outputs:
      Recipients:
        Value: !Sub ${RuleRecipients}
      S3Bucket:
        Value: !Sub ${RuleS3ActionObjectBucketName}
      S3Prefix:
        Value: !Sub ${RuleS3ActionObjectKeyPrefix}
      SNSTopicArn:
        Value: !Sub ${RuleS3ActionObjectSNSTopic}

Upvotes: 0

Views: 894

Answers (1)

Laurent Jalbert Simard
Laurent Jalbert Simard

Reputation: 6339

Considering that the topic is passed as a template parameter you could either:

  1. Accept a SNSTopicArn as parameter instead of having just the asking for the topic name

  2. You can build the ARN in the Outputs section like you did in the ReceiptRule. Well, not exactly, because there's a better way. In fact you were almost there. Here's what it could look like:

--

Outputs:
  SNSTopicArn:
    Value: !Sub "arn:aws:sns:${AWS::Region}:${AWS::AccountId}:${RuleS3ActionObjectSNSTopic}"

--

Note that using the intrinsic function Fn::GetAtt wouldn't have worked since the SNS resource wasn't created in the same template.

Upvotes: 1

Related Questions