skip to Main Content

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

2

Answers


  1. 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.
    Login or Signup to reply.
  2. Besides the already mentioned methods, you can use CodePipeline with CodeBuild.
    Use the AWS CDK to dynamically (using for instanve standard python code) create the CF template in CodeBuild and then pass it to a CloudFormation deploy stage which deploys the previous generated template.

    https://aws.amazon.com/cdk/

    Secondly using StackSets is an option if you want to deploy one template in multiple regions, accouts or organization OU level from one central place!

    https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/what-is-cfnstacksets.html

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search