[Java] [CodePipeline x Elastic Beanstalk] CI/CD Java application to Elastic Beanstalk with CodePipeline Part 2

7 minute read

It is a hands-on to CI/CD a Java application (obtaining a value from a database and returning the result in JSON format) to Elastic Beanstalk with CodePipeline. [CodePipeline × Elastic Beanstalk] CI/CD of Java application to Elastic Beanstalk with CodePipeline Part 1 Is the continuation of.

Environment

  • OS: Windows10
  • IDE: Eclipse 2020-03
  • JDK: Amazon Correto 8
  • Framework: Spring Boot
  • AWS -CodePipeline -CodeCommit -CodeBuild -CodeDeploy -Elastic Beantalk -Java SE (Java 8 version 2.10.8)
    → You can follow the same procedure for different platform branches (Corretto 11, etc.). -RDS: MySQL Community Edition (Version 8.0.17)

Procedure

3. Create Elastic Beanstalk environment

Next, create an Elastic Beanstalk environment. Environment creation → If you start EC2, you will be charged for it, so if possible, it is best to run to the pipeline creation in step 5 at once! If you feel a little tired from steps 1 and 2, take a rest and wait until your energy is restored.

Let’s go!

[1] Create new environment and application

(1) Log in to the AWS Management Console, find and click “Services” (1)> “Elastic Beanstalk”. image.png

(2) Click “Environment” in the navigation pane. image.png

(3) Click “Create New Environment”. image.png

(4) Select “Web server environment” (1) and click “Select” (2). image.png

(5) Move to the “Create Web Server Environment” page. Enter/set as follows and click “Set more options” (⑨).

No Name Settings
Application name sample-eb-java
Environment name Application name is automatically entered (*1)
Platform Java
Platform branch Java 8 running on 64bit Amazon Linux
Platform version 2.10.9: Recommended
Application Code Select “Upload Code”
Source code source Select “Local File”
Select file Upload the jar file created in “1-[4] Create jar file” (*2)

image.png

*1: If an environment with the same name already exists, “-n” (n is a number) will be added after the environment name. *2: If Java 8 is selected as the platform branch, From How to start Java with Elastic Beanstalk | aws, You can get “java-se-jetty-gradle-v3.zip” and use it.

(6) Move to the option setting screen. The whole picture is as below. image.png

This time,

  • ①: Software
  • ②: Network
  • ③: Database

Put your hands on the three.

(7) First, from (1) software. Check “Enable” of “Log Streaming” and click “Save” at the bottom of the screen. This will allow you to see the logs, which can be useful in investigating the cause of errors in your environment. image.png

(8) Next, ② Network. Check the check box to the left of Public IP Address (1) and Availability Zone (2) and click “Save” (3).

image.png

(9) Finally the database. Set the user name (1) and password (2) and click Save (3). image.png

(10) Click “Create environment” on the “Set more options” screen

(11) When the environment is created in about 10 minutes, let’s confirm that “health” is “OK”. image.png

(12) If the “health” is yellow (Warning) or red (Degraded, Severe), identify and solve the cause. You can check the contents by clicking “Log” or “Health” in the navigation pane.

[2] Additional settings

(1) Let’s make additional settings. Click “Settings” in the Elastic Beanstalk navigation pane. image.png

(2) Click “Edit” on the right side of “Instance”. image.png

(3) Check “Default” (1) and click “Apply” (2). [Important] If you forget, communication will not work and it will be 502 (Bad Gateway), so be sure to set it. → It was fine without this setting. 502 (Bad Gateway) seems to be caused by a mistake in setting the port number of the server. image.png

4. Database settings

Next, let’s set up the database (RDS) created by 3. Creating the Elastic Beanstalk environment.

[1] Connection setting in RDS

(1) In Management Console, search for RDS and click it. image.png

(2) Click “Database” in the navigation pane. image.png

(3) Check the radio button of the created database instance (①) and click “Change” (②). image.png

(4) The screen will change to the database instance change screen. In the “Network & Security” column, set “Public accessibility” to “Yes” and click “Next” at the bottom of the screen. image.png

(5) After transitioning to the “Summary of changes” screen, check the contents, select “Apply immediately” from “Schedule of changes” (1), and click “Change DB instance” (2). image.png(6) After returning to the “Database” screen, click the link above the database instance name. image.png

(7) Click “VPC Security Group” (link) on the “Connections & Security” tab. image.png

(8) After moving to the “Security Group” screen, click the “Inbound Rules” tab (1) and then “Edit Inbound Rules” (2). image.png

(9) Select “Any place” (1) and click “Save rules” (2). [Caution] This setting is not good for security because it allows access from all connection sources. This time, we will use the wireless LAN to connect from the client PC, so this setting is used. image.png

[2] Create table

(1) Next, let’s create a table from the client PC, also for checking the connection. Start the command prompt and execute the following command to connect to the database instance.

Execution command


> mysql -h host name -P port number -u user name -p

Here, the host name is the “endpoint” (1) of the following capture, the port number is “port” (2: default is 3306), and the user name is the “user name” set in 3. Creating the Elastic Beanstalk environment. Will be (admin this time).

image.png

After the command is executed, you will be asked for the password. Enter the “password” set in Creating the Elastic Beanstalk environment and press Enter.

After logging in, execute the following DDL and SQL to create a database and table, and insert data.

