2
votes

I have an application load balancer that contains several EC2 spot instances.

I found that one of them was terminated because of BidEvictedEvent, i.e., someone requested a spot instance with a price higher than mine.

The BidEvictedEvent made my web service unavailable (502 bad gateway) for a few seconds.

So I want to execute a script that detaches the EC2 from the auto-scaling group before it got terminated.

Now my question is, how can I simulate BidEvictedEvent so that I can make sure that my script got executed correctly?

I've tried terminating the EC2 instance from AWS console. But this doesn't create 502 bad gateway for the client of my web service when there's no script executed before EC2 termination.

I use the method mentioned here to monitor when the BidEvictedEvent occurs.

1

1 Answers

0
votes

The link you provided is for the old way of detecting spot interruption notices. Maybe this is exactly how you want to do this, but these days you would use CloudWatch Event rule for EC2 Spot Instance Interruption Warning.

Using the rule, you could setup a lambda function which would perform actions on your instance scheduled for the termination. These action can include removal from ASG, or running custom commands on it through SSM Command Run.

What's more you could easily simulate the event, as it has known format and you could invoke the lambda with the test event.

More info about this is in Taking Advantage of Amazon EC2 Spot Instance Interruption Notices. The example of spot interruption event that your lambda function would need to process/simulate is:

{
  "version": "0",
  "id": "1e5527d7-bb36-4607-3370-4164db56a40e",
  "detail-type": "EC2 Spot Instance Interruption Warning",
  "source": "aws.ec2",
  "account": "123456789012",
  "time": "1970-01-01T00:00:00Z",
  "region": "us-east-1",
  "resources": [
    "arn:aws:ec2:us-east-1b:instance/i-0b662ef9931388ba0"
  ],
  "detail": {
    "instance-id": "i-0b662ef9931388ba0",
    "instance-action": "terminate"
  }
}