Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start AWS instance using a link and AWS-Lambda

I'd like to give my client a (password protected) link, such that opening it will start an AWS instance. Is it possible to do it using AWS-Lambda without constantly running another instance?

like image 578
dimid Avatar asked Oct 20 '25 02:10

dimid


2 Answers

Yes, for example here is a function that will start an instance:

import boto3
# Enter the region your instances are in, e.g. 'us-east-1'
region = 'XX-XXXXX-X'
# Enter your instances here: ex. ['X-XXXXXXXX', 'X-XXXXXXXX']

instances = ['X-XXXXXXXX']

def lambda_handler(event, context):

    ec2 = boto3.client('ec2', region_name=region)

    ec2.start_instances(InstanceIds=instances)

    print 'started your instances: ' + str(instances)

once you create that Lambda function (similar to the above - from the link below), you can define an AWS API endpoint in AWS and have it call the lambda function.

This link may help with some of what you want to do (the lambda functions).

https://aws.amazon.com/premiumsupport/knowledge-center/start-stop-lambda-cloudwatch/

like image 54
E.J. Brennan Avatar answered Oct 21 '25 17:10

E.J. Brennan


Yes you can. You should use a combination of AWS lambda and AWS api gateway. AWS api gateway (gw) takes care of exposing a http url and you can optionally let AWS api gateway handle the authentication using a custom authorizer.

Api gateway can trigger an AWS lambda function which will then run the actual code to start the instance.

Additional notes:

  • bare in mind AWS lambda max execution time is 5minutes, an instance may take more than 5 minutes to get in 'running' state therefore you should handle this event in your function code
  • AWS Api gateway integrates with cognito for request authentication without having to implement a basic auth.
  • you can also pass parameters to your call so the api can get the instance Id or tag as parameter.

I hope this helps.

G.

like image 26
Girolamo Piccinni Avatar answered Oct 21 '25 17:10

Girolamo Piccinni



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!