Tuesday, 30 May 2023

Replacing Unhealthy EC2 Instance in Elastic Beanstalk Environment

 In this article, we will look into how to automate the replacement of EC2 instances in an AWS Elastic Beanstalk environment that are failing Elastic Load Balancer health checks.

By default, the Elastic Beanstalk environment’s Auto Scaling group health check configuration is set to the EC2 type which simply performs a status check of EC2 instances. This means that if you want to automate the replacement of unhealthy EC2 instances you must change the health check type of the environment’s auto-scaling group from EC2 to ELB by using an .ebextension.

To do this follow the below steps:

Step 1: Navigate to your working directory locally. You can create an .ebextensions folder. In this case, there is already one.

Step 2: Now let’s create a new .config file. We’ll name this autoscaling.config. 

Step 3: Now we’ll edit the autoscaling.config file and enter the following details and save it.

Step 4: Now let’s create a new application source bundle by going back to the root of our working directory. At this point we can deploy the updated source bundle after logging into the AWS Management Console. For this navigate to the Elastic Beanstalk console.

Step 5: Then select the environment. Choose Upload and deploy.

Step 6: Then select Choose file. Browse to the working directory and then select the new zip. Provide a logical Version label and finally Deploy it to your environment.

This can take some time depending on your deployment strategy and now you’ve successfully updated your environment to allow the Elastic Beanstalk environment’s auto-scaling group to automatically replace instances that fail ELB health checks. 

Attach EBS Volume in EC2 Instance

 Elastic Block Store is a type of storage in AWS. It behaves like a virtual hard drive device in AWS and it is persistent storage. In EC2- The instance root volume will be destroyed when we terminate the EC2 instance, as well as data, will also be deleted. That’s why we attach extra volume with our EC2-Instance for the data availability it does not matter EC2 instance is terminated or not. we can easily be attached or de-attached with any EC2 instance but we can use once at a time EBS volume with one EC2 Instance.

EBS Volume Types:

  • SSD
  • HDD
  • Magnetic Disk

Step To Attach EBS Volume in EC2-Instance:

Step 1: Go to All services and click on EC2

EC2

 

Step 2: Click on the Launch Instance.

launch instance

 

Step 3: Give the EC2 instance name or tag like the “EBS-Volume” name. 

name and tag

 

Step 4: Choose Amazon Machine Image for your instance.

ami

 

Step 5: Choose an Instance type.

instance type

 

Step 6: Choose the Key Pair or Create the Key pair.

key pair

 

Step 7: Create the Security Group then open the port as per your requirement

security group

 

Note: By Default EC2 root volume is attached with 8GB. ‘1’ is showing the root volume.

Step 8: Click on the Add new Volume for the extra EBS Volume. ‘2’ is denoting how to attach EBS

config storage

 

Step 9: Configure the EBS Size.

ebs size

 

Step 10: Select the EBS type as per your requirement.

ebs type

 

Step 11:Click on the Launch Instance button.

launch instance

 

Now you can see both volume Root Volume 8GB and EBS Volume 10GB

output

Introduction to AWS Elastic Beanstalk

 AWS Elastic Beanstalk is an AWS-managed service for web applications. Elastic Beanstalk is a pre-configured EC2 server that can directly take up your application code and environment configurations and use it to automatically provision and deploy the required resources within AWS to run the web application. Unlike EC2 which is Infrastructure as a service, Elastic Beanstalk is a Platform As A Service (PAAS) as it allows users to directly use a pre-configured server for their application. Of course, you can deploy applications without ever having to use elastic beanstalk but that would mean having to choose the appropriate service from the vast array of services offered by  AWS, manually provisioning these AWS resources, and stitching them up together to form a complete web application. Elastic Beanstalk abstracts the underlying configuration work and allows you as a user to focus on more pressing matters.

This raises a concern that if elastic Beanstalk configures most of the resources itself and abstracts the underlying details. Can developers change the configuration if needed? The answer is Yes. Elastic Beanstalk is provided to make application deployment simpler but at no level will it restrict the developers from changing any configurations.

How Elastic Beanstalk Works 

Elastic Beanstalk is a fully managed service provided by AWS that makes it easy to deploy and manage applications in the cloud without worrying about the underlying infrastructure. First, create an application and select an environment, configure the environment, and deploy the application.

Elastic Beanstalk Works

 

 

Elastic Beanstalk-Supported Platforms

AWS Elastic Beanstalk provides a number of platforms on which we may develop our apps, deploy our code, and it manages the entire supporting architecture and computational resources for code execution. Following are the Platforms for programming languages provided by Elastic Beanstalk.

Platforms for Programming Languages Provided By Elastic Beanstalk are 

Platforms for Application Servers Provided by Elastic Beanstalk are

