简体   繁体   English

有没有办法在 CDK 上重启 RDS 和 EC2 实例?

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

I've recently started using CDK and I have little programming experience.我最近开始使用 CDK,但我的编程经验很少。

I've managed to set up an environment with a basic EC2 instance, a VPC with 2 subnets and an RDS instance.我设法设置了一个包含基本 EC2 实例、一个具有 2 个子网的 VPC 和一个 RDS 实例的环境。 I've also created some CloudWatch Alarms for CPU usage of the RDS DB, for example:我还为 RDS 数据库的 CPU 使用情况创建了一些 CloudWatch 警报,例如:

      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.我想创建一个警报,如果实例的 CPU(EC2 的一个警报和 RDS 的另一个警报)超过 %(在本例中为 4),它将重新启动。

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).到目前为止,我还没有找到任何可以重新启动 RDS 实例的东西,而对于 EC2 实例,我只发现了不太适合我需要的InitCommand 类,因为我不想在代码中使用 shell 命令(如果没有其他方法,我猜它必须这样做)。

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:是的,如果您提供的指标是内置的每个实例指标,或者任何具有包含有效实例 ID 的InstanceId维度的指标,您可以使用EC2Action警报操作:

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

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

CDK does not do anything in the cloud directly. CDK 不会直接在云中做任何事情。 The only thing the CDK library does is synth the Cloudformation template. CDK 库唯一要做的就是合成 Cloudformation 模板。 When you use cdk deploy it sends that synthed template to Cloudformation to run.当您使用cdk deploy时,它会将合成的模板发送到 Cloudformation 以运行。

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.如果您需要在部署期间或之后实现功能,最好结合使用 CodePipeline 和自定义资源来检查某些情况并采取相应措施。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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