Deploy JAR file to Elastic Beanstalk using EB CLI
Tags: java, aws, devops, tutorial
Published: 2020-05-31 19:14:27

Like me, you also probably deploy your applications by uploading it's JAR to Elastic Beanstalk. Since the AWS Web UI allows you to directly upload a JAR file and deploy it, like me, you'd probably find a hard time finding a CLI alternative to directly deploy your JAR. 

There are 2 ways you can deploy your JAR into EB via CLI.
1. Using the [AWS EB CLI](https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/eb-cli3.html): The EB CLI is preferrable if you intend to run the command yourself. Most commands are short and sweet.
2. Using the [AWS CLI 2](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html): The AWS CLI is much more powerful and be used to carry out almost all tasks that you'd normally do through the web console, but commands are longer and not quite friendly if you intend to type in these commands every time.

This tutorial is to explain the first method which is the more straightforward option.

## Deploy using EB CLI

To deploying your JAR using the EB CLI is quite straightforward, you need to install EB CLI
####Pre-requisites
1. Install EB CLI
2. Generate an IAM User with EB Full Access.

####Configure Credentials for EB CLI
You can `cd` into your project's route directory and run `eb init` which would run you through inputting the credentials, the default region, selecting your environment etc.

```
$ eb init

Select a default region
1) us-east-1 : US East (N. Virginia)
2) us-west-1 : US West (N. California)
3) us-west-2 : US West (Oregon)
4) eu-west-1 : EU (Ireland)
5) eu-central-1 : EU (Frankfurt)
6) ap-south-1 : Asia Pacific (Mumbai)
7) ap-southeast-1 : Asia Pacific (Singapore)
8) ap-southeast-2 : Asia Pacific (Sydney)
9) ap-northeast-1 : Asia Pacific (Tokyo)
10) ap-northeast-2 : Asia Pacific (Seoul)
11) sa-east-1 : South America (Sao Paulo)
12) cn-north-1 : China (Beijing)
13) cn-northwest-1 : China (Ningxia)
14) us-east-2 : US East (Ohio)
15) ca-central-1 : Canada (Central)
16) eu-west-2 : EU (London)
17) eu-west-3 : EU (Paris)
18) eu-north-1 : EU (Stockholm)
19) eu-south-1 : EU (Milano)
20) ap-east-1 : Asia Pacific (Hong Kong)
21) me-south-1 : Middle East (Bahrain)
22) af-south-1 : Africa (Cape Town)
(default is 3): [enter your region]
```
After selecting your region, it prompts for your IAM Credentials

```
You have not yet set up your credentials or your credentials are incorrect.
You must provide your credentials.
(aws-access-id): AKIAJOUAASEXAMPLE
(aws-secret-key): 5ZRIrtTM4ciIAvd4EXAMPLEDtm+PiPSzpoK
```
After this it will ask you to select your application
```
Select an application to use
1) your-app-name
2) [ Create new Application ]
(default is 1): 
```
After this they'd ask if you'd like to use CodeCommit(the AWS VCS service). Since we're deploying JARs, it's not quite relevant for use, so you could select no.
```
Do you wish to continue with CodeCommit? (y/N) (default is n): n

```

If you need to run the same config at a different location(CI/CD Services), you could just add the ENV variables `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY`


#### Deploy Config
In order to declare the artifact(JAR) that you need to deploy,you can append  the newly generated `/.elasticbeanstalk/config.yml`, with the following

```
deploy:
  artifact: target/<your-jar-name>.jar
```

#### Deploy to EB

Now that you're done configuration, you could directly use the command `eb deploy -v` to deploy your project JAR.(-v for verbose)

```
$ eb deploy -v
INFO: Deploying code to <your-env> in region us-west-2 
INFO: Uploading archive to s3 location: <your-app-name>/<your-jar-name>.jar 
Uploading: [##################################################] 100% Done... 
INFO: Creating AppVersion <app-version>
2020-05-31 17:41:40    INFO    Environment update is starting.      
2020-05-31 17:41:45    INFO    Deploying new version to instance(s).
2020-05-31 17:41:54    INFO    New application version was deployed to running EC2 instances.
2020-05-31 17:41:54    INFO    Environment update completed successfully.
 
```

Kudos! you've successfully deployed your application to EB. Now in order to deploy consecutive versions, you could simply run `eb deploy -v`