Using CloudFormation with Elastic Beanstalk – Use cases and Benefits

Short Introduction to CloudFormation

CloudFormation is an automation tool which automates the process of creating resources for your infrastructure in AWS. You can write YAML or JSON scripts (also called templates) where you specify details for each of your resources and their dependencies. Once the scripts get executed successfully all the resources mentioned in it will be created exactly as per provided configurations. In case of any failure, all the changes are rolled back.

Short Introduction to Elastic Beanstalk

AWS Elastic Beanstalk is a deployment service for your web applications and services that you developed with Java, .NET, PHP, Node.js, Python, Ruby, Go, and Docker on familiar servers such as Apache, Nginx, Passenger, and IIS. You can simply upload your code and Elastic Beanstalk automatically handles all aspects of deployment, like capacity provisioning, adding auto scaling groups and load balancers and monitoring health of your EC2 instances. Yet it also provides option to fully customize the AWS resources running your application.

Use cases for using Elastic Beanstalk and CloudFormation

Use Elastic Beanstalk when you want your applications to be deployed as quickly as possible and you do not want to waste human resources, time and effort in learning cloud infrastructure management. For example you want your applications to be up and running for presenting demos and prototypes or to perform testing. Also Elastic Beanstalk is suitable to be used when your application has smaller lifecycles. Which means, for simple and less complex applications that have quick and easy build, deployment and release process, Elastic Beanstalk is the suitable option.

For your typical complex applications (like java and .net) that have a lots of dependencies and lengthy application lifecycles you should prefer CloudFormation as this will give you complete control over your infrastructure and enables you to configure your applications and their dependencies. Important to note that CloudFormation works as fine as Elastic Beanstalk for smaller and simpler applications as well.

Use cases and Benefits of using CloudFormation in combination with Elastic Beanstalk

Both of these services can work together and provide you with a great application deployment and resource management strategy. You can use Elastic Beanstalk for deploying your application code and if there are associated resources (ASG, ELB, S3 etc.) of your applications that you need to setup, you can use CloudFormation template for that.

A common scenario is that for your small and simple applications, deployed via EBS, that still require some changes to be made in the associated resources, you should CloudFormation templates as this way you will be able to keep track of every change made to your infrastructure comparing the CloudFormation templates.

Now, consider this scenario where you have multiple web applications and each one of them requires unique resources and configuration. You want to setup an environment with all these applications deployed and each applications having separate resources. Also you want to perform, let’s say, Blue-Green deployment on that environment. The ideal setup for this would be to write separate CloudFormation template to create EBS applications for each of your application and in the same template you can mention the associated resources and their configurations. Once you have the templates executed and environment ready, to update your EBS application, you can write new template with new EBS application and resources. Once the new version application is tested you can make the proxy changes to route the traffic from old application to new application.

Example

Let’s look into a short example on how you can use both services together

1. Go to AWS console and route to CloudFormation console.

2. Select “Design Template”.

3. Select “Template” tab from the bottom of the page. Paste the following code

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Resources": {
    "sampleApplication": {
      "Type": "AWS::ElasticBeanstalk::Application",
      "Properties": {
        "Description": "AWS Elastic Beanstalk Sample Application"
      }
    },
    "sampleApplicationVersion": {
      "Type": "AWS::ElasticBeanstalk::ApplicationVersion",
      "Properties": {
        "ApplicationName": { "Ref": "sampleApplication" },
        "Description": "AWS ElasticBeanstalk Sample Application Version",
        "SourceBundle": {
          "S3Bucket": { "Fn::Join": [ "-", [ "elasticbeanstalk-samples", { "Ref": "AWS::Region" } ] ] },
          "S3Key": "php-newsample-app.zip"
        }
      }
    },
    "sampleConfigurationTemplate": {
      "Type": "AWS::ElasticBeanstalk::ConfigurationTemplate",
      "Properties": {
        "ApplicationName": { "Ref": "sampleApplication" },
        "Description": "AWS ElasticBeanstalk Sample Configuration Template",
        "OptionSettings": [
          {
            "Namespace": "aws:autoscaling:asg",
            "OptionName": "MinSize",
            "Value": "2"
          },
          {
            "Namespace": "aws:autoscaling:asg",
            "OptionName": "MaxSize",
            "Value": "6"
          },
          {
            "Namespace": "aws:elasticbeanstalk:environment",
            "OptionName": "EnvironmentType",
            "Value": "LoadBalanced"
          }
        ],
        "SolutionStackName": "64bit Amazon Linux running PHP 5.3"
      }
    },
    "sampleEnvironment": {
      "Type": "AWS::ElasticBeanstalk::Environment",
      "Properties": {
        "ApplicationName": { "Ref": "sampleApplication" },
        "Description": "AWS ElasticBeanstalk Sample Environment",
        "TemplateName": { "Ref": "sampleConfigurationTemplate" },
        "VersionLabel": { "Ref": "sampleApplicationVersion" }
      }
    }
  }
}

This sample template deploys a sample PHP web application that is stored in an Amazon S3 bucket by AWS. From the value specified to “SolutionStackName” you can see that Elastic Beanstalk environment is 64-bit Amazon Linux running PHP 5.3 that also uses an Auto Scaling Group and an Elastic Load Balancer.

Reference: Elastic Beanstalk Template Snippets

4. Click “Next” and specify details like Stack name etc. For the next steps keep all the default options and complete the creation of stack.

5. Once your stack is created, select it and select “Output” instead of “Events” from the panel at bottom. You will be able to see the access URL of your sample PHP application.

 

Muhammad Ali
Bluestack IT Solutions

 
 
Muhammad Ali