edank
edank

Reputation: 619

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

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

Upvotes: 0

Views: 474

Answers (2)

Riz
Riz

Reputation: 1167

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"]
  }
}

Upvotes: 1

Mark B
Mark B

Reputation: 200722

Create an Event Bridge rule for a failed canary run, that triggers a Lambda function. Have the Lambda function restart the EC2 server via the AWS API/SDK.

Upvotes: 1

Related Questions