Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Using CloudFormation to Set Up Scalable Apps

DZone's Guide to

Using CloudFormation to Set Up Scalable Apps

If you want to get started with horizontal scaling, AWS Beanstalk can make it happen. But CloudFormation will get your bucket and template running the way you want.

· Cloud Zone
Free Resource

Site24x7 - Full stack It Infrastructure Monitoring from the cloud. Sign up for free trial.

AWS Beanstalk is an Amazon Web Service that does most of the configuration for you and creates an infrastructure suitable for a horizontally scalable application. Instead of Beanstalk, the other approach would be to configure load balancers and auto-scaling groups, which requires a bit of AWS expertise and time.

In this tutorial, we are going to upload a Spring Boot JAR application using Amazon Elastic Beanstalk and a CloudFormation bundle.

Less is more, therefore we are going to use pretty much the same Spring Boot application taken from the official Spring guide as a template.

The only change would be to alter rootProject.name to beanstalk-deployment and some changes to the package structure. Downloading the project from GitHub is sufficient.

Then we can build and run the project:

 gradlew build 
 java -jar build/libs/beanstalk-deployment-1.0-SNAPSHOT.jar 


The next step is to upload the application to S3.

 aws s3 cp build/libs/beanstalk-deployment-1.0-SNAPSHOT.jar s3://{you bucket name}/beanstalk-deployment-1.0-SNAPSHOT.jar 


You need to install the Elastic Beanstalk client, as it helps a lot with most Beanstalk operations.

Since we will use Java 8, I would get a list with Elastic Beanstalk environments in order to retrieve the correct SolutionStackName.

 aws elasticbeanstalk list-available-solution-stacks |grep Java 


Based on the results, I will use the “64bit Amazon Linux 2016.09 v2.3.0 running Java 8” stackname.

Now we are ready to proceed to our CloudFormation script.

We will specify a parameter and this will be the bucket containing the application code:

 "Parameters" : {
   "SourceCodeBucket" : { 
     "Type" : "String" 
   }
 } 


Then we will specify the name of the application:

 "SpringBootApplication": { 
   "Type": "AWS::ElasticBeanstalk::Application", 
   "Properties": { 
     "Description":"Spring boot and elastic beanstalk" 
   } 
 } 


Next step will be to specify the application version:

 "SpringBootApplicationVersion": { 
   "Type": "AWS::ElasticBeanstalk::ApplicationVersion", 
   "Properties": { 
     "ApplicationName":{
       "Ref":"SpringBootApplication"
     }, 
     "SourceBundle": { 
       "S3Bucket": {
         "Ref":"SourceCodeBucket"
       }, 
       "S3Key": "beanstalk-deployment-1.0-SNAPSHOT.jar" 
     } 
   } 
 } 


And then we specify our configuration template:

 "SpringBootBeanStalkConfigurationTemplate": { 
   "Type": "AWS::ElasticBeanstalk::ConfigurationTemplate", "Properties": { 
     "ApplicationName": {
       "Ref":"SpringBootApplication"
     }, 
     "Description":"A display of speed boot application", "OptionSettings": [ {
       "Namespace": "aws:autoscaling:asg", 
       "OptionName": "MinSize", 
       "Value": "2" 
     }, { 
       "Namespace": "aws:autoscaling:asg", 
       "OptionName": "MaxSize", 
       "Value": "2" 
     }, { 
       "Namespace": "aws:elasticbeanstalk:environment", 
       "OptionName": "EnvironmentType", 
       "Value": "LoadBalanced" 
     }], 
     "SolutionStackName": "64bit Amazon Linux 2016.09 v2.3.0 running Java 8" 
   } 
 } 


The last step would be to glue the above properties by defining an environment:

 "SpringBootBeanstalkEnvironment": { 
 "Type": "AWS::ElasticBeanstalk::Environment", 
 "Properties": { 
   "ApplicationName": {
     "Ref":"SpringBootApplication"
   }, 
   "EnvironmentName":"JavaBeanstalkEnvironment", 
   "TemplateName": {
     "Ref":"SpringBootBeanStalkConfigurationTemplate"
   }, 
   "VersionLabel": {
     "Ref": "SpringBootApplicationVersion"
   } 
 } 
 } 


Now you are ready to upload your CloudFormation template and deploy your Beanstalk application:

aws s3 cp beanstalkspring.template s3://{bucket with templates}/beanstalkspring.template 
aws cloudformation create-stack --stack-name SpringBeanStalk --parameters ParameterKey=SourceCodeBucket,ParameterValue={bucket with code} --template-url https://s3.amazonaws.com/{bucket with templates}/beanstalkspring.template 


You can download the full source code and the CloudFormation template from GitHub.

Site24x7 - Full stack It Infrastructure Monitoring from the cloud. Sign up for free trial.

Topics:
spring boot ,cloudformation ,aws beanstalk ,cloud ,horizontal scaling

Published at DZone with permission of Emmanouil Gkatziouras, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}