AWS Elastic Beanstalk # 1 with Java starting from scratch-Building a Java web application environment using the EB CLI-

Overview

--With AWS ** Elastic Beanstalk **, you can build a web application execution environment with a single command without having to set up an OS or middleware on EC2. Security patches can be applied automatically, which has the advantage of lightening operations, and since the base is a combination of AWS components such as EC2, it is a very easy-to-use service that can be customized in various ways. --In this article, we will use a tool called ** EB CLI ** to build an execution environment for ** Java Web App ** with a single command. --Java web apps are not WARs that run on Tomcat, but ** Suppin web apps (JAR) ** made with Java SE. (In other words, the web application framework can be ** Spring Boot ** or ** Play framework **, and the app server can be ** Tomcat **, ** Jetty **, or ** Glassfish ** as long as you understand the concept. OK) --Also, in Sequel, you can set your own domain support, HTTPS support, HTTP → HTTPS redirect support, and Auto Scaling settings.

Environment / configuration

Create the following configuration from the command line (Since it is made from the command line, mass production and reconstruction of the environment is easy)

image.png

--App platform --AWS Elastic Beanstalk ** Java 8 ** platform --In this article, the JSP / Servlet web application is executed by Jetty9. --Cloud environment --Service: AWS Elastic Beanstalk (EC2 + Application Loadbalancer on VPC) --Region: Tokyo (ap-northeast-1) → OK even if it is not Tokyo

Source code

The full source code introduced in this article can be found here. https://github.com/riversun/java-jetty-app-on-elasticbeanstalk

Main story

Install Elastic Beanstalk Command Line Interface (EB CLI)

Elastic Beanstalk apps can also be built using the Web GUI, but considering production operations such as automation of environment construction, using the command line saves time and effort without having to perform the same operations over and over, and the app / environment Since construction / change can be done with a single command, stable operation is possible by incorporating it into CI.

1. Install EB CLI

Python is required to install the EB CLI. If you don't have Python installed on your PC https://www.anaconda.com/distribution/ Install from such as. OK if you install the latest version

If Python is installed, you can install the EB CLI with the following command

pip install awsebcli --upgrade --user

2. Check the installation completion with the following command

Check if the EB CLI is installed and available on the command line with the following command

eb --version

EB CLI 3.15.2 (Python 3.7.1)

It seems that it was installed properly

TIPS In Windows environment, eb command may not be found </ font>. That time,

C:\Users\[username]\AppData\Roaming\Python\Python37\Scripts

To the path

Make a web app in Java

Java apps created with Elastic Beanstalk are created in FAT JAR format

There are several ways to run a Java web app on Elastic Beanstalk.

--Upload war file (run on tomcat) --Upload JAR file (run in Java SE environment)

In this paper, the method of ** uploading the JAR file ** is adopted.

** JAR file ** is a thing that combines the source code of an application (Web application) made in Java + dependent files into one JAR. It is also called "** FAT JAR **" because it makes a large JAR with all one.

The merit of using JAR is that ** anything is OK ** as long as the created web application can be converted into a JAR file.

The Framework can be Play Framework, Spring Boot, Struts, JSF (of course, plain JSP / Servlet is OK), AP server, Tomcat, Jetty, Glassfish, Undertow (Wildfly).

** However, ** To create a JAR and run it with Elastic Beanstalk, there is "** Elastic Beanstalk method **", so let's take a look there.

That said, it's not difficult.

Java source code configuration for Elastic Beanstalk

Consider a Java web application that uses Jetty as a web AP server.

First, the overall picture is as follows. The place marked with "★" is the configuration for Elastic Beanstalk Others are the standard source code structure of Java Maven project

Java source code configuration for Elastic Beanstalk


