Advanced CodeDeploy; The AppSpec File
AppSpec File - Lambda Deployments
The AppSpec File is used to define the parameters that will be used for a CodeDeploy Deployment. The file structure depends on whether you are deploying to Lambda or EC2 / On Premises.
For Lambda Deployments, the AppSpec File may be written in YAML or JSON and contains the following fields:
- Version
- reserved for future use - currently the only allowed value is 0.0
- Resources
- The name and properties of the Lambda function to deploy
- Hooks
- Specifies Lambda functions to run at set points in the deployment lifecycle to validate the deployment
- e.g. validation tests to run before allowing traffic to be sent to your newly deployed instances
- Specifies Lambda functions to run at set points in the deployment lifecycle to validate the deployment
appspec.yml File - Lambda Example
version: 0.0
resources:
- myLAmbdaFunction:
Type: AWS::Lambda::Function
Properties:
Name: "myLambdaFunction"
Alias: "MyLambdaFunctionAlias"
CurrentVersion: "1"
TargetVersion: "2"
hooks:
- BeforeAllowTraffic: "LambdaFunctionToValidateBeforeTrafficShift"
- AfterAllowTraffic: "LambdaFunctionToValidateAfterTrafficShift
- Properties fields are mandatory for Lambda
AppSpec File - Hooks - Lambda
The following hooks are available for use:
- BeforeAllowTraffic
- Used to specify the tasks or functions you want to run before traffic is routed to the newly deployed Lambda function
- e.g. test to validate that the function has been deployed correctly
- Used to specify the tasks or functions you want to run before traffic is routed to the newly deployed Lambda function
- AfterAllowTraffic
- Used to specify the tasks or functions you want to run after the traffic has been routed to the newly deployed Lambda function
- e.g. test to validate that the function is accepting traffic correctly and behaving as expected
- Used to specify the tasks or functions you want to run after the traffic has been routed to the newly deployed Lambda function
AppSpecFile - EC2 and On Premises
- version
- Reserved for future use - currently the only allowed value is 0.0
- OS
- The Operating System version you are using, e.g. linux, windows
- files
- The location of any appliccation files that need to be copied and where they should be copied to (source and destination folders)
- Hooks
- Lifecycle event hooks allow you to specify scripts that need to run at set points in the deployment lifecycle
- e.g. to unzip application files prior to deployment, run functional tests on the newly deployed application, and to de-register and re-register instances with a load balancer
- Lifecycle event hooks allow you to specify scripts that need to run at set points in the deployment lifecycle
For EC2 and On Premises deployments, the appspec.yml file must be placed in the root directory of your revision - this is the directory containing your application source code, otherwise the deployment will fail
Typical setup looks like this:
- mywebapp folder:
- appspec.yml
- /Scripts - scripts you want to run (3 directories)
- /Config - config files
- /Source - source files
appspec.yml File - EC2
version: 0.0
os: linux
files:
- source: Config/config.txt
destination: /webapps/Config
- source: Source
destination: /webapps/myApp
hooks:
BeforeInstall:
- location: Scripts/UnzipResourceBundle.sh
- location: Scripts/UnzipDataBundle.sh
AfterInstall:
- location: Scripts/RunResourceTests.sh
timeout: 180
ApplicationStart:
- location: Scripts/RunFunctionalTests.sh
timeout: 3600
ValidateService:
- location: Scripts/MonitorService.sh
timeout: 3600
runas: codedeployuser
- runas: codedeployuser - IAM user set up with permissions for codedeploy
AppSpec File - Supported Hooks for EC2 and On Premises
Set of EC2 instances sitting behind a load balancer. Update using CodeDeploy:
- BeforeBlockTraffic
- run tasks on instances before they are deregistered from a load balancer
- BlockTraffic
- Deregister instances from a load balancer
- AfterBlockTraffic
- Run tasks on instances after they are deregistered from a load balancer
- Steps you would normally need to do when upgrading any application
- ApplicationStop
- Gracefully stop the application in preparation for deploying the new version
- DownloadBundle
- The CodeDeploy agent copies the application revision files to a temporary location
- BeforeInstall
- Details of any pre-installation scripts
- e.g. backing up the current version, decrypting files
- Details of any pre-installation scripts
- Install
- The CodeDeploy agent copies the application revision files from their temporary location to their correct location
- AfterInstall
- Details of any post-installation scripts
- e.g. configuration tasks, change file permissions
- Details of any post-installation scripts
- ApplicationStart
- Restarts any services that were stopped during ApplicationStop
- ValidateService
- Details of any tests to validate the service
- Hooks related to re-registerring instances
- BeforeAllowTraffic
- Run tasks on instances before they are registered with a load balancer
- AllowTraffic
- Register instances with a load balancer
- AfterAllowTraffic
- Run tasks on instances after they are registered with a load balancer
3 stage process:
- deregister systems from load balancer
- tasks you need to do to upgrade application
- tasks to re-register with load balancer
These events are referred to as the run order of hooks
CodeDeploy Advanced Settings - Exam Tips
- The AppSpec file defines all the paramters needed for the deployment
- e.g. location of application files and pre/post deployment validation tests to run
- For EC2 / On Premises systems, the appspec.yml file must be placed in the rot directory of your revision (the same folder that contains your application code) Written in YAML
- Lambda supports YAML or JSON
- Run order for hooks in a CodeDeploy Deployment (see above)