amazon-web-servicesamazon-ec2aws-lambdaspot-instances

Check for ec2 spot interruption notice from Lambda function


I'm currently building an application that requires quite a lot of compute resources, making me use multiple EC2s. I am trying to reduce costs by using spot instances. What I would like to do is have 2 autoscaling groups, one with spot instances and the other one with on-demand instances.

I'd like to be able to query the meta data of the spot instances to check for eventual interruption notices via a Lambda function to start on-demand instances before the spot instance is interrupted.

My problem is that I could not find a way to query the spot related meta data from outside the ec2 instance that I'm trying to query. Has anyone done something similar before? Have I missed a simple way to achieve my goal?


Solution

  • You can configure an EventBridge rule to call the Lambda when an Interruption Warning is launched, and from where you'll start the new On Demand instance.

    Here's the related event pattern to listen in your EventBridge rule:

    {
      "source": ["aws.ec2"],
      "detail-type": ["EC2 Spot Instance Interruption Warning"]
    }
    

    This event will be launched 2 minutes before the spot instance termination, as stated here.