elastic-beantalk-java-app
├── src/main/java ・ ・ ・ Root directory of Java source code
│   └── myserver  
│       └── StartServer.java
├── src/main/resources
│ └── webroot ・ ・ ・ Static web content and JSP root directory
│       ├── index.html
│       └── show.jsp 
├── src/main/assembly
│   └── zip.xml ・ ・ ・ ★ Describe the rules for generating ZIP files to be uploaded to elastic beanstalk
├── .elasticbeanstalk ・ ・ ・ ★ Directory for storing deployment information to Elasticbeanstalk
│   └── config.yml ・ ・ ・ ★ YAML file of deployment information to Elastic Beanstalk
├── target ・ ・ ・ Build result(jar etc.)Directory where is generated
├── Procfile ・ ・ ・ ★ Write a script to execute the JAR file on EC2 of Elastic Beanstalk
└── pom.xml ・ ・ ・ Maven configuration file

The contents of the source code and configuration files will be explained later. First let's see how this source code is built and uploaded to Elastic Beanstalk.

Structure of the ZIP file to upload to Elastic Beanstalk

In this article, I build the source code on my PC, package it in one JAR file, and generate a ZIP file including the files required for Elastic Beanstalk in addition to the JAR file.

Here, the file name of the FAT JAR created by building the Java source code is ** eb-app-jar-with-dependencies.jar **. (Generated by maven-assembly-plugin, explained later) If the ZIP file name for uploading to Elastic Beanstalk is ** my-jetty-app-eb.zip **, the content structure of the ZIP file is as follows.

my-jetty-app-eb.Contents of zip


my-jetty-app-eb.zip
├── my-jetty-app-jar-with-dependencies.jar ・ ・ ・ Java source code packaged in one JAR
└── Procfile ・ ・ ・ Script for executing JAR in EC2 of Elastic Beanstalk

There are two ZIP files, a ** JAR file ** that summarizes the sources and a ** Procfile ** that is the file in which the execution script is written. So you can deploy this ZIP file by uploading it to Elastic Beanstalk via the EB CLI. This is one of the "** Elastic Beanstalk practices **".

Next, let's look at the Java application source. In particular, I will explain mainly the part related to "** Elastic Beanstalk manners **"

How to make Java application source code

The Elastic Beanstalk environment that I'm trying to run a Java application from now on is as follows, and the points that should be taken care of as a Java Web application are ** listen to port number 5000 **

image.png

In other words, if you create a server ** program that listens on ** port number 5000 in Java, it's OK for the time being.

** Sample code ** So I put the sample code in the following repository https://github.com/riversun/java-jetty-app-on-elasticbeanstalk

This sample runs the ** Servlet / JSP / Push Notification ** feature on Jetty

If you clone this code

clone https://github.com/riversun/java-jetty-app-on-elasticbeanstalk.git

As mentioned above, the source code structure is as follows. (Some files are omitted)

Java source code configuration for Elastic Beanstalk


elastic-beantalk-java-app
├── src/main/java ・ ・ ・ Root directory of Java source code
│   └── myserver  
│       └── StartServer.java
├── src/main/resources
│ └── webroot ・ ・ ・ Static web content and JSP root directory
│       ├── index.html
│       └── show.jsp 
├── src/main/assembly
│   └── zip.xml ・ ・ ・ ★ Describe the rules for generating ZIP files to be uploaded to elastic beanstalk
├── .elasticbeanstalk ・ ・ ・ ★ Directory for storing deployment information to Elasticbeanstalk
│   └── config.yml ・ ・ ・ ★ YAML file of deployment information to Elastic Beanstalk
├── target ・ ・ ・ Build result(jar etc.)Directory where is generated
├── Procfile ・ ・ ・ ★ Write a script to execute the JAR file on EC2 of Elastic Beanstalk
└── pom.xml ・ ・ ・ Maven configuration file
  • The part marked with "★" is the configuration for Elastic Beanstalk.

The main class (entry point) as a Java application is ** StartServer.java **. If you run this locally and access http: // localhost: 5000 /, you'll see something like this: It is a sample that allows you to try simple implementations such as JSP and Servlet.

image.png

From here, the important points in the source code are explained below.

src/main/java myserver.StartServer.java

Main class. Start Jetty, host Servlet / JSP and listen on port 5000

/pom.xml

