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

Hands-on to CI / CD Java application (gets value from database and returns result in JSON format) to Elastic Beanstalk with CodePipeline. [Code Pipeline x Elastic Beanstalk] CI / CD Java application to Elastic Beanstalk with Code Pipeline Part 1 It is a continuation of.

environment

procedure

3. Creating an Elastic Beanstalk environment

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

Let's go!

[1] 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" (①) and click "Select" (②). image.png

(5) You will be taken to the "Create Web Server Environment" page. Enter and set as follows, and click "Set more options" (9).

No name Settings
Application name sample-eb-java
Environment name When you enter the application name, it will be entered automatically (* 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 files 「1-[4]Upload the jar file created in "Creating a jar file" (* 2)

image.png

(6) Transit to the option setting screen. The whole picture is as shown in the capture below. image.png

This time,

-①: Software -②: Network --③: Database

Put your hands on the three.

(7) First, ① from software. Check "Enable" in "Streaming Logs" and click "Save" at the bottom of the screen. This will allow you to see the logs, which is useful for investigating the cause of the error in your environment. image.png

(8) Next, ② Network. Check the check box to the left of the public IP address (1) and Availability Zone (2), and click "Save" (3).

image.png

(9) Finally, the database. Set the user name (①) and password (②), and click Save (③). image.png

(10) Click "Create Environment" on the "Set more options" screen.

(11) After the environment is created in about 10 minutes, check that "Health" is "OK". image.png

(12) If "Health" turns yellow (Warning) or red (Degraded, Severe), identify and resolve 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" (①) and click "Apply" (②). ~~ [Important] If you forget it, communication will not work and it will be 502 (Bad Gateway), so be sure to set it. </ b> ~~ → It was okay without this setting. 502 (Bad Gateway) seems to be caused by a misconfiguration of the server port number. image.png

4. Database settings

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

[1] Connection settings in RDS

(1) Find and click RDS on the management console. 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. Set "Public accessibility" in the "Network & Security" column to "Yes" and click "Next" at the bottom of the screen. image.png

(5) After transitioning to the "Change Summary" screen, check the contents, select "Apply Immediately" in "Change Schedule" (1), and click "Change DB Instance" (2). image.png

(6) When you return to the "Database" screen, click the link above the database instance name. image.png

(7) On the "Connection and Security" tab, click "VPC Security Group" (link). image.png

(8) After transitioning to the "Security Group" screen, click the "Inbound Rules" tab (①) and click "Edit Inbound Rules" (②). image.png

(9) Select "Any location" (①) and click "Save rules" (②). [Caution] This setting is not good for security because it allows access from all connection sources. </ b> This time, we will use wireless LAN to connect from the client PC, so this setting is used. image.png

[2] Creating a table

(1) Next, let's create a table from the client PC, which also serves as a connection check. Start a command prompt and execute the following command to connect to the database instance.

Execution command


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

Here, the host name is the "endpoint" (1) in the capture below, the port number is the "port" (2: default is 3306), and the user name is the "user name" set in 3. Creating the Elastic Beanstalk environment. (This time admin).

image.png

After executing the command, you will be asked for the password, so enter the "password" set in Creating the Elastic Beanstalk environment and press Enter.

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

Creating databases and tables


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 'Postal code',
  `address` varchar(100) DEFAULT NULL COMMENT 'Street 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';
begin;

INSERT INTO shop_informations VALUES(1, 'Spring breeze 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', 'Takaracho, Kanagawa Prefecture ...', '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, 'Ceded Akiyama', '049-XXXX-XXXX', '350-0041', 'Rokkenmachi, Kawagoe City, Saitama Prefecture ...', '7 minutes walk from Kawagoeshi station/5 minutes walk from Honkawagoe Station', 'Noon 11:00~14:00 / night 18:00~23:30', 'Sundays and public holidays');
INSERT INTO shop_informations VALUES(4, 'Winter sky kitchen', '04-XXXX-XXXX', '350-0041', 'Honcho, Abiko City, Chiba Prefecture ...', '8 minutes walk from Abiko Station', 'Noon 11:30~14:00 / night 17:00~23:00', 'Thursday');

commit;
  • The above data is fictitious. It is not a real store.

[3] Settings in Elastic Beanstalk

(2) Next, add environment variables for database connection in Elastic Beanstalk. In the management console, find and click Elastic Beanstalk. image.png

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

(4) In the "Environment Properties" field, set as follows (①) and click "Apply" (②).

name value
DB_HOST (10)Host name entered in
DB_PORT (10)Port number entered in
DB_NAME (10)Database name created in
DB_USER (10)Username entered in
DB_PASSWORD (10)Password entered in
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)Host name entered in}:${DB_PORT:(10)Port number entered in}/${DB_NAME:(10)Database name created in}?serverTimezone=JST
    username: ${DB_USERNAME:(10)Username entered in}
    password: ${DB_PASSWORD:(10)Password entered in}
  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 (①) and click "Run" (②)> "Spring Boot Application" (③) to start the local server. image.png

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

Execution URL


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

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

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

About the sequel

The continuation

-[Code Pipeline x Elastic Beanstalk] CI / CD Java application to Elastic Beanstalk with Code Pipeline Part 3

At. 5. Set up the connection for creating the pipeline and 6. Clean up.

Change log

--2020/08/13: [1] Environment, new application creation, [2] Partial change of additional settings in "3. Creating Elastic Beanstalk environment"

reference

procedure

3. Creating an Elastic Beanstalk environment

-Create an Elastic Beanstalk environment | aws -Status color and status | aws --How do I resolve the yellow or red health status warning in an Elastic Beanstalk environment? | aws -[AWS, VPC] I learned how the default security group works while fighting communication errors | Qiita

4. Database connection settings

--[How do I resolve a connection issue to an Amazon RDS instance that uses a VPC public or private subnet? | Aws](https://aws.amazon.com/jp/premiumsupport/knowledge- center / rds-connectivity-instance-subnet-vpc /)

Recommended Posts