Save on Compute Costs for Applications Deployed Using AWS Elastic Beanstalk with Amazon EC2 Spot Instances

TUTORIAL

Overview

AWS Elastic Beanstalk is an easier way for you to quickly deploy and manage applications in the AWS Cloud. You simply upload your application and Elastic Beanstalk automatically handles the deployment details of capacity provisioning, load balancing, auto scaling, and application health monitoring.
 
Amazon EC2 Spot Instances offer spare compute capacity available in the AWS Cloud at steep discounts compared to On-Demand prices. Amazon EC2 can interrupt Spot Instances with two minutes of notification when Amazon EC2 needs the capacity back. You can use Spot Instances for various fault-tolerant and flexible applications. Some examples are machine learning, analytics, containerized workloads, high-performance computing (HPC), stateless web servers, rendering, CI/CD, and other test and development workloads.
 
With AWS Elastic Beanstalk, you can use EC2 Spot Instances to run your stateless, fault-tolerant applications at up to 90% discount by enabling the Auto Scaling Group to use EC2 Spot Instances following the Spot best practices.

What you will accomplish

In this tutorial, you will learn how to deploy a pre-built sample Node.js application on EC2 Spot Instances using AWS Elastic Beanstalk.

Prerequisites

Before starting this tutorial, you will need:

 AWS experience

Intermediate

 Time to complete

10 minutes

 Cost to complete

Less than $10

 Requires

AWS account with administrator-level access*
 
*Accounts created within the past 24 hours might not yet have access to the services required for this tutorial.

 Last updated

February 3, 2023

Step 1: Create an application using Elastic Beanstalk

1.1 — Log in to the AWS Management Console.

Already have an account? Log in to your account

1.2 — Open the Elastic Beanstalk console.

Choose Create Application to create and configure your application

1.3 — In the Application information section, use sample-nodejs-app as the Application name.

1.4  — In the Platform section, select Node.js and leave the defaults for the Platform branch and Platform version.

Note: You might see a newer Platform version if we have updated the default since this tutorial was last published.

1.5 — In the Application code section, select Sample application.

We will be using the sample application provided by Elastic Beanstalk for this example, but you can upload your own code as well.

Choose Create application.

1.6 — It will take a few minutes while Elastic Beanstalk creates a collection of AWS resources including an Application Load Balancer and an Amazon EC2 Auto Scaling group, in addition to deploying your application code.

1.7 — Once your application is deployed, you will be redirected to the Environments page showing the health and details of the deployed version of your application.

Step 2: Edit the compute environment to use EC2 Spot Instances

2.1 — By default, Elastic Beanstalk creates compute environments with an Auto Scaling group (ASG) with a single instance type and On-Demand purchase option.

In this section, you will modify the compute environment to use EC2 Spot Instances to achieve up to 90% cost savings off of the On-Demand prices.

To modify the compute environment, select Configuration in the left navigation pane under Samplenodejsapp-env.

2.2 — On the Configurations overview page, choose the Edit button in the Capacity settings section.

2.3 — For Environment type, keep the default (Load balanced).

The application needs a minimum of 9 instances to function and should be able to scale up to 3x based on the load. Set the Min to 9 and Max to 27.

2.4 — We will use both On-Demand Instances and Spot Instances in our Auto Scaling group using the MixedInstancesPolicy. Select Combine purchase options and instances for Fleet composition. Leave the Default for Maximum spot price.

For more information on the MixedInstancesPolicy, see the Amazon EC2 Auto Scaling API Reference.

2.5 — Because the sample application is stateless and fault tolerant and can handle an instance being interrupted, we will mostly use Spot Instances, with a small number of On-Demand Instances to provide a consistent baseline of compute. Set the On-Demand base to 3. Set the On-Demand above base to 0%, which means anything above the three On-Demand base Instances will run Spot Instances.

2.6 — Selecting Instance types is an important part of following best practices when using EC2 Spot Instances with Auto Scaling groups. Because Spot Instances are spare On-Demand capacity and their availability at any given moment is dynamic and continually changes in real time, it’s important to be flexible with multiple instance families, sizes, and generations, and to use multiple Availability Zones. Set the Instance types to t2.large, t3a.large, t3.large, m5a.large, m5.large, m4.large, m6i.large, and m6a.large.

Note the instance types availability varies in Regions and if you don’t see an instance in the list, please select another instance that has similar specs.

Hint: You can go to EC2 Console > Instance types to find a list of instance types that meets your criteria by applying filters like vCPUs and Memory.

2.7 — Selecting multiple Availability Zones is important for high availability of the application and also follows EC2 Spot best practices. Select Any 3 for Availability Zones and select three Availability Zones from the dropdown for Placement.

2.8 — Leave the default setting for Scaling triggers and choose Apply.

Step 3: Access your application

3.1 — Once your environment is healthy, choose the load balancer URL for the application, as shown in the screenshot.

3.2 — Congratulations! You have successfully deployed an application using Elastic Beanstalk on EC2 Spot Instances.

Step 4: Review cost savings

4.1 — You can check the compute savings you realized by using EC2 Spot Instances for your application by going to EC2 console > Spot Requests > Savings summary.

Step 5: Clean up resources

5.1 — Although the Amazon EC2 Spot Instances are typically 70% to 90% less expensive than an On-Demand EC2 instance, it is still a good idea to clean up and delete resources once you have completed the tutorial.

Delete the Elastic Beanstalk application by selecting Applications > the application sample-nodejs-app -> Actions menu -> Delete application.

Enter the application name sample-nodejs-app to confirm and choose Delete.

Conclusion

Congratulations! You have successfully deployed your sample application on EC2 Spot Instances for a portion of the compute to take advantage of the steep savings of up to 90% off of the On-Demand pricing.

Was this page helpful?

Next steps

Explore Amazon EC2 Spot Instances

If you want to learn more about Amazon EC2 Spot Instances, visit the Amazon EC2 Spot Instances product page to explore documentation, videos, blogs, and more.

Spot Instances workshops

Want to learn more about Spot Instances? Check out the self-guided Spot Instance workshops to learn more about additional use cases for Spot Instances.

Spot Instances support in Elastic Beanstalk

To learn more about Spot Instance support in the Auto Scaling group for your Elastic Beanstalk environment using Elastic Beanstalk console, the EB CLI, or configuration options, review the documentation.