On-premise support came in mid-2015. AWS CodeDeploy has been the choice of many since a long time and we are going to talk about it in this blog.
I had to automate my deployment process. I came across AWS CodeDeploy and it helped me to deploy my new code with ease and simplicity. We will go through the basic setup of CodeDeploy service, installation of CodeDeploy agent on a server (AWS EC2 instance) and deployment of new code from GitHub onto it. CodeDeploy is a region level service and in this, we will talk about deployment in the Virginia region. For using it in any other region, you have to make just a few changes in endpoints which is simple too.
An AWS EC2 instance with IAM role attached. If you don't have a role attached to your EC2 instance, then you will have to launch a new EC2 instance with an attached role from an AMI. For now, you can just attach a blank role, and I will tell you later in the blog about what permissions policy to attach.
The above image shows the various stages and event hooks during a deployment using CodeDeploy. We define what CodeDeploy should do during all these events in a file called appspec.yml as mentioned in the prerequisites.
Above shown is the way you define various event hooks along with source ( the destination in the Git repository from where you want the code to be deployed, "/" means the root directory of the repo) and destination ( the location on the server where the new code has to be placed). Each hook has a script associated with it.
For this example, I have just used scripts for BeforeInstall, AfterInstall, ApplicationStart.
In BeforeInstall I have used a script (apache_stop.sh) which will run before the install step i.e. before CodeDeploy puts the code to GitHub onto a node.
This script will firstly clears the root directory where the older code is present. Reason being that CodeDeploy needs a clean directory where it has to deploy the code. Before cleaning the directory, it will also make a backup of the files. You can do any relevant changes in this step depending on your application like stop Apache or Tomcat etc. which would you need to do before the code is deployed.
After this, the Install hooks is run by AWS CodeDeploy itself where it pulls the new code from the Git Repository and places it in the destination directory.
Here, I have simply added a command that will start apache and check whether it is running or not. If Apache fails to start, obviously there are some code issues, and you will be shown an error on the console.
Now, that we have the appspec.yml and the scripts handy, just push it along with the code to GitHub.
Once that is done, we need to deploy the new code and let CodeDeploy know from where it has to pick up the code and deploy to the deployment group demo that has instance code_deploy_test in it.
Deploy Half at once: Deploys to exactly half of the servers at a time.
Deploy All at once: Deploys to all servers at once.
I prefer one at a time as any error if there is one can be detected early rather than after new code is already deployed to all the servers.
After selecting the event just mention the SNS topic and create the trigger. You can create multiple triggers.
You can create a new Deployment Group by clicking on "Create deployment group". "Revisions" shows the number of times the code was deployed to this Deployment Group which of course is zero for now. If you have multiple Deployment Groups, just select it and then click on Revisions which will expand any previous deployment details. You can also revert to a previous revision of code by selecting the revision and deploying it to the Deployment Group again. All that said, let's do our first AWS CodeDeploy deployment.
Remember you can't start a deployment again from here itself. You need to go to Deployment Groups page and deploy a new revision.
So, this is how CodeDeploy works and how you can deploy your code easily using CodeDeploy on n number of servers.
You can perform all the above console steps via AWS CLI as well. Read more about it here: AWS CodeDeploy CLI.
For now, automatic rollback is not provided as a feature, and you have to deploy a previous revision through the console manually. To get rid of that, we at TO THE NEW have been able to automate that using AWS Lambda. You can read about that use-case here: Automatic Rollback in AWS CodeDeploy.