CI & CD ON AWS
Build and deliver products using AWS
Sistemi Distribuiti e Cloud Computing
Università degli Studi di Roma "Tor Vergata”
DICII
Paolo Latella

XPeppers - Cloud & DevOps

paolo.latella@xpeppers.com
@LatellaPaolo
WHAT ARE CI AND CD ?
• Continuous Integration: ?
• Continuous Delivery: ?
paolo.latella@xpeppers.com
WHAT ARE CI AND CD ?
• Continuous Integration: every time
somebody commits any change, the entire
application is built and a comprehensive set of
automated tests is run against it.
• Continuous Delivery: more than just a new
delivery methodology. It is a whole new paradigm
for running a business that depends on software.
Continuous Delivery - Jez Humble, David Farley
CI/CD AWSTOOLS
Code Build Test Provision Deploy Monitor
Integration
Delivery
AWS CodePipeline AWS CodeBuildAWS CodeCommit
AWS CodeDeploy
AWS
CloudFormation
AWS OpsWorks
Amazon EC2
Container Service
AWS Elastic
Beanstalk
Amazon
CloudWatch
AWS

CloudTrail
paolo.latella@xpeppers.com
CODECOMMIT
• Fully managed.
• Repositories of any size and any file type.
• Has highly available repositories.
• Choose the region where your repository should reside.
• Supports the standard functionality of Git
• Online code tools to browse, edit, and collaborate on projects.
paolo.latella@xpeppers.com
CODECOMMIT
Host git-codecommit.*.amazonaws.com
User APKAEIBAERJR2EXAMPLE
IdentityFile ~/.ssh/codecommit_rsa
git clone ssh://git-codecommit.us-
east-1.amazonaws.com/v1/repos/MyDemoRepo
my-demo-repo
$ ssh-keygen
Generate public/private rsa key pair.
paolo.latella@xpeppers.com
CODEPIPELINE
• Graphical user interface to create, configure, and
manage your pipeline Fully managed.
• Parallel Execution
• Integration with CodeDeploy, Lambda, S3,
CodeCommit, Beanstalk, Opsworks and third-party
tools
• Custom action, manual approval, retry,
https://aws.amazon.com/codepipeline/product-integrations/
paolo.latella@xpeppers.com
CODEPIPELINE
Source
Specify where source code is
stored.
Amazon S3, GitHub
Build
Specify how application should
be built.
Jenkins and
other providers
Test
Specify how application should
be tested.
Jenkins, Ghost Inspector
and other providers
Deploy
Specify how application should
be deployed.
AWS Elastic Beanstalk,
AWS CodeDeploy
Invoke
Specify custom function to
invoke.
AWS Lambda
Actions
Pipeline
Parallel actions
paolo.latella@xpeppers.com
CODEBUILD
• Fully managed: eliminates the need to set up, patch, update,
and manage your own build servers.
• On demand: scales on demand to meet your build needs.
You pay only for the number of build minutes you
consume.
• Out of the box: provides preconfigured build environments
for the most popular programming languages.All you need
to do is point to your build script to start your first build.
https://aws.amazon.com/codepipeline/product-integrations/
paolo.latella@xpeppers.com
CODEBUILD
paolo.latella@xpeppers.com
CODEDEPLOY
• Coordinates application deployments to Amazon
EC2 instances.
• Deploy from a GitHub repository or from any local
codebase.
• Manage deployments across environments, upgrade
applications, and perform rolling updates.
• Supports Linux and Windows deployments.
paolo.latella@xpeppers.com
Deployment Group
Auto Scaling Group
Agent Agent
Agent Agent
Tags:Name=DeploymentTarget,

