automatically start an EC2 instance when a request is made

Question:

I have website that I access very infrequently (sometimes 2/3 times a day and sometimes none)

I would like to automatically shutdown the instance as soon as no traffic happen (this would be possible when setting a cloudwatch alarm).

The current issue I have is that I would like to start the instance again as soon as there is a request on the website (I don’t mind at all having to wait for the instance to come back online).

Is there any way of doing that? If yes how would that work technically?

Answer:

DISCLAIMER: only some theoretical thoughts

Main idea: a landing page using S3 (static website), visiting this page will trigger an Lambda script.This Lambda script will launch the instance.

More details:

  1. an Amazon S3 static website with a landing page + javascript to:
    • make a call to Amazon API Gateway
    • test if the instance is ready
    • redirect to proper page when everything is ready
    • maybe some url/javascript tricks to avoid the instance to be started by crawlers and bots, scanners etc
  2. an Amazon API Gateway: only using to trigger an event for Amazon Lambda
  3. an Amazon lambda script used for launching your instance, if is not running.

Depending on your needs, you can try to go serverless like here: https://gofore.com/en/going-serverless-with-amazon-s3-and-lambda/

Leave a Reply