Wednesday, April 27, 2022

[SOLVED] How to restart an ec2 server when CloudWatch Synthetics Canary fails?

Issue

I have a site I'm working on, and one of the pages retrieves data from another server, let's call it server B.

Occasionally server B fails to return data, and the main site will give a 500 error.

I want to restart server B when that happens, and I was thinking I could use CW synthetics to do that. I've created a CW alarm to trigger, but I don't have a direct way to restart an ec2 server, since it's not associated directly with one.

I've thought of calling a lambda that will restart the server, but I'm wondering if there's a simpler configuration/solution I can use.

Thanks


Solution

You can creat an Event Bridge rule for failed canary run by selecting Synthetics Canary TestRun Failure from AWS events then in Event pattern -> AWS service select Cloudwatch Syntheticsand in Event type select Synthetics Canary TestRun Failure. From the Target select AWS service -> Select a target select EC2 Rebootinstances API call and give the instance id.

UPDATED: You can use custom patterns and pass your json which can match the failure pattern. In your case I would use something like,

{
  "source": ["aws.synthetics"],
  "detail-type": ["Synthetics Canary TestRun Failure"],
  "region": ["us-east-1"],
  "detail": {
  "account-id": ["123456789012"],
  "canary-id": ["EXAMPLE-dc5a-4f5f-96d1-989b75a94226"],
  "canary-name": ["events-bb-1"]
  }
}


Answered By - Riz
Answer Checked By - Marilyn (WPSolving Volunteer)