AWS CodeDeploy
By Anton Babenko, May 2016
Hi!
Anton Babenko
I enjoy AWS, DevOps and web-development.
I am AWS Certified Solution Architect, SysOps and DevOps.
I work as DevOps engineer at Your.MD.
I am one of organizers of AWS User Group Norway meetups
( Next meetup - “Big data experience at Schibsted”, 30th of May, 17:30 at MESH )
github.com/antonbabenko linkedin.com/in/antonbabenko
anton@antonbabenko.com
What is AWS CodeDeploy?
Fully managed service which allows deployment to Amazon EC2 and on-premise instances
Requires no modifications to existing code and is technology agnostic
Can deploy from Amazon S3 buckets and Github repos
Free
Getting started
Install codedeploy agent
Prepare your application (add appspec.yml)
Create archive and register application revision
Create archive, upload it to S3 and register application revision:
aws deploy push
Register application revision (can be combined with ghr):
aws deploy register-application-revision
Configure target environment (“deployment group”)
Deploy
Deploy application revision (myapp-v1.0.zip) to deployment group (myapp-prod) according to deployment
configuration (CodeDeployDefault.OneAtATime):
aws deploy create-deployment 
--application-name myapp 
--deployment-config-name CodeDeployDefault.OneAtATime 
--deployment-group-name myapp-prod 
--description "My app v1.0 deployment to production" 
--s3-location bucket=myapp-archives,bundleType=zip,key=myapp-v1.0.zip
Execution flow
appspec.yml
version: 0.0
os: linux
files:
- source: Config/config.txt
destination: webapps/Config
- source: source
destination: /webapps/myApp
# permissions: # skipped on this example
hooks:
ApplicationStop:
- location: codedeploy/playbooks/application_stop.yml
BeforeInstall:
- location: codedeploy/playbooks/before_install.yml
- location: Scripts/UnzipDataBundle.sh
AfterInstall:
- location: codedeploy/playbooks/after_install.yml
ApplicationStart:
- location: codedeploy/playbooks/application_start.yml
timeout: 3600
ValidateService:
- location: Scripts/MonitorService.sh
timeout: 3600
runas: codedeployuser
codedeploy/playbooks/application_stop.yml
#!/usr/bin/env ansible-playbook
---
- hosts: localhost
gather_facts: false
become: true
tasks:
- name: Stop supervisor service
ignore_errors: yes
supervisorctl:
name: "search"
state: stopped
Deployment configuration
One at a time
Half at a time
All at once
Custom
Deployment group
Options:
One per environment (development-site, staging-site, production-site)
Blue-green fashion:
production-site-blue + application revision v1.0
production-site-green + application revision v1.1
Integrations
Github webhooks
S3 + AWS Lambda
CircleCI, CodeShip, Jenkins, etc
AWS
Auto-scaling
ELB
SNS
Cloudwatch
Cloudtrail
Terraform
Considerations
S3 bucket and CodeDeploy application should be in the same AWS region
S3 cross-region replication does not work for private files
Solution: Register application revision for each region/bucket individually
Take care of created files not managed by CodeDeploy yourself
Solution: Use BeforeInstall hook
No control of what revision to deploy during ASG scaling activity
No straightforward solutions I know, only hacks (wrapper-application, triggers to SNS)
Watch out for infinite EC2 restarts during ASG scaling activity
Solution: Use ValidateService hook wisely
Hint: Test deployments on both running and newly created instances
Thank you!
Questions ?

AWS CodeDeploy - basic intro

  • 1.
    AWS CodeDeploy By AntonBabenko, May 2016
  • 2.
    Hi! Anton Babenko I enjoyAWS, DevOps and web-development. I am AWS Certified Solution Architect, SysOps and DevOps. I work as DevOps engineer at Your.MD. I am one of organizers of AWS User Group Norway meetups ( Next meetup - “Big data experience at Schibsted”, 30th of May, 17:30 at MESH ) github.com/antonbabenko linkedin.com/in/antonbabenko anton@antonbabenko.com
  • 3.
    What is AWSCodeDeploy? Fully managed service which allows deployment to Amazon EC2 and on-premise instances Requires no modifications to existing code and is technology agnostic Can deploy from Amazon S3 buckets and Github repos Free
  • 4.
    Getting started Install codedeployagent Prepare your application (add appspec.yml) Create archive and register application revision Create archive, upload it to S3 and register application revision: aws deploy push Register application revision (can be combined with ghr): aws deploy register-application-revision Configure target environment (“deployment group”)
  • 5.
    Deploy Deploy application revision(myapp-v1.0.zip) to deployment group (myapp-prod) according to deployment configuration (CodeDeployDefault.OneAtATime): aws deploy create-deployment --application-name myapp --deployment-config-name CodeDeployDefault.OneAtATime --deployment-group-name myapp-prod --description "My app v1.0 deployment to production" --s3-location bucket=myapp-archives,bundleType=zip,key=myapp-v1.0.zip
  • 6.
  • 7.
    appspec.yml version: 0.0 os: linux files: -source: Config/config.txt destination: webapps/Config - source: source destination: /webapps/myApp # permissions: # skipped on this example hooks: ApplicationStop: - location: codedeploy/playbooks/application_stop.yml BeforeInstall: - location: codedeploy/playbooks/before_install.yml - location: Scripts/UnzipDataBundle.sh AfterInstall: - location: codedeploy/playbooks/after_install.yml ApplicationStart: - location: codedeploy/playbooks/application_start.yml timeout: 3600 ValidateService: - location: Scripts/MonitorService.sh timeout: 3600 runas: codedeployuser
  • 8.
    codedeploy/playbooks/application_stop.yml #!/usr/bin/env ansible-playbook --- - hosts:localhost gather_facts: false become: true tasks: - name: Stop supervisor service ignore_errors: yes supervisorctl: name: "search" state: stopped
  • 9.
    Deployment configuration One ata time Half at a time All at once Custom
  • 10.
    Deployment group Options: One perenvironment (development-site, staging-site, production-site) Blue-green fashion: production-site-blue + application revision v1.0 production-site-green + application revision v1.1
  • 11.
    Integrations Github webhooks S3 +AWS Lambda CircleCI, CodeShip, Jenkins, etc AWS Auto-scaling ELB SNS Cloudwatch Cloudtrail Terraform
  • 12.
    Considerations S3 bucket andCodeDeploy application should be in the same AWS region S3 cross-region replication does not work for private files Solution: Register application revision for each region/bucket individually Take care of created files not managed by CodeDeploy yourself Solution: Use BeforeInstall hook No control of what revision to deploy during ASG scaling activity No straightforward solutions I know, only hacks (wrapper-application, triggers to SNS) Watch out for infinite EC2 restarts during ASG scaling activity Solution: Use ValidateService hook wisely Hint: Test deployments on both running and newly created instances
  • 13.

Editor's Notes

  • #4 * It can be used to deploy to anything connected to internet where Ruby can be installed ** Free for EC2 instances, 2 cents per deployment for on-premise instances *** Deployable content can be code, configuration files, packages, anything.