Contents
  1. 1. Why AWS Elastic Beanstalk?
  2. 2. How to use Elastic Beanstalk?
    1. 2.1. Cloudformation Template
    2. 2.2. .ebextensions

Why AWS Elastic Beanstalk?

A single server instance is good for development but it would be bad for production environment because it’s unable to scale horizontally. Also it’s a waste of money to let a giant instance run 7x24 while we just have a few rushing hours in a day. Therefore, we need to be able to scale horizontally base on the network traffic, server cpu utilization percentage etc.

How to use Elastic Beanstalk?

I will create the Elastic Beanstalk environment by a cloudformation template and make specific server instance change in project artifact’s .ebextension config files.

Cloudformation Template

Let’s begin with a snippet.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
AWSTemplateFormatVersion: '2010-09-09'
Resources:
sampleApplication:
Type: AWS::ElasticBeanstalk::Application
Properties:
###################################
# Specify the Application name here
###################################
ApplicationName: Application Name(XXXXX)
Description: AWS Elastic Beanstalk Sample Application
sampleApplicationVersion:
Type: AWS::ElasticBeanstalk::ApplicationVersion
Properties:
ApplicationName:
Ref: sampleApplication
Description: AWS ElasticBeanstalk Sample Application Version
SourceBundle:
###################################
# Specify the artifact's location
###################################
S3Bucket: !Sub "elasticbeanstalk-samples-${AWS::Region}"
S3Key: php-newsample-app.zip
sampleConfigurationTemplate:
Type: AWS::ElasticBeanstalk::ConfigurationTemplate
Properties:
ApplicationName:
Ref: sampleApplication
Description: AWS ElasticBeanstalk Sample Configuration Template
###################################
# Specify the solution stack name
# The list of all solution stack can be found from https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/concepts.platforms.html
###################################
SolutionStackName: 64bit Amazon Linux 2018.03 v2.8.1 running PHP 7.0
OptionSettings:
###################################
# Specify the option settings of environment and platform
# - Environment options: https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/command-options-general.html
# - Platform specific options: https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/command-options-specific.html
###################################
- Namespace: aws:autoscaling:asg
OptionName: MinSize
Value: '2'
- Namespace: aws:autoscaling:asg
OptionName: MaxSize
Value: '6'
- Namespace: aws:elasticbeanstalk:environment
OptionName: EnvironmentType
Value: LoadBalanced
sampleEnvironment:
Type: AWS::ElasticBeanstalk::Environment
Properties:
ApplicationName:
Ref: sampleApplication
Description: AWS ElasticBeanstalk Sample Environment
TemplateName:
Ref: sampleConfigurationTemplate
VersionLabel:
Ref: sampleApplicationVersion

NOTE: If we need to have immutable auto scaling, aws:elasticbeanstalk:healthreporting:system -> System Type should be set as “enhanced”.

.ebextensions

How about the software and the script deployment in EC2 instance? How can we refer and use the environment variables in ebextensions? Here is an example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
## Install loggly
packages:
yum:
rsyslog-gnutls: []

files:
"/tmp/configure-apache.sh" :
mode: "000755"
owner: root
group: root
source: https://www.loggly.com/install/configure-apache.sh

"/opt/elasticbeanstalk/hooks/appdeploy/post/90_reindex_elastic_cache.sh" :
mode: "000755"
owner: root
group: root
content: |
#!/usr/bin/env bash
/tmp/configure-apache.sh -a `{"Fn::GetOptionSetting": {"Namespace": "aws:elasticbeanstalk:application:environment", "OptionName": "LogglySubDomain"}}` -u `{"Fn::GetOptionSetting": {"Namespace": "aws:elasticbeanstalk:application:environment", "OptionName": "LogglyUserName"}}` -p `{"Fn::GetOptionSetting": {"Namespace": "aws:elasticbeanstalk:application:environment", "OptionName": "LogglyPassword"}}`
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
packages:
yum:
newrelic-php5: []
rpm:
newrelic: http://yum.newrelic.com/pub/newrelic/el5/x86_64/newrelic-repo-5-3.noarch.rpm

files:
"/tmp/update_nr_application_name.sh" :
mode: "000744"
owner: root
group: root
content: |
#!/bin/bash
sed -i 's/^\;\?newrelic\.appname\s*=.*/newrelic.appname = "`{"Fn::GetOptionSetting": {"Namespace": "aws:elasticbeanstalk:application:environment", "OptionName": "NewRelicAppName"}}`"/' /etc/php.d/newrelic.ini
sed -i 's/^\;\?newrelic\.appname\s*=.*/newrelic.appname = "`{"Fn::GetOptionSetting": {"Namespace": "aws:elasticbeanstalk:application:environment", "OptionName": "NewRelicAppName"}}`"/' /etc/php-7.0.d/newrelic.ini


commands:
01_configure_new_relic:
command: newrelic-install install
env:
NR_INSTALL_SILENT: true
NR_INSTALL_KEY:
"Fn::GetOptionSetting":
Namespace: "aws:elasticbeanstalk:application:environment"
OptionName: NewRelicLicenseKey
02_set_application_name:
command: sudo bash /tmp/update_nr_application_name.sh
03_stop_daemon:
command: sudo /etc/init.d/newrelic-daemon stop

Note:

  • If we want to contain characters like ‘|’ in commands, the command should be placed inside a single quote.
Contents
  1. 1. Why AWS Elastic Beanstalk?
  2. 2. How to use Elastic Beanstalk?
    1. 2.1. Cloudformation Template
    2. 2.2. .ebextensions