Edit Account ID field with Cloud Formation template

0

Hello guys,

I want to create a CloudWtach alarm with a CloudFormation template. The creation works up to the account ID. I specify the ID of the EC2 instance I want to monitor in the template with a paramter but after the creation of the template the metric gets no data because the management account is always listed in the field "Account ID". Is there a way to pass the parameter Account ID with the Cloud Formation template?

This is my current template:

AWSTemplateFormatVersion: '2010-09-09'
Parameters:
  EC2InstanceId:
    Type: String
    Description: The ID of the EC2 instance to monitor
  AlarmName:
    Type: String
    Description: The name of the CloudWatch alarm
  AccountId:
    Type: String
    Description: The AWS Account ID to associate with the resources
Resources:
  CPUUtilizationAlarm:
    Type: 'AWS::CloudWatch::Alarm'
    Properties:
      AlarmName: !Ref AlarmName
      AlarmDescription: 'Alarm for CPU Utilization > 2% on EC2 Instance'
      Namespace: 'AWS/EC2'
      MetricName: 'CPUUtilization'
      Dimensions:
        - Name: 'InstanceId'
          Value: !Ref EC2InstanceId
        - Name: 'AccountId'
          Value: !Ref AccountId  # Use the AccountId parameter for the Dimension
      ComparisonOperator: 'GreaterThanThreshold'
      Threshold: 2
      EvaluationPeriods: 1
      Period: 60
      Statistic: 'Average'

And this is the field I'm referring to: Enter image description here

asked 8 months ago493 views
2 Answers
0
Accepted Answer

There is no AccountId dimension. Dimensions are predefined and you can check them by using AWS docs.

The corrected template will be:

AWSTemplateFormatVersion: '2010-09-09'
Parameters:
  EC2InstanceId:
    Type: String
    Description: The ID of the EC2 instance to monitor
  AlarmName:
    Type: String
    Description: The name of the CloudWatch alarm
Resources:
  CPUUtilizationAlarm:
    Type: 'AWS::CloudWatch::Alarm'
    Properties:
      AlarmName: !Ref AlarmName
      AlarmDescription: 'Alarm for CPU Utilization > 2% on EC2 Instance'
      Namespace: 'AWS/EC2'
      MetricName: 'CPUUtilization'
      Dimensions:
        - Name: 'InstanceId'
          Value: !Ref EC2InstanceId
      ComparisonOperator: 'GreaterThanThreshold'
      Threshold: 2
      EvaluationPeriods: 1
      Period: 60
      Statistic: 'Average'

But, you can create a custom metrics and use any dimensions you want: https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/publishingMetrics.html

profile picture
answered 8 months ago
0

Hi,

You can access current account id via CFN pseudo-parameters: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/pseudo-parameter-reference.html

It's called: AWS::AccountId see details from link above

Some examples on this page: https://www.obstkel.com/cloudformation-pseudo-parameters

Best,

Didier

profile pictureAWS
EXPERT
answered 8 months ago
  • Hey,

    is it then possible to override the AWS::AccountId parameter?

    This way is working for the region, but not for Account ID:

    Region: Description: AWS Region Type: String Default: ${AWS::Region}

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions