Connect AWS Elastic Beanstalk and СloudBees

Introduction



In this article I will try to explain how to make work together two popular cloud service for the benefit of mankind. Undoubtedly, will come the bright day, when Elastic Beanstalk is perfect and this article is not required.

Functionality which provides СloudBees which I miss in Elastic Beanstalk Free Tier


the
    the
  • MySQL DB
  • the
  • Jenkins
  • the
  • Svn, Git,
  • the
  • Deploy to CloudBees


Functionality provided by Elastic Beanstalk which I miss in СloudBees


the
    the
  • "100%" uptime
  • the
  • performance
  • the
  • monitoring, event s, and so on.


Elastic Beanstalk is undoubtedly more powerful and promising service that will fully absorb СloudBees in the future, in the meantime, make it better for yourself.



Step 1: Register Elastic Beanstalk (skip if you already have)



In the example I've created a test app on the server US East (Virginia) which, I think, significantly affected the speed of the connection and deployment of the application from Jenkins. But of course, you can try other regions.

1.1 Create a new application

image

1.2 the result is:

image

1.3 Create a new user for Elastic Beanstalk API

image

1.4 Give the user permission "AWS Elastic Beanstalk Full Access"

image

1.5 Check the S3 bucket you created to hold WAR

image

1.6 Check that the test application is running

image

Step 2: Register CloudBees (skip if you already have)



2.1 Create a new MySQL DB

image

For example, were created in the same table testdata fields id, foo, bar to demonstrate the query performance.

2.2 Create a GIT repository (only used for example to store source code)

image

2.3 to be continued... see step: 4

Step 3: Create a test web application



pom.xml


the
<?xml version="1.0"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.elasticbeanstalk.sampleapp</groupId>
<artifactId>elasticbeanstalk-sampleapp</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>elasticbeanstalk-sampleapp</name>
<url>http://Default-Environment-whahswsu23.elasticbeanstalk.com</url>

<properties>
<maven.build.timestamp.format > yyyyMMddHHmmss < /maven.build.timestamp.format>
<beanstalk.versionLabel > ${maven.build.timestamp} < /beanstalk.versionLabel>
</properties>

<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.16</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>runtime</scope> 
</dependency> 
</dependencies>

<build>
<finalName>elasticbeanstalk-sampleapp</finalName>
<plugins>
<plugin>
<groupId>br.com.ingenieux</groupId>
<artifactId>beanstalk-maven-plugin</artifactId>
<version>0.2.6</version>
<configuration>
<applicationName>My First Elastic Beanstalk Application</applicationName>
<s3Bucket > elasticbeanstalk-us-east-1-997639223855</s3Bucket>
<s3Key > ${maven.build.timestamp}-${project.build.finalName}.war < /s3Key > 
<environmentName > Default-Environment < /environmentName > 
</configuration>
</plugin>
</plugins>
</build>
</project>


Of unusual only plug-beanstalk-maven-plugin to automatically install the application on Elastic Beanstalk.

applicationName — specify a value by default from point 1.2
s3Key — the name of the war file for saving into S3, ${maven.build.timestamp} will be used as the name of the application version.
environmentName — indicated value by default from point 1.2

context.xml


the
<Context>

<Resource name="jdbc/beanstalk"
auth="Container"
type="javax.sql.DataSource"
factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
username="beanstalk"
password=""
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://ec2-50-19-213-178.compute-1.amazonaws.com:3306/beanstalk?autoReconnect=true&characterEncoding=utf8"
validationQuery="SELECT 1"
maxWait="1000"
removeAbandoned="true"
maxActive="30"
maxIdle="10"
removeAbandonedTimeout="60"
logAbandoned="true"/>

</Context> 


Describes the parameters of the connection to the database created in step 2.1.

web.xml


the
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">

<display-name>Archetype Created Web Application</display-name>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<resource-ref>
<res-ref-name>jdbc/beanstalk</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

</web-app>


index.jsp


the
<%@ page session="false" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Custom Welcome Page</title>
</head>
<body>
<p>Your <b>custom</b> your AWS Elastic Beanstalk Application is now running on your  own  dedicated environment in the AWS Cloud</p>


<br/><br/><b>testdata:</b><br/>

<jsp:useBean id="now1" class="java.util.Date" />
<sql:query dataSource="jdbc/beanstalk" var="rst" scope="request">
select id, foo, bar from testdata
</sql:query>
<c:forEach items="${rst.rows}" var="row">
${row.id} ${row.foo} ${row.bar}<br/>
</c:forEach>
<jsp:useBean id="now2" class="java.util.Date" />
<br/><br/>Time: ${now2.time - now1.time} ms.

