Micro-service with AWS ElasticBeanStalk | Part1
Welcome back readers. In this blog, we shall be seeing how to deploy and launch a Spring boot based micro-service at AWS cloud using ElasticBeanStalk.
Question:- How do we access the AWS dashboard ?
Answer:- Accessing the AWS dashboard :- Assuming that we have a AWS account for ourselves, Let’s first create an IAM user with AdministratorAccess. First step shall be to create the UserGroup.
Question:- How to create the IAM User ?
Answer:- Next, we shall be creating an IAM user and shall provide to this IAM user, the Programmatic-Access & AWS-Management-Console-access :-
Next step would be to assign the Group to this IAM user :-
Let’s review the IAM user that we are planning to create :-
Finally, hit the create button and here we go with our user. Note that, using this IAM user, we shall be logging into the AWS console as well as our Spring Boot application shall also be using this very IAM user in order to connect to the AWS world.
Question :- What is The cloud ?
Answer:- The most important feature of cloud is on demand resource provisioning. Let’s talk about the good old days where if I need a server, I would need to start planning months ahead but with the cloud it’s on demand. If I need it now, I place a request, I would get the resource. That’s the most important feature of the cloud — on demand resource provisioning. If we use the cloud then when we have peak load, we provision resources and as soon as the peak load is over, we would release them. Cloud is really helpful in a variety of use cases and that takes us to AWS — Amazon Web Services. AWS is the leading cloud service provider. The other two competitors are Azure Microsoft , and Google cloud provider or the GCP.
Question :- What is Elastic BeanStalk ?
Answer:- Elastic Beanstalk aims to helps us to get started with the cloud. It helps us to quickly deploy our application with a very very simple interface. Elastic Beanstalk helps us to deploy a variety of applications on a variety of platforms. We can deploy applications to tomcat. We can run Java applications, Python applications nodeJS applications, and a variety of other apps as well. In this blog, we will deploy Rest API web applications, full stack applications using Elastic Beanstalk.
***********Deploying sample application to AWS EBS************
Question :- Let’s demonstrate the HandsOn with Elastic BeanStalk ?
Answer:-
Step#1.) Let’s use the region U.S. East-North-Virginia for now. Elastic Beanstalk helps us to deploy, monitor, and scale an application quickly. Let’s for now create a simple EBS application.
Finally, we have an Application being created now :-
Step#2.) There is nothing deployed yet. To create a deployment, you need to create something called an Environment. The concept is very simple — we have an application and we can have multiple environments for that specific application. So we can have a Dev environment, a QA environment, a UAT environment or a production environment.
Step#3.) Now I’m choosing what kind of an application environment I would want. Do we want to deploy a web application, or do we want to deploy a batch application? The best option to choose is web server environment. If we want to create a batch environment, we choose a worker Environment. What I would do is, I would choose the environment name. I would want to actually give a proper environment name always. So I choose application-name hyphen the name of the environment which is hyphenated. I leave the domain name empty. I would let Amazon reconfigure it for me.
Step#4.) The next screen is where we can actually configure what kind of a platform we would want to run our environment. Is it a Java application? Is it a python application? Is it a WAR file? Is it a container? For now the focus is on Elastic Beanstalk, so let’s
Step#5. )Next, let’s use a sample application and hit create Environment.
Step#6.) Next, we might have to wait for sometime, in order for this environment to be ready :-
Step#7.) Now, once the environment is ready, let’s explore it :-
Let’s now try to access the “sample-application” that we developed using the end-point as launched by the AWS : Ebsadityaapp1-dev.eba-kyyqk3gb.us-east-1.elasticbeanstalk.com
Question:- What all resources are created under the hoods by the afore-created EBS application, we launched ?
Answer:- Let’s now explore, what all resources are being created under the hoods, by the AWS-BeanStalk in the background :-
- EC2 :- The first service created by EBS is EC2. EC2 stands for Elastic Compute Cloud. These are the virtual servers on which our application is deployed. EC2 is basically a web service to obtain virtual resources, virtual servers in the cloud. EC2 supports a variety of resource configurations. We can configure memories, CPUs, storage, network capacity, and a variety of other things for our virtual servers. EC2 instances are charged by the amount of time they are running. So, if we keep our EC2 instance running for a long time and we exceed the free tier limit, we shall be charged. In this scenario, EBS application have launched a “t2.micro” type of instance, which is currently part of the free tier.
We can perform a variety of actions with the EC2 instance. For example we can stop, reboot, terminate the EC2 instance directly from here.
- EBS :- The Elastic-BeanStalk application also did created the an EBS (elastic block store) by default for us. If we go-to volumes, we’d see that there is one volume of 8 GB which is associated with this specific instance. So this is basically the hard disk which is attached to the virtual server. The elastic block stores serves the purpose of a typical hard disk on your machine. So an elastic block store instance is created and attached with our EC2 instance.
- Security-Group :- Elastic-BeanStalk application have also created something known as a security group with the same name as the EC2 instance. The important thing to understand is, we can use security groups to configure what kind of traffic is coming into our instance, and what kind of traffic is going out of our instance.
As soon as we terminate the Elastic Beanstalk environment, all this stuff would be automatically terminated.
Question :- How much is billing-usage of our AWS-account so far ?
***********Deploying simple microservice to AWS EBS************
Question :- Can you demonstrate the process of deploying Spring Restful Micro-service to the EBS ?
Answer :-
Step #1.) We are using Spring 2.1.0.RELEASE version throughout our blog. Here is how the controller looks like :-
Step #2.) Let’s try to access our end-point, that we have exposed from the aforesaid Spring Microservice project :-
Step #3.) Next, let’s begin by deploying the JAR, thus formed at ElasticBeanStalk environment, that we just created.
Step #4.) Once, the application (JAR-file) is successfully deployed at the EBS environment, we can try access the end-points / APIs that we have exposed from our very own plain micro-service. Here is one end-point, that we can see by trying to access the same. Note that, below response is coming from our microservice, which has been deployed at the ElasticBeanStalk service of AWS :-
Step #5.) We can also have a look at the application logs from the environment’s section by clicking on the “Logs” sub-folder :-
There is a lot of magic behind what elastic beanstalk does, in order to make sure that, we are able to access the application.
*****Microservices on AWS EBS with H2 database connectivity*****
Question :- Can you demonstrate the process of deploying Spring Restful Micro-service with the help of H2 database to the EBS ?
Answer :-
Part #1.) We shall introduce the Entity for “Task” :-
Part #2.) We shall introduce the Repository to manage tasks :-
Part #3.) We shall introduce the Controller layer to perform CRUD WITH tasks :-
Part #4.) Following are the dependencies required for the same :-
Part #5.) Let’s also insert some initial data to the underlying datasource H2 :-
Part #6.) Now, upon running the application @ local, we can access the GET url and observe some data :-
Part #6.) Now, we can also check for specific data :-
Now, we proceed to deploy our recently developed application to the ElasticBeanStalk. Let’s use the region Ohio i.e. “us-east-2” for now. Elastic Beanstalk helps us to deploy, monitor, and scale an application quickly.
Step #1.) Let’s for now create a simple EBS application. The idea is very simple i.e. We have an Application within EBS and within that application, we can have multiple environments like :-
- DEV environment.
- QA environment.
- UAT environment.
- PROD environment.
Step #2.) There is nothing deployed yet. To create a deployment, you need to create something called an Environment.
Step #3.) Now I’m choosing what kind of an application environment I would want.
- If we want to create a batch environment, we choose a worker Environment.
- If we want to deploy a web application, then we shall choose web-server-environment.
Step #4.) Now What I would do is, I would choose the environment name. I would want to actually give a proper environment name always. So I choose dev as the name of environment. I leave the domain name empty. I would let Amazon reconfigure it for me.
Step #5.) The next screen is, where we can actually configure what kind of a platform we would want to run our environment. Is it a Java application? Is it a python application? Is it a WAR file? Is it a container? For now the focus is on Elastic Beanstalk, so let’s choose Java :-
Step #6.) Next, let’s use a sample application and hit create Environment. Next, we might have to wait for sometime, in order for this environment to be ready. Now, once the environment is ready, we shall explore the same.
Step #7.) Next, let’s check, whether our environment got created succesfully or not :-
Step #8.) Next, let’s go ahead and deploy our micro-service JAR to this EBS environment.
Step #9.) Let’s verify whether deployment of our JAR is successful now or not ?
Step #10.) Let’s verify whether the end-point exposed by our micro-services are working fine ?
Step #11.) Verifying another endpoint of GET type, exposed by our micro-service :-
Step #12.) Verifying another endpoint of POST type, exposed by our micro-service :-
curl --location --request POST 'http://microserviceebs-dev.eba-rxbhveqq.us-east-2.elasticbeanstalk.com//users/govind/tasks' \
--header 'Content-Type: application/json' \
--data-raw '{
"username": "govind",
"description": "Learn to Drive a Car",
"targetDate": "2030-11-09T10:49:23.566+0000",
"done": false
}'
Step #13.) Let’s cross-verify once, whether the newly created task has been duly created now or not, by hitting to GET end-point :-
Step #14.) Here is the view from h2-database, which again we can access from the end-point itself :-
Question :- Let’s summarise, whatever we have learnt so far ?
Answer :- So now, you have application and you have an application environment (dev). What do you do in the environment, is you keep updating and deploying new versions of your application. Now, let’s say I have deployed a specific version of the application in here, and I would want to go back to a previous version, or let’s say I want to actually take this and deploy it into a new environment, say QA. So I’ve tested the application it looks fine. I would want to promote it to QA.
Question :- What does Application-versions signifies ?
Answer :-
- The other interesting thing you’d see here is application versions. Basically all the versions of the applications that we have uploaded until now, while we have deployed and below are stored in here.
- And it also shows current environment where a specific thing is deployed to ? So, it shows that this jar version label is deployed to this, and version.
Recall that, version that we specified, while deploying our h2 JPA powered micro-service, we wilfully gave the version of “EBS-MS-2”.
Question :- Let’s go ahead and create a fresh new environment i.e. QA for our Quality-Analyst team ?
Step #1.) Let’s go-to Applications and start creating a new environment :-
Step #2.) We would name this environment as: “Microserviceebs-QA”. Note that, the name of environment is case-sensitive.
Step #3.) Next, we would be deploying sample application to our this newly launched QA environment. Now, we go back to the Applications and observe that, we have got 2 environments there :-
Step #4.) Next, let’s verify whether the newly launched environment is up & working fine ?
Question:- Let’s now verify from “Application Versions” that, which all application-versions are being deployed to which all environments :-
From afore-pasted screenshot, it’s clear that, under our application “microservice-ebs”, following are the versions we have :-
- Version-Label “Sample Application” is running on the newly launched “Microserviceebs-QA” environment.
- Version-Label “EBS-MS-H2-JPA-REST-1” is running onto our old “Microserviceebs-dev” environment.
Question:- Let’s say that, we want to go ahead and deploy the newest version “EBS-MS-H2-JPA-REST-1” even to the newly launched QA environment as well ?
Step #1.) Let’s goto Application-versions and click on actions tab :-
Step #2.) Now, select the Environment from the drop-down. We want the Application version “EBS-MS-H2-JPA-REST-1” to the “Microserviceebs-QA” environment :-
Step #3.) Finally, click on the Deploy button :-
Step #4.) Let’s verify, now the versions again. So, we can see that the Application Version “EBS-MS-H2-JPA-REST-1” is now being deployed to the “Microserviceebs-QA” environment along with “Microserviceebs-dev” environment too.
Step #5.) Let’s now verify our application, whether this is accessible from the QA environment as well, by hitting to the endpoint of QA environment ?
Question:- What should we take care, from the costing prospective ?
Answer:- Make sure to terminate the particular environment, as it can eat the free-tier resources very fastly. The good thing is, we can restore the environment quickly again.
Question:- Can we delete the application and restore it well ?
Answer:- NO. Unfortunately, we can’t restore the application back, but we can very well restore the environment, once deleted.
Question:- Let’s go ahead and terminate the newly launched QA environment ?
Answer:- We saw that this creates a EC2 instance for us, so a virtual server was created where the application is deployed. We saw that a public IP was assigned to us other than these two. There are a lot of other resources which are created, and all those resources would be released when we terminate the environment.
Question:- Let’s verify, which all active environments do we have right now :-
Question:- Let’s check, which all logs, do we have for this particular environment :-
Answer:- Let’s first download the logs from our DEV environment :-
If you extend the log-folder, you would see 2 folders in here :
healthd → It’s basically a daemon. So healthd daemon is running on your EC2 instance which is always checking the health of your EC2 instance. It keep supporting the health of it, so that it can send alerts on the monitoring.
nginx → The next one is nginx. nginx is the reverse proxy. If you remember we deployed the elastic beanstalk application to run on port 5000. However, the way we used to access the application did not have any port in it, because we were accessing the application directly hitting that application right. What happens is :-
- nginx receives this request on port 80 and it would redirect it to the application which is running in the background, on port 5000.
- So nginx does that, and you can see all the logs for nginx. In here, you can see all the access logs in here.
- You would see that all the records that we were sending in , you can see them in here. You can see the request, you can see what was the status of the request, and all that fun stuff.
- They are available containing any errors that happen, this would be useful when you debug stuff.
- If somebody is not working, you maybe you’d be able to start with nginx log first. The logs are rotated in regular intervals, and they are put into the specific folder which is rotated.
cfn logs → You’d see a huge set of logs in here, which have the word cfn, and so you’d do cfn, and open in it in it command line, and you’d also see a couple of files in here. In it we see event.init log. These are all the cloud formation logs. This are related to the IAAC (Infrastructure As Code). These are the log related to that activity like creating an environment , updating URLs, new documents and all that fun stuff.
Web-Application logs → Here are our actual web-application logs from our micro-service :-
Question:- What’s the relationship between CFN and EBS ?
Answer:- Cloud-Formation is amazing infrastructure as code tool and Elastic Bean Stalk uses that in the background. So wherever we are creating an environment using elastic beanstalk, what happens is elastic beanstalk a cloud formation template, and it uses the cloud formation template to create the environment
That’s all in this section. If you liked reading this blog, kindly do press on clap button multiple times, to indicate your appreciation. We would see you in next series.
References :-
- https://adityagoel123.medium.com/deep-dive-into-aws-for-developers-part1-ef923e714326
- https://aws.amazon.com/elasticbeanstalk/
- https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/Welcome.html
- https://aws.amazon.com/elasticbeanstalk/faqs/
- https://stackoverflow.com/questions/18732646/name-attribute-in-entity-and-table
- https://www.baeldung.com/jpa-entity-table-names
- https://www.baeldung.com/jpa-entities#:~:text=Entities%20in%20JPA%20are%20nothing,a%20row%20in%20the%20table.
- https://stackoverflow.com/questions/38602580/spring-boot-application-is-failing-to-insert-data
- https://stackoverflow.com/questions/3855127/find-and-kill-process-locking-port-3000-on-mac