Following my recent talk at AWS Nottingham, I thought it would be good to share some info about CodeDeploy with you.
What is CodeDeploy?
You can think of CodeDeploy as an alternative to Chef, Puppet or any other orchestration management tool. Its a great way to deploy code on your systems (duh) but also preform actions on the server, such as install software or tweak a config file. This is achieved by the use of scripts which we’ll speak about later. Fundamentally CodeDeploy is an agent that runs on your server (linux or windows) and actions your instructions and puts your code in the right place.
The big question is where does this fit in the management tools offered by AWS. My feeling is that it sits in the middle with OpsWorks. They are both far more flexible than Elastic BeanStalk and less complex than CloudFormation which can control lots more of the AWS services. However is it easier or harder to use than OpsWorks? Well that all depends on if you know Chef well. If you do, OpsWorks will be simple, if you don’t CodeDeploy has less of a steep learning curve in my opinion.
Once you issue a deploy instruction via awscli or the console you have two options for getting the code to the server. These are Github or S3, rather strangely at the time of writing CodeCommit (amazons own git offering) is missing. If you choose to use S3 you can upload the code in tarball or zip format. The structure of the files in the repo or zip should include an appspec.yml file. This file is where you define what files get copied where, what permissions they have and what scripts should be run when. Your directory structure should look something like this:
appspec.yml – scripts —- install_dependencies —- start_server —- stop_server – code —- your files – config —- config files
Files and Permissions
appspec.yml lets you specify files that are included in your bundle and there destination directory on your server, for example /code/ you may want moved to /var/www/. Permissions allow you to set modes on either individual files or directories.
Scripts and Dependancies
Within appsec.yml you can also define scripts within the hooks section. These can be called at various stages of the deployment. If you look at the diagram below, the yellow highlighted boxes are all areas where you define a script to run.
In my example directory structure I’d choose to run install_dependencies in BeforeInstall. In here I could install required packages such as nginx, php, php5-mysql etc etc. The other scripts are kind of self explanatory. The fact you can call scripts at different points in the deployment gives you flexibility to do actions in the right order. The install section in the centre is where your code directory gets copied to the place defined in your appspec.yml. So if you wanted to add a variable to the to your code dynamically you’d typically call this inAfterInstall.
CodeDeploy allows you choose what servers it’s going to run your deployment on by looking for tags that you specify in the deployment group. These can be any EC2 tag or even a autoscale group. The other nice feature is you can specify to only deploy the new code to one server at a time. This way if anything fails, CodeDeploy will stop deploying and not break your other servers, thus preventing a total outage. You can (if you are brave) choose to update all the servers at once or 50% at a time.
As with all the deployment tools from AWS its free, however there’s an extra option here. You can also use CodeDeploy on-prem that is to say in your own AWS data centre. If you choose to use on-prem there is a $0.02 price per instance update. Its quite a nice touch to have to option to use the same tools in other environments.
We’ve now used CodeDeploy with a couple of clients and the response is very positive. The real win is when using CodeDeploy ‘deployment configurations’, you don’t run the risk of b0rking all your servers at once. That together with the fact if you don’t already know chef its very easy to get on-board with the product make COdeDeploy well worth a look.
Official Site: http://aws.amazon.com/codedeploy/
Slides form my talk: https://docs.google.com/presentation/d/19r3BCzBmFViJP3YHisn4miJBz2Oq0EH4bbpVDhjWZXQ/edit?usp=sharing