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

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:


writing daily lessons on the stuff tom does in code.

thomas michael wallace

Written by

writes about the stuff he does in code.



writing daily lessons on the stuff tom does in code.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade