Launching EC2 Instances from Lambda

A quick guide to launching EC2 instances from AWS Lambda for those long running tasks.

thomas michael wallace
tomincode

--

Today’s post is going to be a quick one- as I got a bit wrapped up solving the problem. I’m a member of the Serverless Forum Slack, and yesterday someone came online and asked if anyone had any examples of launching EC2 instances from lambdas.

Well, here’s something:

If you’ve never seen it before, Boto3 is the AWS-SDK for Python. By using run_instances we can quickly and easily launch an EC2 instance. The trick, however, is getting the newly launched instance to actually do something.

To do that, we need to use the UserData option. With a compatible image (like Amazon Linux), it allows you to provide a script to be executed as a function. For this demo, we fire up a server that responds with the message provided in the lambda’s arguments.

The final thing to notice is the use of InstanceInitiatedShutdownBehaviour — assuming that this functionality needs to last longer than a lambda, but not forever; you need a way to switch the instance off. By setting the shutdown behaviour to terminate, you ensure that the EC2 instance will be terminated (not just shutdown, or even reboot) when you tell the OS to shutdown.

I’ll be elaborating and tweaking this in later posts (think Spot Instances and S3 data transfers).

n.b. You can copy the above into the inline-editor for a Python 2.7 lambda function (the handler is lambda_function.lambda_to_ec2). For this example you may also need to open port 80 to traffic on the default security role. You’ll also need an IAM Role with a policy similar to the following to actually use this lambda, as it needs to be able to run EC2 instances:

--

--