Last updated on June 2, 2023
In biology, life cycle refers to the series of stages or events that occur during an organism’s lifetime. Lifecycle also applies when automating software deployment on AWS CodeDeploy. Lifecycle event hooks refer to the series of events that describes how a deployment is accomplished. It allows you to control or perform actions on different stages of your deployment.
In the traditional way, whenever you release a new feature for your application, you would have to manually disable the app and install the code. If the code resides in a repository like github, you’d have to manually download the code from github to your machine, make the necessary changes on your machine then restart the application. The lifecycle event hooks in CodeDeploy represent the manual steps taken during deployment. This way, the steps are automated so you can quickly release new features for your application and at the same time prevent downtime.
In this article, I will walk you through the different event hooks in CodeDeploy.
Event hooks are configured on a file called Application Specification file (AppSpec). An AppSpec file is nothing but a piece of a configuration file that can be written using YAML or JSON, which defines how an application is deployed. AppSpec file is available for deploying on three platforms:
- Amazon ECS Compute Platform
- Amazon Lambda Compute Platform
- Amazon EC2/On-premises Compute Platform
Each of them has different hook events. For simplicity, we will be looking at deploying on an EC2 instance. You can find the information for the other two here:
https://docs.aws.amazon.com/codedeploy/latest/userguide/reference-appspec-file-structure-hooks.html#appspec-hooks-server
Here is an overview of the lifecycle event hooks on an EC2/On-premises platform:
Events
Here’s a brief description of the lifecycle events so you can decide at which stage you are going to put custom actions as needed by your deployments.
- Start – This is the first event of the lifecycle event. The CodeDeploy Agent automatically executes this for you. This initiates the instance for deployment.
- ApplicationStop – it is the stage for running any scripts that will stop your old application. For example, if you have a new version of an e-commerce web application, let’s say v.1, this event will allow you to disable v.0 and prepare the instance to receive a new version, which in this case is v.1.
- DownloadBundle – During this event, the CodeDeploy Agent will pull the new version onto the instance.
- BeforeInstall – You can use this event to store the previous configuration of your old install that you want to keep, decrypt files, and create a backup of the current version.
- Install – During this event, the CodeDeploy Agent will copy the revision files to a file destination that you specify.
- AfterInstall – this event gives you the chance to change the configuration of your application before the application starts.
- ApplicationStart – As the name implies, you use this event to turn on your application which is now v.1 instead of v.0.
- ValidateService – You can use this event to include any validation logic to determine that the deployment succeeded.
- End – This is the last event of the lifecycle event. This will notify the central service that the deployment to the instance was successful.
Things To Consider
- YAML is the only available format for configuring AppSpec in EC2/On-premise.
- You can either use YAML or JSON for configuring AppSpec in ECS and Lambda.
- You need to install the CodeDeploy Agent for configuring AppSpec in EC2/On-premise.
- For ECS and Lambda, you can directly use the online editor.
- Referring to the diagram above, only the blue-colored events can be scripted. The rest are handled by the CodeDeploy Agent.
Sources:
https://docs.aws.amazon.com/codedeploy/latest/userguide/reference-appspec-file-structure-hooks.html#appspec-hooks-server
https://docs.aws.amazon.com/codedeploy/latest/userguide/reference-appspec-file-example.html
https://docs.aws.amazon.com/codedeploy/latest/userguide/application-specification-files.html#appspec-files-on-ecs-compute-platform
https://www.youtube.com/watch?v=A4NSyUbAEkw&t=2328s