首页 > 解决方案 > How to reuse CloudFormation CodeBlocks?

问题描述

I wonder if there is a way for me to reuse codeblock in CloudFormation without copy and paste. For example: right now I am making a bunch of alarms for different Redis clusters, the only real difference is the CacheClusterId (cache-001 then cache-002, cache-003 in this case) itself. I have looked up the instruction but I couldn't find a good way to not Copy and Paste. Or is it possible to have array of values instead of single value

    "CacheMemoryUsage001": {
      "Type": "AWS::CloudWatch::Alarm",
      "Properties": {
        "MetricName": "DatabaseMemoryUsagePercentage"
        ...
       }
        "Dimensions": [{
          "Name": "CacheClusterId",
          "Value": "cache-001"
        },
    ....

I would like to organize the template better, right now I have couple alarms type per cluster and they are getting messier to maintain and keep track of

标签: amazon-cloudformation

解决方案


There are general two ways for that:

  1. Use nested stacks. In this case, definition of a common resource(s) would be put to a separate stack, and in the main stack you would use the nested stack to create multiple resources based on the nested stack.
  2. Create CFN macro which would perform basic find-and-replace type of your template processing to create the copies of the resources.

推荐阅读