Matching ServiceName with a specified prefix

Viewed 59

I'm trying to create an alarm in AWS CloudWatch. There are two CloudFormation templates: one for creating the architecture (for which I have no access) and one for alarms. I want to match ServiceName which prefix is known to me but it has some random suffix. Both ServiceName and ClusterName are related to AWS ECS. Here is code snippet:

SomeAlarm:
  Type: 'AWS::CloudWatch::Alarm'
  Properties:
    ...
    Dimensions:
      - Name: ClusterName
        Value: <known cluster name>
      - Name: ServiceName
        Value: <known prefix><random suffix>

As I understand, I cannot use wildcard or regex in this case, or I don't know any way for achieving it. Also, I'm not able to change first template to export values and import them in second template. What syntax can I use to match ServiceName? Any thoughts?

1 Answers

Since you cannot change the first template, I think your best bet is to use a custom resource. You can go about this in two ways:

One, you just write the entire CloudWatch alarm in the custom resource. This means that you'll have to ensure that you implement all functionality for updating and deleting as well. This way you can use the AWS SDK to retrieve the correct values that you need.

The second way is that you use a custom resource, but only to retrieve the value. You can then pass the value along as a return value to your CloudFormation resource, and subsequently use the !GetAtt method to retrieve and use the output value. In this case I would also add a dummy parameter in your custom resource that you can change whenever you want. When something changes in the stack you don't control, you can use this dummy parameter to force the custom resource (and subsequently the alarm) to be recreated.

I have used this second approach before when I needed to get to the Application Load Balancer ARN for an Elastic Beanstalk deployment, which also wasn't readily available.

Related