NEO
NEO

Reputation: 2001

AWS trigger when an instance is available

I have created a workflow like this:


    ec2 = boto3.resource('ec2', region_name='us-east-1')
    instances = ec2.create_instances(...)
    instance = instances[0]
    time.sleep(3)
    instance.load()
    return instance.public_dns_name

The problem with this approach is that the user has to wait almost 2 minutes before they were able to login successfully. I'm totally okay to let the lamda run for that time by adding the following code:

instance.wait_until_running()

But unfortunately the API gateway has a 29 seconds timeout for lambda integration. So even if I'm willing to spend it wouldn't work. What's the easiest way to overcome this?

Upvotes: 0

Views: 355

Answers (1)

Ele
Ele

Reputation: 33726

My approach to accomplish your scenario could be Cloudwatch Event Rule.

  • The lambda function after Instance creation must store a kind of relation between the instance and user, something like this:

Proposed table: The table structure is up to you, but these are the most important columns.

------------------------------
| Instance_id   |  User_Id   |
------------------------------

  • Creates a CloudWatch Event Rule to execute a Lambda function.

Firstly, pick Event Type: EC2 Instance State-change Notification then select Specific state(s): Running:

enter image description here

Secondly, pick the target: Lambda function:

enter image description here

  • Lambda Function to send email to the user.

That Lambda function will receive the InstanceId. With that information, you can find the related User_Id and send the necessary information to the user. You can use the SDK to get information of your EC2 instance, for example, its public_dns_name.

This is an example of the payload that will be sent by Cloudwatch Event Rule notification:

{
  "version": "0",
  "id": "6a7e8feb-b491-4cf7-a9f1-bf3703467718",
  "detail-type": "EC2 Instance State-change Notification",
  "source": "aws.ec2",
  "account": "111122223333",
  "time": "2015-12-22T18:43:48Z",
  "region": "us-east-1",
  "resources": [
    "arn:aws:ec2:us-east-1:123456789012:instance/i-12345678"
  ],
  "detail": {
    "instance-id": "i-12345678",
    "state": "running"
  }
}

That way, you can send the public_dns_name when your instance is totally in service.

Hope it helps!

Upvotes: 1

Related Questions