AWS CodeDeploy EC2/On-Premises Deployment - Designing AppSpec and Lifecycle Hooks
Declaratively define deployment target paths and lifecycle hooks using AppSpec files. Learn about fleet management with tag-based deployment groups and Auto Scaling integration.
Structure of the AppSpec File
The AppSpec file (appspec.yml) for EC2/on-premises deployments uses the files section to define deployment target paths for source files, the permissions section to set file ownership and permissions, and the hooks section to specify scripts that run at each stage of the lifecycle. In the files section, you define mappings between source (path within the revision) and destination (path on the instance). In the permissions section, you specify owner, group, and mode to properly configure permissions for deployed files.
Using Lifecycle Hooks
Lifecycle hooks are scripts that run at each stage of the deployment. BeforeInstall runs before files are deployed and is used for cleaning up old versions or installing dependency packages. AfterInstall runs after files are deployed and is used for generating configuration files or running database migrations. ApplicationStart executes the application startup script. ValidateService runs post-deployment health checks to verify that the application is functioning correctly. If ValidateService fails, the deployment is recorded as failed, and if automatic rollback is configured, it reverts to the previous version.
Deployment Groups and Fleet Management
A deployment group is a unit that groups the instances targeted for deployment. Tag filters allow dynamic selection, such as choosing instances where Environment:Production and Role:WebServer. With Auto Scaling group integration, the latest revision is automatically deployed to newly launched instances during scale-out. By specifying the minimum healthy hosts in the deployment configuration, you ensure that a certain number of instances continue processing requests during deployment. CodeDeployDefault.OneAtATime deploys one instance at a time, while CodeDeployDefault.HalfAtATime deploys half at a time as predefined configurations. For a deeper understanding of EC2 deployment practices, specialized books (Amazon) can be helpful.
CodeDeploy Pricing
CodeDeploy EC2/on-premises deployments are free to use. There are no additional charges for deploying to EC2 instances. Deployments to on-premises servers cost approximately $0.02 per instance per deployment. Deployments to ECS and Lambda are also free. Since CodeDeploy itself costs virtually nothing, it has a low barrier to adoption as a deployment stage in CI/CD pipelines.
Summary
CodeDeploy EC2/on-premises deployments declaratively define the deployment process using AppSpec files and lifecycle hooks. Tag-based deployment groups handle dynamic fleets, and minimum healthy hosts ensure availability during deployment. Since on-premises servers can also be included as targets, it is effective for unified deployment management in hybrid environments.