AppSpec File for EC2 and On-Premises
What you'll learn: How to write an AppSpec file that tells CodeDeploy exactly what to deploy, where to put it, and what scripts to run during each deployment phase on EC2 or on-premises servers.
What Is the AppSpec File?
The AppSpec file (Application Specification) is CodeDeploy's instruction manual for your deployment. Think of it as a recipe: it lists the files to copy, their destinations, and the commands to run before, during, and after deployment. For EC2 and on-premises deployments, this file must be named appspec.yml and sit at your application's root.
Core Structure
The AppSpec file has three main sections:
1. Version: Always 0.0 (required format)
2. Files section: Maps source files to destination paths on the instance:
files:
- source: /app
destination: /var/www/html
- source: /configs/app.conf
destination: /etc/myapp/
3. Hooks section: Defines lifecycle event scripts that run at specific deployment phases:
hooks:
BeforeInstall:
- location: scripts/install_dependencies.sh
timeout: 300
AfterInstall:
- location: scripts/configure_app.sh
ApplicationStart:
- location: scripts/start_server.sh
ValidateService:
- location: scripts/test_service.sh
Lifecycle Events
Common hooks include: BeforeInstall, AfterInstall, ApplicationStart, ApplicationStop, and ValidateService. Each hook can run scripts with configurable timeouts and specify which user should execute them.
Key Takeaway: The AppSpec file is CodeDeploy's blueprint for EC2/on-premises deployments—it defines file mappings from source to destination and lifecycle hooks that execute scripts at precise deployment moments, giving you full control over the deployment process.