简体   繁体   中英

Is there a way to restart RDS and EC2 instances on CDK?

I've recently started using CDK and I have little programming experience.

I've managed to set up an environment with a basic EC2 instance, a VPC with 2 subnets and an RDS instance. I've also created some CloudWatch Alarms for CPU usage of the RDS DB, for example:

      const CPUUsage = new cw.Alarm(this, 'CPUUsage', {
        metric: cpuUsage, //imported from another stack where I created the DB
        threshold: 4,
        evaluationPeriods: 2,
        alarmName: 'DB CPU Usage',
      });

I want to create an alarm where if the CPU of an instance (one alarm for EC2 and another for RDS) goes over an % (4 in this case) it would restart.

So far I haven't found anything that would restart the RDS instance, and for the EC2 instance I've only found the InitCommand class which doesn't really fit my needs, as I don't want to use shell commands in the code (if there's no other way it has to be done that way I guess).

Thank you in advance for any help given!

Yes, if you supply a metric that is a built-in per-instance metric, or any metric with an InstanceId dimension that contains a valid instance ID, you can use the EC2Action alarm action:

alarm.addAlarmAction(
  new actions.Ec2Action(actions.Ec2InstanceAction.REBOOT),
);

Refer to https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/UsingAlarmActions.html

CDK does not do anything in the cloud directly. The only thing the CDK library does is synth the Cloudformation template. When you use cdk deploy it sends that synthed template to Cloudformation to run.

If you need functionality to occur during or after deployment, you are better served by a combination of CodePipeline and custom resources to check for certain situations and act accordingly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM