0
votes

I'm trying to dynamically generate SNS subscriptions in CDK based on what we have in mappings. What's the best way to do this here? I have mappings that essentially maps the SNS topic ARNs my queue want to subscribe to in each region/stage. The mapping looks something like this:

"Mappings":
  "SomeArnMap":
    "eu-west-1":
      "beta":
      - "arn:aws:sns:us-west-2:0123456789:topic1"
      "gamma":
      - "arn:aws:sns:us-west-2:0123456789:topic2"
      - "arn:aws:sns:us-west-2:0123456789:topic3"

How do I write code in CDK that creates a subscription for each element in the list here? I can't get regular loop to work because we don't know the size of the list until deployment. After CDK synth, it would just give me tokens like #{Token[TOKEN.264]} for my topic ARN.

Is it even doable in CDK/CloudFormation? Thanks.

1

1 Answers

0
votes

Since tokens aren't resolved during the runtime of aws-cdk code, usually you can use cfn intrinsic functions which declare some sort operation on the token in your template. These are accessible in @aws-cdk/core.fn. However, cfn doesn't have intrinsics for looping over values, only selecting values from a list/map.

If your cdk has these mappings in its output template and you just want to extract a value for reference when building another construct Fn.findInMap I believe should do that.

const importedTopic = Sns.Topic.fromTopicArn(this, "ImportedTopicId", Fn.findInMap("SomeArnMap", "eu-west-1", "beta"));
importedTopic.addSubscription(SomeSqsQueueOrSomething);