Elastic Beanstalk Features

  • Elastic Beanstalk offers preconfigured runtime-like environments and deployment tools which makes it easy to deploy our application 
  • It supports numerous platforms and programming languages like GO, Python java, etc.
  • Elastic Beanstalk scales your application automatically when the demand increases with the help of auto-scaling rules.
  • Elastic Beanstalk can integrate with databases such as Mysql, Oracle, and Microsoft SQL Server. 
  • Access control via AWS Identity and Access Management and built-in security features like SSL/TLS encryption are provided by Elastic Beanstalk (IAM).

AWS Elastic Beanstalk Components

  • Application: Elastic Beanstalk directly takes in our project code. So Elastic Beanstalk application is named the same as your project home directory.
  • Application Environments: Users may want their application to run on different environments like DEV, UAT, and PROD. You can create and configure different environments to run applications on different stages.
  • Environment Health:  One of the most lucrative features of running applications on AWS or most of the other cloud platforms is automated health checks. AWS runs automatic health checks on all EC-2 deployments (Elastic Beanstalk is a managed EC-2 service) which can be monitored from the AWS console. For example, in the case of web applications AWS will regularly, as scheduled by the developers, ping the application to check if the response is status code 200 and if the application is running as expected. Health check responses:
    • Red: The application failed all health tests.
    • Yellow: The application failed some of the health tests.
    • Grey: The application is updating.
    • Green: The application passed the health check successfully.
  • Isolated: All environments within a single application are isolated from each other (independent of each others’ running states). Needless to say, two different applications are also isolated.
  • Scalability: Using Auto-Scaling within Elastic Beanstalk makes the application dynamically scalable.
  • Elastic Load Balancing: All the web requests to the application are not directly relayed to application instances. They first hit the Elastic Load Balancer (ELB), which, as the name suggests, balances the load across all the application instances.
  • Language support: Elastic Beanstalk supports the applications developed with Java, .NET, PHP, Node.js, Python, Ruby, Go, and Docker on familiar servers such as Apache, Nginx, Passenger, and IIS.
  • Pricing: There is no extra charge for using Elastic Beanstalk. Users are only required to pay for the services and resources provisioned by Elastic Beanstalk Service.
  • Automatic Provisioning: Elastic Beanstalk takes away the burden of choosing the right services and configuring their security groups to work together. 
  • Impossible to Outgrow: AWS claims that since Elastic Beanstalk uses the Auto Scaling feature it can, in theory, handle any amount of internet traffic.

How to Deploy an Application Using Elastic Beanstalk

Step 1: Visit the AWS Console and type “Elastic Beanstalk” into the search bar. After selecting Elastic Beanstalk from the drop-down menu below, you will be taken to the Elastic Beanstalk page where you can click “Create application” to launch a sample application using Elastic Beanstalk. As shown in the screenshots

Elastic Beanstalk

 

Create Application

 

Step 3: In this step, we are going to configure the necessary Application information like Name, and key-value pairs and select the platform in which the language the application is developed has to be deployed, the platform branchplatform version, and Application code

Example: In this example, we are going to deploy the sample Java application which is provided by AWS itself select all the options as shown in the below screenshot.

If you want to deploy your own application code then select the option “Upload your code” It needs to be in form of a ZIP or WAR file which is compulsory and needs to be in your local computer or S3 bucket and a version label which your going to provide must be unique.

Create a web app

 

Configure Platform

 

Step 4: In this step, We do not need to do anything AWS Elastic Beanstalk will take care of the resource required to deploy the web application like 

As shown in the image below

Resources of Elastic Beanstalk

 

Step 5: In the screenshot below, under “Gfgapplication-env,” we can see the URL of the application, which can be accessed through the internet. We can also see that the health is marked in “green color, OK,” indicating that our application has been successfully deployed and has not encountered any difficulties. The resources or full architecture developed by AWS Beanstalk to deploy our application are visible in the Recent Events section.

Web application overview

 

Step 6: The very last and most important step requires us to use the URL provided by the Elastic Beanstalk to visit our Jave web application. as seen in the image below.

Java web application

 

Working With Amazon Beanstalk

Amazon Beanstalk Through AWS Console

 We can create and manage Amazon Beanstalk using the AWS Management Console log in to your AWS account. follow the steps that have mentioned above in “How to Deploy an Application Using Elastic Beanstalk”

Amazon Beanstalk Through AWS Command Line Interface (AWS CLI) 

