Deploying Sample Docker Container to AWS Elastic Beanstalk (PAAS)
Published: 2023-03-27 12:22:49

Elastic Beanstalk is an AWS service that leverages Amazon EC2 and S3 and deploys, manages and scales your web applications for you. It uses managed containers that support Node.js, Java, Ruby, Docker and more. There are multiple ways to deploy applications using Elastic Beanstalk. You can deploy using the management console, the CLI, or the API. The amazing thing about Elastic Beanstalk is that once you upload your application, it takes care of things like load balancing, health monitoring, scaling, and more. Check out more information on **[Elastic Beanstalk](https://aws.amazon.com/elasticbeanstalk/)** 
Overall, Elastic Beanstalk builds your docker image using your uploaded source code, and then it is deployed to EC2 instances running in an Elastic Beanstalk environment.

In this demonstration, we will:

- Create an IAM User with sufficient privileges to it to manage AWS Elastic Beanstalk environment and generate Access keys, Secret keys. These keys will help us to configure AWS CLI. Here, I have created IAM User named "eb-demo-piyush".
IMAGE
- Deploy our Docker container with Elastic Beanstalk using a single container configuration (run 1 container per EC2 instance), using the Elastic Beanstalk CLI via AWS Cloud Shell because it is prebuilt with EB CLI along with AWS CLI. Check out more information on **[AWS Cloud Shell](https://aws.amazon.com/cloudshell/)**
- Use a Dockerfile for our configuration for simplicity.
- I will be using `eu-central-1` as my AWS region.

**So, Let’s start!**

## Step 1: Lets check the EB CLI version and lets start configuring the AWS CLI via AWS Cloud shell to talk to respective AWS services via APIs

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i042stxliziafzd6aluy.PNG)

I attached `AdministratorAccess-AWSElasticBeanstalk` AWS managed policy to my IAM User "eb-demo-piyush".

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9pa4o2abp6cs0jab0zvq.PNG)

Lets check AWS CLI version and configure it with generated access and secret keys of my IAM User "eb-demo-piyush".

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/88b0y3on607d034ic1hw.PNG)

## Step 2: In order to deploy a docker container in AWS Elastic Beanstalk we need an application and dockerfile for that application. So, here we will create a simple application and its dockerfile.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lc4hfls3jlo5dv5oa125.PNG)

Paste the below code and save the file.

```
<!DOCTYPE html>
<html>
<body>
<center>
<h1> <font color="GREEN"> Don't Stop Trying, Until You Succeed in Doing!!! </font> </h1>
</center>
</body>
</html>
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/egia96g4jz84b8m8tzno.PNG)

Now we will create a dockerfile of the application.
Paste the below content in Dockerfile.

```
FROM nginx
ADD eb-app-piyush.html /usr/share/nginx/html/
EXPOSE 80
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2mwdv7y5rm7rssfmhzht.PNG)

## Step 3: Initialize your Elastic Beanstalk Environment. By initializing Elastic Beanstalk knows about what type of application you are about to deploy here.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f5cjhviwwf2cu1g9ogix.PNG)

It will ask for an Application name.
It will give options for selecting a platform branch. Here default is option 1 so, let’s move to the default one.
Now it will ask if you want to set up a SSH for your instance. I say No to it.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7k2preerw1leow4zimf5.PNG)

Initialization is done now. On AWS Management console select Elastic Beanstalk. You will see the new Application created. But, when you open it, no environment exists.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4eelj47susxkq8207yjp.PNG)

## Step 4: Lets create the environment for the application created.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a6wutn2rshzpcdvguyaf.PNG)
Enter their environment name as per your wish.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d657a1rinl0c43l13v4p.PNG)
Select the type of load balancer. I will go with the default one.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1g20hswf3vbgk5z8s3jg.PNG)
I will say No to Spot Fleet requests.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zyfz198j97aidg2hlmmm.PNG) 
Now the application version will be uploaded to S3 and it will start the process of creating the Environment.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uxrpmgkl10n07bup9s18.PNG)
This process of createEnvironment will take at least 2-3 minutes. You will see here that resources are being created like target groups, security groups etc. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1ccbt570b0jzsslcvw4q.PNG)
After some time the environment got created.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c1j15st295kc1wea66x4.PNG)
You can recheck it on Elastic Beanstalk. It is successfully created now.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4mt3fe4hm4lygdwk23ut.PNG)
You will find one EC2 instance created.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uicynvxyonxassa5doce.PNG)
Lets check S3 Bucket which got created.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/phkp5w1sb4zq5cikz87j.PNG)
You can check the status of the application by using the following command.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g8odroec70doyxsocpco.PNG)
Now, since the status of Elastic Beanstalk environment is healthy, if you want, we can browse the Application Load Balancer URL.

---
Bingo..!!! We have successfully deployed a Sample docker container based app to AWS Elastic Beanstalk (PAAS) !!!

Its time to wrap up..

**_OK, folks that’s it for this post. Have a nice day guys…… Stay tuned…..!!!!!_**

Don’t forget to like & share this post on social networks!!! I will keep on updating this blog. Please do follow me on "**_[LinkedIn](https://www.linkedin.com/in/chaudharipiyush/)_**" & my other blogs -
**_[cpiyush151 - Wordpress](https://piyushchaudhariblog.wordpress.com/)_**
**_[cpiyush151 - Hashnode](https://cpiyush151.hashnode.dev/)_**