2

I'm trying to automate the creation of a self terminating AWS EC2 instance using AWS LAMBDA. Self-Terminating AWS EC2 Instance? posted sometime back was helpful to do it via console but I was wondering if this is possible with AWS LAMBDA.

To give some additional context, I'm trying to create a workflow like this:

  • Get user registration through a unique link sent to them (A simple static intranet website)

  • POST the form details to AWS LAMBDA function and create an instance from a snapshot which will automatically terminate in say 12 hours

  • Send the user an email with the connection information (Possibly attaching a RDP file)

The issue I face now is with the second point. I also need to cap the registrations to 10 per day.

PS: Any recommendations or advice on this workflow will be helpful too. (I understand the security issues but this is for intranet and will never be visible to the public)

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
NEO
  • 1,961
  • 8
  • 34
  • 53

3 Answers3

1

There are many ways to achieve what you want. Two ways I can think of:

  • When you launch the instance from Lambda, pass userdata to the instance that sets up a cronjob to terminate the instance using AWS CLI after 12 hours. You can get the instance's id from AWS metadata server. For this you need to have AWS CLI installed and the EC2 instance is attached an IAM role with sufficient privileges to terminate the instance. Or if Python is already preinstalled in that instance, you can write a small python script using Boto3 that terminates the instance 12 hours after the instance is launched.

  • Easiest and the cleanest option - Schedule a lambda event to fire 12 hours later. You need to pass the instance-id to the second lambda which can terminate the previously launched instance.

In both cases, you need to attach sufficient privileges to the lambda function or to the launched instance to terminate the instance.

helloV
  • 50,176
  • 7
  • 137
  • 145
1

These are the step I'd follow

Steps thinking on Microservice, however, you can implement this approach in one Lambda function only.

  1. Create a either a DynamoDB table or RDS instance table to store the instance id TTL (Time to live), and the creation time.

  2. Create a Lambda function with a specific purpose: Terminate instances by instance id.

  3. Create a Lambda function to check the instances according their creation date.

  4. Create a Schedule Cloudwatch event rule that will execute a Lambda function (Created Lambda in step 3) every 10, 30, or whatever N minutes (I recommend a good time interval regarding the amount of instances which will need to be removed at a specific time) to check the created date of instances.

  5. If the creation date of a specific instance is greater or equals to TTL (Time to live), invoke a Lambda function (Created Lambda in step 2) for deletion of that instance.

  6. Wait for Lambda invocation and then delete the table's row related to that instance.

Hope it helps to accomplish your scenario

Community
  • 1
  • 1
Ele
  • 33,468
  • 7
  • 37
  • 75
0

Another way to do this is to create an EC2 instance from your lambda function. When you call the AWS API to create the EC2 instance, provide a userData script. (You'll probably need to anyway to install whatever software you need).

As part of your userData script, run shutdown -H +12, which will schedule a shutdown event of the machine via Old School Unix Commands.

If you want more detail, take a look at the blog post I stole the shutdown idea from

RyanWilcox
  • 13,890
  • 1
  • 36
  • 60