Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to set "ec2 auto recovery" action on EC2 instance? #5921

Closed
Pankajc123 opened this issue Jan 23, 2020 · 2 comments
Closed

How to set "ec2 auto recovery" action on EC2 instance? #5921

Pankajc123 opened this issue Jan 23, 2020 · 2 comments
Assignees
Labels
@aws-cdk/aws-ec2 Related to Amazon Elastic Compute Cloud guidance Question that needs advice or information. needs-triage This issue or PR still needs to be triaged.

Comments

@Pankajc123
Copy link

❓ General Issue

The Question

How can I set "ec2 auto recovery" action on EC2 instance, using "Alarm" construct?

Environment

  • CDK CLI Version: 1.21.1
  • Module Version:
  • OS: Windows
  • Language: .Net

Other information

I can create an Alarm using code snippet below

var ec2recoveryalarm = new Alarm(this, "EC2InstanceRecoveryAlarm", new AlarmProps{
                AlarmName = "Sample",
                AlarmDescription = "Trigger a recovery when instance status check fails for 5 consecutive minutes.",
                ComparisonOperator = ComparisonOperator.GREATER_THAN_THRESHOLD,
                Statistic = "Minimum",
                Period = Duration.Seconds(60),
                EvaluationPeriods = 5,
                Threshold = 0,
                Metric = new Metric(new MetricProps{
                    MetricName = "StatusCheckFailed_System",
                    Namespace = "AWS/EC2",
                    Dimensions = new Dictionary<string, object>()
                    {
                        {"InstanceId", primaryec2instance.InstanceId}
                    }
                }),
                ActionsEnabled = true                
            });   

I am not sure how to add ec2 auto recovery action on same alarm.

I tried something like this but didn't work.

  ec2recoveryalarm.AddAlarmAction(new[]{(IAlarmAction) new  AlarmActionConfig{
             AlarmActionArn = Fn.Sub("arn:${AWS::Partition}:automate:${AWS::Region}:ec2:recover")
   }});

I am trying to add following resource in my CF template

"EC2InstanceRecoveryAlarm" : {
            "Type" : "AWS::CloudWatch::Alarm",
            "Properties" : {
                "AlarmDescription" : "Trigger a recovery when instance status check fails for 5 consecutive minutes.",
                "MetricName" : "StatusCheckFailed_System",
                "Namespace" : "AWS/EC2",
                "Statistic" : "Minimum",
                "Period" : "60",
                "EvaluationPeriods" : "5",
                "Threshold" : "0",
                "ComparisonOperator" : "GreaterThanThreshold",
                "AlarmActions" : [ {"Fn::Join" : ["", ["arn:", {"Ref" : "AWS::Partition"}, ":automate:", { "Ref" : "AWS::Region" }, ":ec2:recover" ]]} ],
                "Dimensions" : [ {
                    "Name" : "InstanceId",
                    "Value" : {"Ref" : "AllInOneEC2Instance"}
                } ]
            }
        }

I can add all properties except "AlarmActions" as mentioned above using "Alarm" construct.

I can add all properties including "AlarmActions" with "CfnAlarm" construct. But I want to achieve same using "Alarm" construct.

"CfnAlarm" construct code snippet is as below.

var ec2recoveryalarm = new CfnAlarm(this, "PrimaryInstanceRecoveryAlarm", new CfnAlarmProps{
                AlarmDescription = "Trigger a recovery when instance status check fails for 5 consecutive minutes.",
                MetricName = "StatusCheckFailed_System",
                Namespace = "AWS/EC2",
                Statistic = "Minimum",
                Period = 60,
                EvaluationPeriods = 5,
                Threshold = 0,
                ComparisonOperator = "GreaterThanThreshold",
                AlarmActions = new[]{Fn.Sub("arn:${AWS::Partition}:automate:${AWS::Region}:ec2:recover")},
                Dimensions = new[]{new CfnAlarm.DimensionProperty{
                    Name = "InstanceId",
                    Value = primaryec2instance.Ref
                }}
            }); 

Any help in this regards is much appreciated.

@Pankajc123 Pankajc123 added the needs-triage This issue or PR still needs to be triaged. label Jan 23, 2020
@SomayaB SomayaB added guidance Question that needs advice or information. @aws-cdk/aws-ec2 Related to Amazon Elastic Compute Cloud labels Jan 23, 2020
@rix0rrr
Copy link
Contributor

rix0rrr commented Jan 24, 2020

We're happy to help you out, but GitHub issues is not a support forum. There are
websites much better suited for that, such as StackOverflow.

Please ask that same question again on StackOverflow under the
aws-cdk
. If you can't get
a response to your question, please bring it to our attention again and we will
do our best to answer it.

@rix0rrr rix0rrr closed this as completed Jan 24, 2020
@IndikaUdagedara
Copy link

You can do something like this

  1. Implement IAlarmAction
export class Ec2Action implements IAlarmAction {
  actionArn: string;
  constructor(actionArn: string) {
    this.actionArn = actionArn;
  }

  bind(
    scope: cdk.Construct,
    alarm: cloudwatch.IAlarm
  ): cloudwatch.AlarmActionConfig {
    return { alarmActionArn: this.actionArn };
  }
}

  1. Use it like this
    statusCheckAlarm.addAlarmAction(
      new Ec2Action(`arn:aws:automate:${region}:ec2:recover`)
    );

A full example below

    const statusCheckMetric = new cloudwatch.Metric({
      metricName: "StatusCheckFailed_System",
      namespace: "AWS/EC2",
      dimensions: {
        InstanceId: <instanceId>
      },
      statistic: "Minimum",
      period: cdk.Duration.minutes(1),
    });

    const statusCheckAlarm = statusCheckMetric.createAlarm(
      this,
      `StatusCheckAlarm`,
      {
        evaluationPeriods: 5,
        threshold: 0,
        comparisonOperator:
          cloudwatch.ComparisonOperator.GREATER_THAN_THRESHOLD,
        statistic: "Minimum",
      }
    );

    statusCheckAlarm.addAlarmAction(
      new SnsAction(props.alarmTopic),
      new Ec2Action(`arn:aws:automate:${props.env?.region}:ec2:recover`)
    );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-ec2 Related to Amazon Elastic Compute Cloud guidance Question that needs advice or information. needs-triage This issue or PR still needs to be triaged.
Projects
None yet
Development

No branches or pull requests

4 participants