pom.xml(Excerpt)


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>org.riversun</groupId>
	<artifactId>my-jetty-app</artifactId>
	<version>1.0.0</version>
	<packaging>jar</packaging>
	<name>my-jetty-app</name>
	<description>jetty app on elastic beanstalk</description>

pom.xml


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

	<modelVersion>4.0.0</modelVersion>

	<groupId>org.riversun</groupId>
	<artifactId>my-jetty-app</artifactId>
	<version>1.0.0</version>
	<packaging>jar</packaging>
	<name>my-jetty-app</name>
	<description>jetty app on elastic beanstalk</description>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<jetty-version>9.4.19.v20190610</jetty-version>
	</properties>

	<dependencies>
		<!-- for server -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.1.0</version>
		</dependency>
		<dependency>
			<groupId>org.eclipse.jetty</groupId>
			<artifactId>jetty-annotations</artifactId>
			<version>${jetty-version}</version>
		</dependency>
		<dependency>
			<groupId>org.eclipse.jetty</groupId>
			<artifactId>jetty-webapp</artifactId>
			<version>${jetty-version}</version>
		</dependency>
		<dependency>
			<groupId>org.eclipse.jetty</groupId>
			<artifactId>apache-jsp</artifactId>
			<version>${jetty-version}</version>
			<type>jar</type>
		</dependency>

		<dependency>
			<groupId>org.eclipse.jetty</groupId>
			<artifactId>apache-jstl</artifactId>
			<version>${jetty-version}</version>
			<type>pom</type>
		</dependency>
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
			<version>2.9.9</version>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.8.1</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
					<excludes>
						<exclude>examples/**/*</exclude>
					</excludes>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-javadoc-plugin</artifactId>
				<version>3.1.0</version>
				<executions>
					<execution>
						<id>attach-javadocs</id>
						<goals>
							<goal>jar</goal>
						</goals>
					</execution>
				</executions>
				<configuration>
					<author>true</author>
					<source>1.7</source>
					<show>protected</show>
					<encoding>UTF-8</encoding>
					<charset>UTF-8</charset>
					<docencoding>UTF-8</docencoding>
					<doclint>none</doclint>
					<additionalJOption>-J-Duser.language=en</additionalJOption>
				</configuration>
			</plugin>
			<!-- add source folders -->
			<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>build-helper-maven-plugin</artifactId>
				<version>3.0.0</version>
				<executions>
					<execution>
						<phase>generate-sources</phase>
						<goals>
							<goal>add-source</goal>
						</goals>
						<configuration>
							<sources>
								<source>src/main/java</source>
							</sources>
						</configuration>
					</execution>
				</executions>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-assembly-plugin</artifactId>
				<executions>
					<!--Fat the source code-Jar-->
					<execution>
						<id>package-jar</id>
						<phase>package</phase>
						<goals>
							<goal>single</goal>
						</goals>
						<configuration>
							<appendAssemblyId>true</appendAssemblyId>
							<archive>
								<manifest>
									<mainClass>myserver.StartServer</mainClass>
								</manifest>
							</archive>
							<finalName>${project.artifactId}</finalName>
							<descriptorRefs>
								<descriptorRef>jar-with-dependencies</descriptorRef>
							</descriptorRefs>
						</configuration>
					</execution>
					<!--Zip to upload to Elastic Beanstalk(fat.Includes jars and related files)To create-->
					<execution>
						<id>package-zip</id>
						<phase>package</phase>
						<goals>
							<goal>single</goal>
						</goals>
						<configuration>
							<appendAssemblyId>true</appendAssemblyId>
							<finalName>${project.artifactId}</finalName>
							<descriptors>
								<descriptor>src/main/assembly/zip.xml</descriptor>
							</descriptors>
						</configuration>
					</execution>
				</executions>
			</plugin>
		</plugins>
		<resources>
			<resource>
				<directory>src/main/resources</directory>
			</resource>
		</resources>
	</build>
</project>

Below, I will only look at the points.

maven artifactId

The following is ** my-jetty-app ** as ** artifactId **. The value of this ** artifactId ** is also used for the file name etc. generated in the subsequent build.

	<groupId>org.riversun</groupId>
	<artifactId>my-jetty-app</artifactId>

