Raxso
Raxso

Reputation: 35

Cloudwatch Get InstanceId

How do I get the InstanceId of all instances of Cloudwatch Alarm, I am trying to create a cloudwatch alarm to send email if the disk reach 90% usage.

Resources:    
  EC2DiskHealth:
  Type: AWS::CloudWatch::Alarm
  Properties:
    AlarmDescription: { "Fn::Join" : ["", [{ "Ref" : "AWSEBEnvironmentName" }, ": Disk Usage" ]]}
    Namespace: System/Linux
    MetricName: DiskSpaceAvailable
    Dimensions:
      - Name: InstanceId
        Value : { "Ref" : "instance-id" }
      - Name: Filesystem
        Value: /dev/xvda1
      - Name: MountPath
        Value: /
    Statistic: Average
    Period: 60
    EvaluationPeriods: 5
    Threshold:
      Fn::GetOptionSetting:
        OptionName: ELBHealth
        DefaultValue: "90"
    ComparisonOperator: GreaterThanThreshold
    AlarmActions:
      - arn:aws:sns:awsregion:sns
    InsufficientDataActions:
      - arn:aws:sns:awsregion:sns
    OKActions:
      - arn:aws:sns:awsregion:sns

Output: I should be able to get the instance-id in order for the alarm to work.

    Dimensions:
      - Name: InstanceId
        Value : { "Ref" : "instance-id" }

Error:

Service:AmazonCloudFormation, Message:Template format error: Unresolved resource dependencies [instance-id] in the Resources block of the template

Upvotes: 1

Views: 1218

Answers (2)

C.Vergnaud
C.Vergnaud

Reputation: 877

Your question is to send an email when cloudwatch detect that the disk instance is over 90% used.

It is the basics of cloudwatch task : create the email notification in the cloudwatch alarm itself, set the emails and save.

more details here (it is an example related to CPU, but it is the same principle) :

https://docs.aws.amazon.com/fr_fr/AmazonCloudWatch/latest/monitoring/US_AlarmAtThresholdEC2.html

If you want your instance-id from the instance itself, its instance-id is available through its metadata :

curl http://169.254.169.254/latest/meta-data/instance-id

Upvotes: -1

John Rotenstein
John Rotenstein

Reputation: 269330

It appears that your situation is:

  • You have some existing Amazon EC2 instances
  • You are running some script/code on the instances that send a metric called DiskSpaceAvailable at regular intervals to Amazon CloudWatch
  • You wish to create a CloudFormation template
  • The template should create an Alarm for every EC2 instance when DiskSpaceAvailable exceeds a certain metric

This is not possible.

An Amazon CloudWatch template can create resources and can refer to resources, but it cannot go out and discover resources, nor perform loops over discovered resources.

A template could, for example, create an instance and then add an alarm specifically for that instance. However, it won't auto-discover resources.

You can write an AWS Lambda-backed Custom Resource that can do whatever you wish (you'd have to write the code), but your code would need to create the alarms rather than CloudFormation.

Bottom line: Your use-case is best done via your own code (Lambda or just straight code) rather than using CloudFormation.

Upvotes: 3

Related Questions