How do a trigger a reboot action on lightsail cloudwatch alarm state?

0

I have a lightsail instance that I want to reboot if cloudwatch alarm on it goes off. I don't see in cloudwatch how to restart lightsail instances. Please let me know if someone knows a way to do this.

asked 2 years ago1771 views
3 Answers
0
Accepted Answer

Hello Hunainkapadia,

I understand that you want to trigger a reboot action using CloudWatch alarm on Lightsail. The monitoring that is built in on Amazon Lighsail is only accessible within Lightsail console and are not visible in CloudWatch dashboard by default. If you would like to view Lighsail metrics in CloudWatch and set CloudWatch alarm to trigger an action. You will have to[1]:

  1. Create an IAM user with the necessary permissions to submit the CloudWatch metrics data collected from the Lightsail instance.
  2. Installing the CloudWatch Agent on your Lightsail.
  3. Configuring the CloudWatch Agent to use the IAM user when submitting data to CloudWatch

Once that is done, you can create a CloudWatch Event. Then use that event with a Lambda function to reboot your instance. Below is a sample code you can use to reboot your instance:

import boto3
region = 'us-west-1'
client = boto3.client('lightsail', region_name='region')

def lambda_handler(event, context):
      client.reboot_instance( instanceId='ID-OF-YOUR-LIGHTSAIL-INSTANCE')

For region, replace "us-west-1" with the AWS Region that your instance is in and replace 'ID-OF-YOUR-LIGHTSAIL-INSTANCE' with the ID of the specific instance that you want to reboot.

I hope that this information will be helpful.

Resources:

  1. https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/installing-cloudwatch-agent-ssm.html
Cebi
answered 2 years ago
0

When creating the alarm, you can configure an 'EC2 action' of recover/stop/terminate/reboot. You may want a notification too so that you know it has happened.

profile picture
answered 2 years ago
0

The cloudwatch alarm has been setup automatically when a health check on route53 DNS route fails. This alarm does not allow me call any EC2 Action as it says it's "This action is only available for EC2 Per-Instance Metrics."

How do I reboot the instance when a health check fails?

answered 2 years ago

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