[Elastic Beanstalk] Springboot Gradle Docker Elastic Beanstalk

Posted by Jarxi on 2020-11-07

Deploy Dockered Gradled Spring Boot Application to Elastic Beanstalk

Source code

After download, change /build/docker/Dockerfile first line to FROM frolvlad/alpine-java

Preparation

Java 8
Docker downloaded and started
AWS Cli version 2

Build

./gradlew clean build

Build Docker

./gradlew clean buildDocker

Run

java -jar build/libs/spring-boot-docker-beanstalk.jar

Run Docker

Run the spring boot app locally

docker run -p 8080:8080 -t springboot-docker-beanstalk

Tag the Docker image

Tag the docker image and point at remote registry
docker tag springboot-docker-beanstalk <accountid>.dkr.ecr.<your-default-region>.amazonaws.com/springboot-docker-beanstalk

Docker Login to AWS ECR

Give docker permissions to access AWS ECR

AWS Cli Version 2:aws ecr get-login-password

Run the output from the previous command
docker login -u AWS -p <password> https://<accountid>.dkr.ecr.<your-default-region>.amazonaws.com

Create Repository

Create the repository in the ECR for our application.

aws ecr create-repository --repository-name springboot-docker-beanstalk

Push to AWS ECR

Push the docker image to ECR.

docker push <accountid>.dkr.ecr.<your-default-region>.amazonaws.com/springboot-docker-beanstalk:latest

Ensure your image is in the repository.

aws ecr describe-images --registry-id <registry-id (same as aws accountid)> --repository-name springboot-docker-beanstalk

Create the Beanstalk Application

Initialize the project with AWS CLI. It should ask what kind of environment you want to create. Select Docker single instance.

eb init

This will ask you a series of questions as shown below, and the output should create a directory .elasticbeanstalk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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) us-east-2 : US East (Ohio)
14) ca-central-1 : Canada (Central)
15) eu-west-2 : EU (London)
(default is 3): 1

Select an application to use
1) retail-dev
2) trip-lookup
3) ENS Delivery
4) DPPI
5) sabre-proxy-api
6) [ Create new Application ]
(default is 6): 6

Enter Application Name
(default is "springboot-docker-beanstalk"):
Application springboot-docker-beanstalk has been created.

It appears you are using Docker. Is this correct?
(Y/n): Y
Select a platform branch.
1) Docker running on 64bit Amazon Linux 2
2) Multi-container Docker running on 64bit Amazon Linux
3) Docker running on 64bit Amazon Linux
(default is 1): 1

Do you wish to continue with CodeCommit? (Y/n): n
Do you want to set up SSH for your instances?
(Y/n): Y

Select a keypair.
1) hackintosh
2) [ Create new KeyPair ]

(default is 1): 1

Create Environment

This will create a new environment where the docker container will be deployed

eb create

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
Enter Environment Name
(default is springboot-docker-beanstalk-dev):
Enter DNS CNAME prefix
(default is springboot-docker-beanstalk-dev):

Select a load balancer type
1) classic
2) application
3) network
(default is 2): 2