Value=OurAppOnCodeDeploy
Create Deployment
AWS CodeDeploy
Pull Latest Revision
AWS CodeDeploy-ready
deployment
AppSpec fileApplication
+
Revision 1
Revision 2
Revision 3
Amazon S3 Bucket
CODEDEPLOY
paolo.latella@xpeppers.com
CODEDEPLOY
version: 0.0
os: linux
files:
- source: Config/config.txt
destination: /webapps/Config
- source: source
destination: /webapps/myApp
hooks:
BeforeInstall:
- location: Scripts/UnzipResourceBundle.sh
- location: Scripts/UnzipDataBundle.sh
AfterInstall:
- location: Scripts/RunResourceTests.sh
timeout: 180
ApplicationStart:
- location: Scripts/RunFunctionalTests.sh
timeout: 3600
ValidateService:
- location: Scripts/MonitorService.sh
timeout: 3600
runas: codedeployuser
Hooks
CODEDEPLOY
#!/usr/bin/python
import boto3
import urllib2
import time
CONNECTION_DRAINING_TIMEOUT = 60
print "Get metadata for instance"
instance_id=urllib2.urlopen('http://169.254.169.254/latest/meta-data/instance-id').read()
availability_zone=urllib2.urlopen('http://169.254.169.254/latest/meta-data/placement/availability-zone').read()
region = availability_zone[:-1]
asg = boto3.client('autoscaling',region)
print 'Trying to move instance ' + instance_id + ' in stand-by'
res_asg_of_instance = asg.describe_auto_scaling_instances(InstanceIds=[instance_id])
name_asg_of_instance = res_asg_of_instance['AutoScalingInstances'][0]['AutoScalingGroupName']
state_of_instance = res_asg_of_instance['AutoScalingInstances'][0]['LifecycleState']
if (state_of_instance == 'InService'):
res_enter_in_standby =
asg.enter_standby(InstanceIds=[instance_id],AutoScalingGroupName=name_asg_of_instance,ShouldDecrementDesiredCapac
ity=True)
print 'Instance ' + instance_id + 'entering in stand-by'
print res_enter_in_standby
time.sleep(CONNECTION_DRAINING_TIMEOUT)
exit(0)
if (state_of_instance == 'Standby'):
print 'Instance ' + instance_id + ' already in stand-by'
time.sleep(10)
exit(0)
else:
print 'Instance ' + instance_id + ' is in ' + state_of_instance + ' ignoring ...'
exit(0)
Application Stop
?
BLUE/GREEN
DEPLOYMENT
paolo.latella@xpeppers.com
BLUE/GREEN DEPLOYMENT
v1.0 v2.0
INFRASTRUCTURE AS CODE
Infrastructure Automation
Configuration Management
Application Deployment
I
a
C
paolo.latella@xpeppers.com
CLOUDFORMATION
• Simplify infrastructure management in the cloud
• Perform predictable, repeatable, and automated
deployments.
• Easily control and track changes to your infrastructure.
• Simply JSON orYAML formatted text file that describes
the AWS infrastructure
paolo.latella@xpeppers.com
CLOUDFORMATION
CloudFormation
Engine
Template Stack
JSON Syntax
Versioning
Runtime parameters
Dependency
Dependencies
Conflict resolve
All Resources
Create/Update/Delete
Scriptable
Deploy several times
Multiregions
Dev, Test, Prod
paolo.latella@xpeppers.com
Amazon

Route 53
AWS BLUE/GREEN
DEPLOYMENT
Auto Scaling group Auto Scaling group
instances instances
template
BLUE

Stack
AWS

CloudFormation
GREEN

Stack
90% 10%
alarm
IF (alarm) 

Then Rollback
else increment %
AWS 

CodeDeploy
ALLTOGETHER
GRAZIE!
Q&A
Paolo Latella

XPeppers - Agile, Cloud & DevOps

paolo.latella@xpeppers.com
@LatellaPaolo