maven-assembly-plugin settings

The following is the setting of ** maven-assembly-plugin **. maven-assembly-plugin is a maven plugin for creating jar files and zip files for distribution. This maven-assembly-plugin has two tasks.

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-assembly-plugin</artifactId>
  <executions>
    <!--Fat the source code-Jar-->
    <execution>
      <id>package-jar</id>
      <phase>package</phase>
      <goals>
        <goal>single</goal>
      </goals>
      <configuration>
        <appendAssemblyId>true</appendAssemblyId>
        <archive>
          <manifest>
            <mainClass>myserver.StartServer</mainClass>
          </manifest>
        </archive>
        <finalName>${project.artifactId}</finalName>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
      </configuration>
    </execution>
    <!--Zip to upload to Elastic Beanstalk(fat.Includes jars and related files)To create-->
    <execution>
      <id>package-zip</id>
      <phase>package</phase>
      <goals>
        <goal>single</goal>
      </goals>
      <configuration>
        <appendAssemblyId>true</appendAssemblyId>
        <finalName>${project.artifactId}</finalName>
        <descriptors>
          <descriptor>src/main/assembly/zip.xml</descriptor>
        </descriptors>
      </configuration>
    </execution>
  </executions>
</plugin>

** Part 1 JAR file generation ** First, pay attention to the settings in the first half.

  <execution>
      <id>package-jar</id>
      <phase>package</phase>
      <goals>
        <goal>single</goal>
      </goals>
      <configuration>
        <appendAssemblyId>true</appendAssemblyId>
        <archive>
          <manifest>
            <mainClass>myserver.StartServer</mainClass>
          </manifest>
        </archive>
        <finalName>${project.artifactId}</finalName>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
      </configuration>
    </execution>

What is described here is to combine Java source code into one JAR file. In other words, it is a task setting for creating a FAT JAR.

<mainClass> myserver.StartServer </ mainClass> is the start class when executing the JAR file.

<finalName> $ {project.artifactId} </ finalName> is the file name of the final JAR file specified by ** artifactId ** = ** my-jetty-app-[AssemblyId ] .jar **.

<descriptorRef> jar-with-dependencies </ descriptors </ descriptorRef> also packages the dependent libraries described in ** dependencies ** of maven as a JAR file.

<appendAssemblyId> true </ appendAssemblyId> sets whether to add AssemblyId to the file name of the generated JAR file. If this is ** true **, the JAR file name will be ** my-jetty-app-jar-with-dependencies.jar **.

To generate a JAR file under the conditions set here

mvn package

And it is sufficient. This will generate ** my-jetty-app-jar-with-dependencies.jar ** in the ** target ** directory.

** Part 2 ZIP file generation **

Next is the second half setting


   <execution>
      <id>package-zip</id>
      <phase>package</phase>
      <goals>
        <goal>single</goal>
      </goals>
      <configuration>
        <appendAssemblyId>true</appendAssemblyId>
        <finalName>${project.artifactId}</finalName>
        <descriptors>
          <descriptor>src/main/assembly/zip.xml</descriptor>
        </descriptors>
      </configuration>
    </execution>

The task described here is to combine the JAR file created above and Elastic Beanstalk related files (Procfile, etc.) into a ZIP file.

Where <finalName> $ {project.artifactId} </ finalName> is used, the file name to be generated is specified as in the case of JAR.

The ZIP generation rule is set in the external configuration file <descriptor> src / main / assembly / zip.xml </ descriptor>.

src/main/assembly/zip.xml

Now, let's take a look at the zip.xml that has been removed.

This is the generation rule when the ZIP file is finally generated.

Specify the JAR file and Procfile with <include>, and instruct how to generate the ZIP file in the format to be uploaded to Elastic Beanstalk.

zip.xml


