e-Zest members share technology ideas to foster digital transformation.

Just-in-Time Cloud Resources

Written by Sneha Sajjan | Nov 22, 2018 7:41:39 AM
To date, there exists many methods to schedule the start and stop of cloud resources for an application. If it can be known when an application is used and when not, for example, if at night the machines are not in use then scheduling can be used to accordingly deprovision the resources.

We can now take a step ahead in a way that the system can detect when a user is looking to use an application, and only then starts the resources for that application. Similarly, it also detects if an application hasn’t been used for quite some time, and accordingly stops the corresponding resources.

Key technologies used:

  • AWS cloud service
  • PHP for server-side scripting
  • SQL as a database
Adding an application and its resources’ details to the database:

A simple form lets an administrator to add an application along with its URL and details of its corresponding cloud resources like region, tag name, credentials etc.

These details are saved in the database and simultaneously the page of the mentioned URL and its assets are downloaded and saved to the system in one folder.

This is to prevent a user from directly accessing an application’s URL. Instead, the user will access the static downloaded page first. It allows to detect when someone is looking to use that application.

Form for adding an application to the system

Static page with all its assets downloaded in specified folder

Code snippet injection:

When the static pages of the URL are downloaded, a small snippet of code is automatically injected into the main HTML page. This snippet imposes a spinner/loader onto the content when the page is opened in the browser. It also contains a callback to a script that starts the cloud resources.


When a user accesses an application:

When a user wants to access a particular application, he/she is first led to the static page of the application downloaded on the system. Because of the injected code, a loader is displayed on top of the page content while at the same time a callback containing that static page’s URL is sent to a script which is responsible for starting the associated cloud resources.

Starting cloud resources:

AWS SDK for PHP provides a convenient API to manage EC2 instances. For example, to describe, monitor, start or stop them.

https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/ec2-examples-managing-instances.html

The callback contains the URL of the current static page. The folder name in which the application’s pages are contained is extracted from this URL and based on this, the corresponding cloud resources details of the application are retrieved from the database along with the actual URL.

These details are used in the AWS API call to find out all the instances that are associated with this particular application using describeinstances(). Tags are used as the common property among all the instances. Subsequently, we use startinstances() to start all these instances.

Redirecting to actual application:

The actual URL that is obtained during retrieval from database is returned as the result of the callback once the cloud instances are in running state. The callback function then redirects the browser window to the actual application page.


Stopping/deprovisioning resources:

We need to identify if an application has not been used for quite some time and stop the resources for the same.

AWS SDK for PHP provides an API to pull the metric statistics of EC2 instances using the AWS CloudWatch monitoring service.

https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/API_GetMetricStatistics.html

The list of all applications and their details can be retrieved from the database. For each application in the system, the getMetricStatistics() call is used to obtain the CPU utilization metrics (for a certain time range) of all instances associated with that application. If the average CPU utilization of all the instances for an application is less than a certain threshold, those instances are stopped.

A CRON job executing this is scheduled to run repeatedly with a certain time interval. Therefore, this CRON job keeps checking for unused idle resources at regular intervals.


This is one way in which just-in-time provisioning and deprovisioning of cloud resources for an application is brought about. This ensures that resources are running only when required thus saving on both energy and costs.