Would you like to enable Spot Fleet requests for this environment? (y/N): N
Creating application version archive "app-bbc6-201107_125721".
Uploading springboot-docker-beanstalk/app-bbc6-201107_125721.zip to S3. This may take a while.
Upload Complete.
Environment details for: springboot-docker-beanstalk-dev
Application name: springboot-docker-beanstalk
Region: us-east-1
Deployed Version: app-bbc6-201107_125721
Environment ID: e-bxy9pdm2pm
Platform: arn:aws:elasticbeanstalk:us-east-1::platform/Docker running on 64bit Amazon Linux 2/3.2.0
Tier: WebServer-Standard-1.0
CNAME: springboot-docker-beanstalk-dev.us-east-1.elasticbeanstalk.com
Updated: 2020-11-07 20:57:27.615000+00:00
Printing Status:
2020-11-07 20:57:26 INFO createEnvironment is starting.
2020-11-07 20:57:27 INFO Using elasticbeanstalk-us-east-1-086948765295 as Amazon S3 storage bucket for environment data.
2020-11-07 20:57:48 INFO Created target group named: arn:aws:elasticloadbalancing:us-east-1:086948765295:targetgroup/awseb-AWSEB-11P5ANPRYSLH3/6e7eb8eb9a92098f
2020-11-07 20:57:48 INFO Created security group named: sg-0e96649056b514f69
2020-11-07 20:58:04 INFO Created security group named: awseb-e-bxy9pdm2pm-stack-AWSEBSecurityGroup-SM7UDONEE3CK
2020-11-07 20:58:04 INFO Created Auto Scaling launch configuration named: awseb-e-bxy9pdm2pm-stack-AWSEBAutoScalingLaunchConfiguration-XM83M1QTO4KM
2020-11-07 20:59:05 INFO Created Auto Scaling group named: awseb-e-bxy9pdm2pm-stack-AWSEBAutoScalingGroup-TY6C6OCL3UBS
2020-11-07 20:59:05 INFO Waiting for EC2 instances to launch. This may take a few minutes.
2020-11-07 20:59:05 INFO Created Auto Scaling group policy named: arn:aws:autoscaling:us-east-1:086948765295:scalingPolicy:d7ef864b-7b23-44b9-b82a-66e41ceb0b40:autoScalingGroupName/awseb-e-bxy9pdm2pm-stack-AWSEBAutoScalingGroup-TY6C6OCL3UBS:policyName/awseb-e-bxy9pdm2pm-stack-AWSEBAutoScalingScaleDownPolicy-1VZVBQ2HEERLT
2020-11-07 20:59:06 INFO Created Auto Scaling group policy named: arn:aws:autoscaling:us-east-1:086948765295:scalingPolicy:24edff05-cc3d-4623-827b-507e259811f4:autoScalingGroupName/awseb-e-bxy9pdm2pm-stack-AWSEBAutoScalingGroup-TY6C6OCL3UBS:policyName/awseb-e-bxy9pdm2pm-stack-AWSEBAutoScalingScaleUpPolicy-SEVOCM3CQWHZ
2020-11-07 20:59:06 INFO Created CloudWatch alarm named: awseb-e-bxy9pdm2pm-stack-AWSEBCloudwatchAlarmLow-1LWQDOAK06UWW
2020-11-07 20:59:06 INFO Created CloudWatch alarm named: awseb-e-bxy9pdm2pm-stack-AWSEBCloudwatchAlarmHigh-14WQ83GRM36VB
2020-11-07 20:59:54 INFO Created load balancer named: arn:aws:elasticloadbalancing:us-east-1:086948765295:loadbalancer/app/awseb-AWSEB-NOFHTRRF2ZBX/948100976058d8b0
2020-11-07 20:59:54 INFO Created Load Balancer listener named: arn:aws:elasticloadbalancing:us-east-1:086948765295:listener/app/awseb-AWSEB-NOFHTRRF2ZBX/948100976058d8b0/4db982a4fbc1f9ce
2020-11-07 20:59:58 ERROR Instance deployment: Both 'Dockerfile' and 'Dockerrun.aws.json' are missing in your source bundle. Include at least one of them. The deployment failed.
2020-11-07 20:59:58 ERROR Instance deployment failed. For details, see 'eb-engine.log'.
2020-11-07 21:00:01 ERROR [Instance: i-0fbe6f515c79b9def] Command failed on instance. Return code: 1 Output: Engine execution has encountered an error..
2020-11-07 21:00:01 INFO Command execution completed on all instances. Summary: [Successful: 0, Failed: 1].

Give permission

The above deployment failed because ECR permission is not given to eb role.

In AWS -> IAM -> Roles - > pick the role your beanstalk is using.

In my case it was set to aws-elasticbeanstalk-ec2-role

Under Permissions for the role, attach policy: AmazonEC2ContainerRegistryReadOnly

Deploy again: eb deploy

View Application

eb open or go http://YourAppName.YourDefaultRegion.elasticbeanstalk.com/

Useful docs

https://github.com/hopsoft/relay/wiki/How-to-Deploy-Docker-apps-to-Elastic-Beanstalk

https://spring.io/guides/gs/spring-boot-docker/

http://docs.aws.amazon.com/cli/latest/reference/ecr/get-login.html

http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/eb3-create.html

https://stackoverflow.com/questions/39083768/aws-docker-deployment



支付宝打赏 微信打赏

赞赏一下