create database and table


CREATE DATABASE `sample-db` CHARACTER SET = utf8mb4 COLLATE = utf8mb4_bin;

USE `sample-db`;

CREATE TABLE `shop_informations` (
  `shop_id` mediumint(9) NOT NULL AUTO_INCREMENT COMMENT'Store ID',
  `shop_name` varchar(100) DEFAULT NULL COMMENT'store name',
  `tel` varchar(15) DEFAULT NULL COMMENT'phone number',
  `zip_code` varchar(10) DEFAULT NULL COMMENT'zip code',
  `address` varchar(100) DEFAULT NULL COMMENT'address',
  `access` varchar(100) DEFAULT NULL COMMENT'access',
  `business_hour` varchar(100) DEFAULT NULL COMMENT'business hours',
  `regular_holiday` varchar(100) DEFAULT NULL COMMENT'regular holiday',
  PRIMARY KEY (`shop_id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COMMENT='Store Information';

insert data


begin;

INSERT INTO shop_informations VALUES(1,'Harufu Cafe', '03-XXXX-XXXX', '100-0005','Marunouchi, Chiyoda-ku, Tokyo','5 minutes walk from Tokyo Station','Weekdays 7: 00-21:00 / Saturdays, Sundays, and holidays 10:00-21:00','irregular holidays');
INSERT INTO shop_informations VALUES(2,'Natsumi Shokudo', '050-XXXX-XXXX', '254-0034',' Kanagawa Prefecture Takaramachi...',' 10 minutes walk from Hiratsuka Station',' Weekdays 11:00~ 22:00 / Saturdays, Sundays, and holidays 11:00-20:00','Tuesday');
INSERT INTO shop_informations VALUES(3,'Clearance Akiyama', '049-XXXX-XXXX', '350-0041','Rokugencho, Kawagoe City, Saitama Prefecture',' 7 minutes walk from Kawagoeshi Station / From Honkawagoe Station 5 minutes walk','Lunch 11:00-14:00/Night 18:00-23:30','Sunday/Holiday');
INSERT INTO shop_informations VALUES(4,'Winter Sky Kitchen', '04-XXXX-XXXX', '350-0041','Honmachi, Abiko-shi, Chiba・・・','8 minutes walk from Abiko Station','Lunch 11: 30-14:00/Night 17:00-23:00','Thursday');

commit;

*The above data is fictitious. It is not a real store.

[3] Elastic Beanstalk settings

(2) Next, in Elastic Beanstalk, add environment variables for database connection. In the Management Console, find Elastic Beanstalk and click on it. image.png

(3) Click “Edit” at the right end of “Software”. image.png

(4) In the “Environment Properties” column, set as follows (1) and click “Apply” (2).

Name Value
DB_HOST Host name entered in (10)
DB_PORT Port number entered in (10)
DB_NAME Database name created in (10)
DB_USER User name entered in (10)
DB_PASSWORD Password entered in (10)
SERVER_PORT 5000

image.png

(5) Rewrite the contents of application.yml created in “1. Creating a Java (Spring Boot) application” as follows.

application.yml


spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://${DB_HOST:(10) entered host name}:${DB_PORT:(10) entered port number}/${DB_NAME:(10) created database name}? serverTimezone=JST
    username: ${DB_USERNAME: Username entered in (10)}
    password: ${DB_PASSWORD: Password entered in (10)}
  jpa:
    database: MYSQL
    hibernate.ddl-auto: none
server:
  port: ${SERVER_PORT:5000}

By doing this, you will be able to connect to the database created this time from the client PC.

(6) In Eclipse, right-click on the created project (1) and click “Run” (2)> “Spring Boot Application” (3) to start the local server. image.png

(7) Launch the browser and hit the following URL to check that the results can be obtained.

Execution URL


http://localhost:5000/shop-information/1

(8) Reflect the changed contents of application.yml in the remote repository (CodeCommit) of Git. Start the command prompt, move to the project root directory, and execute the commands in the following order.

> git add application.yml
> git commit -m "fix application.yml"

About the sequel

The continuation is

At.

  1. Make the connection settings for the pipeline creation and 6. Clean up.

Change log

  • 2020/08/13: [1] Environment of “3. Creating Elastic Beanstalk environment”, new application creation, [2] Partially changed the contents of additional settings

Reference

Procedure

3. Creating an Elastic Beanstalk environment

  • [Create Elastic Beanstalk environment aws](https://docs.aws.amazon.com/ja_jp/elasticbeanstalk/latest/dg/using-features.environments.html)-[Statecolorandstatus aws](https://docs.aws.amazon.com/ja_jp/elasticbeanstalk/latest/dg/health-enhanced-status.html)
  • How do I resolve yellow or red health status alerts in my Elastic Beanstalk environment? aws](https://aws.amazon.com/jp/premiumsupport/knowledge-center/elastic-beanstalk-yellow-warning/)
  • [[AWS, VPC] Story that learned mechanism of default security group while fighting communication error Qiita](https://qiita.com/Ushinji/items/6b17717d4f48ca50aa58)

4. Database connection settings

  • [How do I resolve connection issues to my Amazon RDS instance that uses public or private subnets in my VPC? aws](https://aws.amazon.com/us/premiumsupport/knowledge-center/rds-connectivity-instance-subnet-vpc/)