AWS provides a set of commands that can be run on AWS-CLI (AWS Command Line Interface) to manage your services. Much like you’d manage from your AWS Console. Following is a list of commands that can be run for managing the AWS Elastic Beanstalk service.

  • abort-environment-update
  • apply-environment-managed-action
  • check-dns-availability
  • compose-environments
  • create-application
  • create-application-version
  • create-configuration-template
  • create-environment
  • create-platform-version
  • create-storage-location
  • delete-application
  • delete-application-version
  • delete-configuration-template
  • delete-environment-configuration
  • delete-platform-version
  • describe-account-attributes
  • describe-application-versions
  • describe-applications
  • describe-configuration-options
  • describe-configuration-settings
  • describe-environment-health
  • describe-environment-managed-action-history
  • describe-environment-managed-actions
  • describe-environment-resources
  • describe-environments
  • describe-events
  • describe-instances-health
  • describe-platform-version
  • list-available-solution-stacks
  • list-platform-branches
  • list-platform-versions
  • list-tags-for-resource
  • rebuild-environment
  • request-environment-info
  • restart-app-server
  • retrieve-environment-info
  • swap-environment-names
  • terminate-environment
  • update-application
  • update-application-resource-lifecycle
  • update-application-version
  • update-configuration-template
  • update-environment
  • update-tags-for-resource
  • validate-configuration-settings

In simple words, Elastic Beanstalk is an automatic application deployment service. It is the easiest possible way to deploy your applications on AWS even with possibly no prior experience on the platform.

Pricing Models for Amazon EC2

 Amazon Elastic Compute Cloud (Amazon EC2) is a web service that provides secure, resizable compute capacity in the cloud.You can select from a variety of operating systems and resource configurations like memory, CPU, storage that is required for your application. Amazon EC2 enables you to obtain and configure capacity within minutes. You can use one or hundreds or even thousands of server instances simultaneously. Some of the considerations for estimating Amazon EC2 cost are Operating systems, Clock hours of server time, Pricing Model, Instance type and Number of instances. 

Users can pay only for what they are using according to per-second billing. so, This EC2 per-second billing will helps the users to remove the cost of the unused minutes and also seconds from the user’s bill.

Pricing models for Amazon EC2: 

There are four pricing models for Amazon EC2 instances: On-Demand Instances, Reserved Instances, Spot Instances, and Dedicated Hosts.

  1. On-Demand Instances: 
    In this model, based on the instances you choose, you pay for compute capacity per hour or per second (only for Linux Instances) and no upfront payments are needed. You can increase or decrease your compute capacity to meet the demands of your application and only pay for the instance you use. This model is suitable for developing/testing application with short-term or unpredictable workloads. On-Demand Instances is recommended for users who prefer low cost and flexible EC2 Instances without upfront payments or long-term commitments. Depending on the demands of your application Users can also increase or decrease their compute capacity. 
  2. Spot Instances: 
    Amazon EC2 Spot Instances is unused EC2 capacity in the AWS cloud. Spot Instances are available at up to a 90% discount compared to On-Demand prices. The Spot price of Amazon EC2 spot Instances fluctuates periodically based on supply and demand. It supports both per hour and per second (only for Linux Instances) billing schemes. Applications that have flexible start and end times and users with urgent computing needs for large scale dynamic workload can choose Amazon EC2 spot Instances. Spot Instances is very best for the applications that are very feasible and having very low compute prices and also for the applications which have flexible start and end times. 
  3. Reserved Instances: 
    Amazon EC2 Reserved Instances provide you with a discount up to 75% compared to On-Demand Instance pricing. It also provides capacity reservation when used in specific Availability Zone. For applications that have predictable workload, Reserved Instances can provide sufficient savings compared to On-Demand Instances. The predictability of usage ensures compute capacity is available when needed. Customers can commit to using EC2 over a 1- or 3-year term to reduce their total computing costs. It has very flexible pricing model which offers low prices on EC2.
  4. Dedicated Hosts: 
    A Dedicated Host is a physical EC2 server dedicated for your use. Dedicated Hosts can help you reduce costs by allowing you to use your existing server-bound software licenses like Windows server, SQL server etc. and also helps you to meet the compliance requirements .Customers who choose Dedicated Hosts have to pay the On-Demand price for every hour the host is active in the account. It supports only per-hour billing and does not support per-second billing scheme. The Dedicated Host is something physical EC2 server which is dedicated for the use of user.

Per-second billing scheme: 
Today, many customers use Amazon EC2 to do a lot of work in a short time, sometimes minutes or even seconds. In 2017, AWS announced per-second billing for usage of Linux instances across On-Demand, Reserved, and Spot Instances. The minimum unit of time that will be charged is a minute (60 seconds ), but after your first minute of time, it is charged for seconds. However if you start then stop an instance in 30 seconds, you will be charged the 60 seconds not 30.

This Per-second billing is available for all the instances launched in:
 

  • On-Demand Instances
  • Reserved Instances 
  • Spot instances
  • All regions 
  • All  Availability Zones
  • Amazon Linux
  • Windows
  • Ubuntu