</body>
</html>


And a test index page that runs a simple query to the database and displays a run time.

Download the project: git clone git://git.cloudbees.com/wickiup/beanstalk.git

Step 4: go Back to your CloudBees — Jenkins custom



4.1 Create a new task

image

4.2 Specify what repository to download the project (in the example from CloudBees GIT)

image

4.3 Add a Post Step to deploy the application on Elastic Beanstalk

image

aws.accessKey and aws.secretKey specify the one received in step 1.3

This step will add WAR to S3 storage, will create a new version of the app, and update the instance.

4.4 Collect project — see log

the
[INFO] Target Path: s3://elasticbeanstalk-us-east-1-997639223855/20120724145549-elasticbeanstalk-sampleapp.war
[INFO] Uploading artifact file: /scratch/hudson/workspace/beanstalk/target/elasticbeanstalk-sampleapp.war
[INFO] Artifact Uploaded
[INFO] SUCCESS
[INFO] ETag: 81af708b625c34c2b5a9b1d12057f575 [class: String]
[INFO] 
[INFO] --- beanstalk-maven-plugin:0.2.6:create-application-version (default-cli) @ elasticbeanstalk-sampleapp ---
[INFO] SUCCESS
[INFO] sourceBundle: {S3Bucket: elasticbeanstalk-us-east-1-997639223855, S3Key: 20120724145549-elasticbeanstalk-sampleapp.war, } [class: S3Location]
[INFO] versionLabel: 20120724145549 [class: String]
[INFO] description: elasticbeanstalk-sampleapp [class: String]
[INFO] applicationName: My First Elastic Beanstalk Application [class: String]
[INFO] dateCreated: Tue Jul 24 14:56:03 EDT 2012 [class: Date]
[INFO] dateUpdated: Tue Jul 24 14:56:03 EDT 2012 [class: Date]
[INFO] 
[INFO] --- beanstalk-maven-plugin:0.2.6:update-environment (default-cli) @ elasticbeanstalk-sampleapp ---
[INFO] Calling update-environment, and using versionLabel: 20120724145549
[INFO] SUCCESS
[INFO] resources: {LoadBalancer: {LoadBalancerName: awseb-Default-Environment, Domain: awseb-Default-Environment-1219914711.us-east-1.elb.amazonaws.com, Listeners: [{Protocol: http, Port: 80, }], }, } [class: EnvironmentResourcesDescription]
[INFO] versionLabel: 20120724145549 [class: String]
[INFO] status: Updating [class: String]
[INFO] applicationName: My First Elastic Beanstalk Application [class: String]
[INFO] endpointURL: awseb-Default-Environment-1219914711.us-east-1.elb.amazonaws.com [class: String]
[INFO] health: Grey [class: String]
[INFO] dateUpdated: Tue Jul 24 14:56:03 EDT 2012 [class: Date]
[INFO] environmentId: e-4hephxdqd9 [class: String]
[INFO] solutionStackName: 32bit Amazon Linux running Tomcat 7 [class: String]
[INFO] CNAME: Default-Environment-whahswsu23.elasticbeanstalk.com [class: String]
[INFO] description: This is the default environment for the sample application. [class: String]
[INFO] dateCreated: Tue Jul 24 13:10:42 EDT 2012 [class: Date]
[INFO] environmentName: Default-Environment [class: String]

[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 14.653 s
[INFO] Finished at: Tue Jul 24 14:56:04 EDT 2012
[INFO] Final Memory: 6M/15M
[INFO] ------------------------------------------------------------------------
Finished: SUCCESS


Step 5: Test it



5.1 Look in the console for Elastic Beanstalk

image

As we can see, there is a new version of the app (20120724145549) which is now active.

5.2 Look at the S3 console

image

WAR file (20120724145549-elasticbeanstalk-sampleapp.war) with the current version of the application on the spot.

5.3 Check the WEB interface

image

The query execution time is ~2 MS, that seems not bad for a Free Tier.

Documentation



AWS Free Tier
beanstalk-maven-plugin
Article based on information from habrahabr.ru

Комментарии

Популярные сообщения из этого блога

Monitoring PostgreSQL + php-fpm + nginx + disk using Zabbix

Templates ESKD and GOST 7.32 for Lyx 1.6.x

Customize your Google