CI & CD on Amazon Web Services

  • 1.
    CI & CDON AWS Build and deliver products using AWS Sistemi Distribuiti e Cloud Computing Università degli Studi di Roma "Tor Vergata” DICII Paolo Latella
 XPeppers - Cloud & DevOps
 paolo.latella@xpeppers.com @LatellaPaolo
  • 2.
    WHAT ARE CIAND CD ? • Continuous Integration: ? • Continuous Delivery: ? paolo.latella@xpeppers.com
  • 3.
    WHAT ARE CIAND CD ? • Continuous Integration: every time somebody commits any change, the entire application is built and a comprehensive set of automated tests is run against it. • Continuous Delivery: more than just a new delivery methodology. It is a whole new paradigm for running a business that depends on software. Continuous Delivery - Jez Humble, David Farley
  • 4.
    CI/CD AWSTOOLS Code BuildTest Provision Deploy Monitor Integration Delivery AWS CodePipeline AWS CodeBuildAWS CodeCommit AWS CodeDeploy AWS CloudFormation AWS OpsWorks Amazon EC2 Container Service AWS Elastic Beanstalk Amazon CloudWatch AWS
 CloudTrail paolo.latella@xpeppers.com
  • 5.
    CODECOMMIT • Fully managed. •Repositories of any size and any file type. • Has highly available repositories. • Choose the region where your repository should reside. • Supports the standard functionality of Git • Online code tools to browse, edit, and collaborate on projects. paolo.latella@xpeppers.com
  • 6.
    CODECOMMIT Host git-codecommit.*.amazonaws.com User APKAEIBAERJR2EXAMPLE IdentityFile~/.ssh/codecommit_rsa git clone ssh://git-codecommit.us- east-1.amazonaws.com/v1/repos/MyDemoRepo my-demo-repo $ ssh-keygen Generate public/private rsa key pair. paolo.latella@xpeppers.com
  • 7.
    CODEPIPELINE • Graphical userinterface to create, configure, and manage your pipeline Fully managed. • Parallel Execution • Integration with CodeDeploy, Lambda, S3, CodeCommit, Beanstalk, Opsworks and third-party tools • Custom action, manual approval, retry, https://aws.amazon.com/codepipeline/product-integrations/ paolo.latella@xpeppers.com
  • 8.
    CODEPIPELINE Source Specify where sourcecode is stored. Amazon S3, GitHub Build Specify how application should be built. Jenkins and other providers Test Specify how application should be tested. Jenkins, Ghost Inspector and other providers Deploy Specify how application should be deployed. AWS Elastic Beanstalk, AWS CodeDeploy Invoke Specify custom function to invoke. AWS Lambda Actions Pipeline Parallel actions paolo.latella@xpeppers.com
  • 9.
    CODEBUILD • Fully managed:eliminates the need to set up, patch, update, and manage your own build servers. • On demand: scales on demand to meet your build needs. You pay only for the number of build minutes you consume. • Out of the box: provides preconfigured build environments for the most popular programming languages.All you need to do is point to your build script to start your first build. https://aws.amazon.com/codepipeline/product-integrations/ paolo.latella@xpeppers.com
  • 10.
  • 11.
    CODEDEPLOY • Coordinates applicationdeployments to Amazon EC2 instances. • Deploy from a GitHub repository or from any local codebase. • Manage deployments across environments, upgrade applications, and perform rolling updates. • Supports Linux and Windows deployments. paolo.latella@xpeppers.com
  • 12.
    Deployment Group Auto ScalingGroup Agent Agent Agent Agent Tags:Name=DeploymentTarget, Value=OurAppOnCodeDeploy Create Deployment AWS CodeDeploy Pull Latest Revision AWS CodeDeploy-ready deployment AppSpec fileApplication + Revision 1 Revision 2 Revision 3 Amazon S3 Bucket CODEDEPLOY paolo.latella@xpeppers.com
  • 13.
    CODEDEPLOY version: 0.0 os: linux files: -source: Config/config.txt destination: /webapps/Config - source: source destination: /webapps/myApp hooks: BeforeInstall: - location: Scripts/UnzipResourceBundle.sh - location: Scripts/UnzipDataBundle.sh AfterInstall: - location: Scripts/RunResourceTests.sh timeout: 180 ApplicationStart: - location: Scripts/RunFunctionalTests.sh timeout: 3600 ValidateService: - location: Scripts/MonitorService.sh timeout: 3600 runas: codedeployuser Hooks
  • 14.
    CODEDEPLOY #!/usr/bin/python import boto3 import urllib2 importtime CONNECTION_DRAINING_TIMEOUT = 60 print "Get metadata for instance" instance_id=urllib2.urlopen('http://169.254.169.254/latest/meta-data/instance-id').read() availability_zone=urllib2.urlopen('http://169.254.169.254/latest/meta-data/placement/availability-zone').read() region = availability_zone[:-1] asg = boto3.client('autoscaling',region) print 'Trying to move instance ' + instance_id + ' in stand-by' res_asg_of_instance = asg.describe_auto_scaling_instances(InstanceIds=[instance_id]) name_asg_of_instance = res_asg_of_instance['AutoScalingInstances'][0]['AutoScalingGroupName'] state_of_instance = res_asg_of_instance['AutoScalingInstances'][0]['LifecycleState'] if (state_of_instance == 'InService'): res_enter_in_standby = asg.enter_standby(InstanceIds=[instance_id],AutoScalingGroupName=name_asg_of_instance,ShouldDecrementDesiredCapac ity=True) print 'Instance ' + instance_id + 'entering in stand-by' print res_enter_in_standby time.sleep(CONNECTION_DRAINING_TIMEOUT) exit(0) if (state_of_instance == 'Standby'): print 'Instance ' + instance_id + ' already in stand-by' time.sleep(10) exit(0) else: print 'Instance ' + instance_id + ' is in ' + state_of_instance + ' ignoring ...' exit(0) Application Stop ?
  • 15.
  • 16.
  • 17.
    INFRASTRUCTURE AS CODE InfrastructureAutomation Configuration Management Application Deployment I a C paolo.latella@xpeppers.com
  • 18.
    CLOUDFORMATION • Simplify infrastructuremanagement in the cloud • Perform predictable, repeatable, and automated deployments. • Easily control and track changes to your infrastructure. • Simply JSON orYAML formatted text file that describes the AWS infrastructure paolo.latella@xpeppers.com
  • 19.
    CLOUDFORMATION CloudFormation Engine Template Stack JSON Syntax Versioning Runtimeparameters Dependency Dependencies Conflict resolve All Resources Create/Update/Delete Scriptable Deploy several times Multiregions Dev, Test, Prod paolo.latella@xpeppers.com
  • 20.
    Amazon
 Route 53 AWS BLUE/GREEN DEPLOYMENT AutoScaling group Auto Scaling group instances instances template BLUE
 Stack AWS
 CloudFormation GREEN
 Stack 90% 10% alarm IF (alarm) 
 Then Rollback else increment % AWS 
 CodeDeploy
  • 21.
  • 22.
    GRAZIE! Q&A Paolo Latella
 XPeppers -Agile, Cloud & DevOps
 paolo.latella@xpeppers.com @LatellaPaolo