<?xml version="1.0" encoding="UTF-8"?>
<assembly
	xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
	<id>eb</id>
	<baseDirectory>/</baseDirectory>
	<formats>
		<format>zip</format>
	</formats>
	<fileSets>
		<fileSet>
			<directory>${project.basedir}</directory>
			<outputDirectory>/</outputDirectory>
			<includes>
				<include>Procfile</include>
				<include>Buildfile</include>
				<include>.ebextensions/*</include>
			</includes>
		</fileSet>
		<fileSet>
			<directory>${project.build.directory}</directory>
			<outputDirectory>/</outputDirectory>
			<includes>
				<include>my-jetty-app-jar-with-dependencies.jar</include>
				<!-- <include>*.jar</include> -->
			</includes>
		</fileSet>
	</fileSets>
</assembly>

The file name of the finally generated ZIP file will be ** my-jetty-app- [id] .zip **. In zip.xml, <id> eb </ id> is specified, so the generated ZIP file name will be ** my-jetty-app-eb.zip **.

In summary, when you run the maven command mvn package to generate the required packages ** my-jetty-app-jar-with-dependencies.jar ** and ** my-jetty-app-eb.zip * Both * will be generated under the ** target ** directory.

We will check this again later when we actually generate the deploy file.

/Procfile

Procfile is a file for executing JAR in EC2 of Elastic Beanstalk.

The command to start the application on EC2 of Elastic Beanstalk is described under web:.

Procfile


web: java -jar my-jetty-app-jar-with-dependencies.jar

It is OK to describe the startup option like ↓

web: java -jar my-jetty-app-jar-with-dependencies.jar -Xms256m

See Official for details.

.elasticbeanstalk/config.yml

Describe deployment information in config.yml The file target / my-jetty-app-eb.zip to be deployed to Elastic Beansalk is specified here.

config.yml


deploy:
  artifact: target/my-jetty-app-eb.zip

This file is referenced when deploying to Elastic Beanstalk.

Currently, only deploy: artifact: is described, but from now on, the necessary values will be added to this ** config.yml ** in the process of deploying Elastic Beanstalk using EB CLI. I will go.

Deploy your app using the EB CLI

Now that you have a rough idea of what the files mean, deploy your app to Elastic Beanstalk.

Build an Elastic Beanstalk application on AWS using the EB CLI

** 1. Move to the app directory **

cd java-jetty-app-on-elasticbeanstalk

** 2. Make a Java box * in Elastic Beanstalk ** (* Box = application)

The EB CLI command for that is in the following format.

eb init App name --region Region name --platform Platform

Here, the app name is ** my-eb-app , the region is the Tokyo region ( ap-northeast-1 **), and the platform is ** java-8 **.

The command looks like this:

eb init my-eb-app --region ap-northeast-1 --platform java-8

Then

Application my-eb-app has been created.

Message appears, An application box is created on AWS Elastic beanstalk

If you check it on the Web console at once, a box (application) is created

https://ap-northeast-1.console.aws.amazon.com/elasticbeanstalk/home

image.png

Now, take a look at ** / .elasticbeanstalk / config.yml ** in the source code.

With the above command, ** config.yml ** has also been updated to look like this:

config.yml



branch-defaults:
  master:
    environment: null
deploy:
  artifact: target/my-jetty-app-eb.zip
global:
  application_name: my-eb-app
  branch: null
  default_ec2_keyname: null
  default_platform: java-8
  default_region: ap-northeast-1
  include_git_submodules: true
  instance_profile: null
  platform_name: null
  platform_version: null
  profile: eb-cli
  repository: null
  sc: git
  workspace_type: Application

TIPS ------------

If Credential information is not registered </ font>, you will be asked for ** aws-access-id ** and ** aws-secret-key ** as shown below, so enter it. ..

eb init my-eb-app --region ap-northeast-1 --platform java-8
You have not yet set up your credentials or your credentials are incorrect
You must provide your credentials.
(aws-access-id): xxxxx
(aws-secret-key): xxxxx
Application my-eb-app has been created.

If you enter it once, a config file will be created under the [user] /. Aws directory, so you will not be asked again.

------------

Create a package for deployment

As we saw earlier, generate a ** ZIP ** file for deployment to Elastic Beanstalk.

Since ZIP generation is done by Maven, hit the maven package command

(If you do clean as well, the command is as follows)

mvn clean package

As shown below, ** my-jetty-app-eb.zip ** was successfully generated in the ** target / ** directory.

[INFO] --- maven-assembly-plugin:2.2-beta-5:single (package-zip) @ my-jetty-app ---
[INFO] Reading assembly descriptor: src/main/assembly/zip.xml
[INFO] Building zip: jetty-app-on-eb\target\my-jetty-app-eb.zip
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 32.984 s
[INFO] Finished at: 2019-07-01T15:55:14+09:00
[INFO] Final Memory: 37M/449M
[INFO] ------------------------------------------------------------------------

Deploy to Elastic Beanstalk and build environment

From now on, deploy the created ZIP file to Elastic Beanstalk using EB CLI.

Use the ** eb create ** command to deploy to Elastic Beanstalk

** How to use the eb create command **

Specify the eb create command as follows.

eb create Environment name [Option] [Option] ・ ・ ・ [Option]

The options are:

Options Description
--cname Specify the CNAME of the URL.
Example If you specify my-jetty-app-test for CNAME,
http://my-jetty-app-test.ap-northeast-1.elasticbeanstalk.com/
Can be accessed as
--instance_type Instance type.
VPC required to specify t2 series
--elb-type Load balancer type
If you specify "application",
Become an application load balancer.
Later, when supporting your own domain or HTTPS
It's easy to have a load balancer
--vpc.id VPC ID
--vpc.elbpublic Load balancer
Put it on the public subnet
--vpc.elbsubnets Specify the subnet ID of the load balancer.
When specifying more than one, separate them with commas
--vpc.publicip Elastic Beanstalk EC2
Put it on the public subnet
--vpc.ec2subnets Elastic Beanstalk
Specify the EC2 subnet ID.
When specifying more than one, separate them with commas

For details, refer to Official

Well then From the command line:

eb create my-jetty-app-test --cname my-jetty-app-test --instance_type t2.small --elb-type application --vpc.id vpc-xxxxxxxxxxxxxxxxx --vpc.elbpublic --vpc.elbsubnets subnet-xxxxxxxxxxxxxxxxx,subnet-yyyyyyyyyyyyyyyyy --vpc.publicip --vpc.ec2subnets subnet-xxxxxxxxxxxxxxxxx,subnet-yyyyyyyyyyyyyyyyy

In the above, the actual ID etc. are dummy, but the environment name ** my-jetty-app-test **, cname is ** my-jetty-app-test ** and the EC2 instance type is ** t2.small ** , ELB (load balancer) is ** application ** load balancer, VPC ID is ** vpc-xxxxxxxxxxxxxxxxx ** and it is a command to deploy the ZIP file.

(Which ZIP file will be uploaded is specified in **. Elasticbeanstalk / config.yml **, so refer to it.)

Also, **--vpc.elbpublic ** and **--vpc.publicip ** indicate that ELB (load balancer) and EC2 of Elastic Beanstalk will be executed in the public subnet in the specified VPC. **--vpc.elbsubnets ** and **--vpc.ec2subnets ** have the same public subnets (2) respectively. The ELB cannot access the app unless it can access the public, so put it in the public subnet. On the EC2 side, there are two ways to put it on the public subnet and on the private subnet. In the example of this article, it is in the public subnet, but it can only be accessed from the ELB by the automatically generated security group. However, a public IP is assigned. (To make it more secure, you can invent such as placing EC2 in a private subnet. , This is a security issue around EC2 and VPC rather than Elastic Beanstalk, so I will omit it here.)

By the way, when the above command is executed, the process from uploading to environment construction is automatically executed as shown below.


Uploading: [##################################################] 100% Done...

Environment details for: my-jetty-app-test
  Application name: my-eb-app
  Region: ap-northeast-1
  Deployed Version: app-1d5f-190705_00000
  Environment ID: e-2abc
  Platform: arn:aws:elasticbeanstalk:ap-northeast-1::platform/Java 8 running on 64bit Amazon Linux/2.8.6
  Tier: WebServer-Standard-1.0
  CNAME: my-jetty-app-test.ap-northeast-1.elasticbeanstalk.com
  Updated: 2019-07-01 07:08:01.989000+00:00

Printing Status:
2019-07-01 07:08:00    INFO    createEnvironment is starting.
2019-07-01 07:08:02    INFO    Using elasticbeanstalk-ap-northeast-1-000000000000 as Amazon S3 storage bucket for environment data.
2019-07-01 07:08:23    INFO    Created target group named: arn:aws:elasticloadbalancing:ap-northeast-1:000000000000:targetgroup/awseb-AWSEB-LMAAAA/000000000
2019-07-01 07:08:23    INFO    Created security group named: sg-11111111111111111
2019-07-01 07:08:39    INFO    Created security group named: sg-22222222222222222
2019-07-01 07:08:39    INFO    Created Auto Scaling launch configuration named: awseb-e-xxxxxxxxxx-stack-AWSEBAutoScalingLaunchConfiguration-3V
2019-07-01 07:09:41    INFO    Created Auto Scaling group named: awseb-e-xxxxxxxxxx-stack-AWSEBAutoScalingGroup-XXXXXXXXXXXX
2019-07-01 07:09:41    INFO    Waiting for EC2 instances to launch. This may take a few minutes.
2019-07-01 07:09:41    INFO    Created Auto Scaling group policy named: arn:aws:autoscaling:ap-northeast-1:000000000000:scalingPolicy:4e:autoScalingGroupName/awseb-e-
xxxxxxxxxx-stack-AWSEBAutoScalingGroup-XXXXXXXXXXXX:policyName/awseb-e-xxxxxxxxxx-stack-AWSEBAutoScalingScaleUpPolicy-FA
2019-07-01 07:09:42    INFO    Created Auto Scaling group policy named: arn:aws:autoscaling:ap-northeast-1:000000000000:scalingPolicy:8a:autoScalingGroupName/awseb-e-
xxxxxxxxxx-stack-AWSEBAutoScalingGroup-XXXXXXXXXXXX:policyName/awseb-e-xxxxxxxxxx-stack-AWSEBAutoScalingScaleDownPolicy-SSZW
2019-07-01 07:09:57    INFO    Created CloudWatch alarm named: awseb-e-xxxxxxxxxx-stack-AWSEBCloudwatchAlarmHigh-H0N
2019-07-01 07:09:57    INFO    Created CloudWatch alarm named: awseb-e-xxxxxxxxxx-stack-AWSEBCloudwatchAlarmLow-BX
2019-07-01 07:10:34    INFO    Created load balancer named: arn:aws:elasticloadbalancing:ap-northeast-1:000000000000:loadbalancer/app/awseb-AWSEB-1BQ
2019-07-01 07:10:34    INFO    Created Load Balancer listener named: arn:aws:elasticloadbalancing:ap-northeast-1:000000000000:listener/app/awseb-AWSEB-1BQ
2019-07-01 07:11:05    INFO    Application available at my-jetty-app-test.ap-northeast-1.elasticbeanstalk.com.
2019-07-01 07:11:05    INFO    Successfully launched environment: my-jetty-app-test

Once the environment construction is started, the situation can be grasped on the Web console.

It seems that it was able to deploy

image.png

image.png

It seems that the app has been successfully deployed to http://my-jetty-app-test.ap-northeast-1.elasticbeanstalk.com/

When you access

image.png

It seems that JSP and Servlet are working properly

image.png

Summary

--Introduced the procedure for deploying a web application using Java SE to AWS Elastic Beanstalk using the EB CLI in a hands-on manner.

--As a result, we were able to build the following configuration from the command.

image.png

--Click here for all the source code introduced https://github.com/riversun/java-jetty-app-on-elasticbeanstalk.git

-Sequel "AWS Elastic Beanstalk # 2 starting from zero", about original domain support, HTTPS support, HTTP → HTTPS redirect support, Auto Scaling setting , Handle.

Recommended Posts