Skip to content

Advanced Code Deploy The AppSpec File

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.

Lambda Deployments

For Lambda deployments, the AppSpec File may be written in YAML or JSON and contrians 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.

Here is an example YAML file:

version: 0.0
resources:
  -myLambdaFunction:
    Type: AWS::Lambda::Function
    Properties:
      Name: "myLambdaFunction"
      Alias: "myLambdaFunctionAlias"a
      CurrentVersion: "1"
      TargetVersion: "2"
hooks:
  - BeforeAllowTraffic: "LamnbdaFunctionToValidateBeforeTrafficShift"
  - AfterAllowTraffic: "LambdaFunctionToValidateAfterTrafficShift"

Lambda Hooks

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)
  • 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."

AppSpec File - Hooks - EC12 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 or Windows)
  • Files: The location of any application 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.)

For EC2 and On-Premises deployments, the appspec.yml must be placed in the root of the 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 /Config /Source

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

Definition of Hooks

It's really important to try to remember the sequence of events that occur during a CodeDeploy deployment:

  • 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.
  • ApplicationStop: Gracefully stop the application in preparation for deploying the new revision.
  • 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.
  • 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, etc.)
  • ApplicationStart: Restarts any services that were stopped during ApplicationStop.
  • ValidateService: Details of any tests to validate the service.
  • 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.

Advanced CodeDeploy Exam Tips

  • The AppSpec file defines all the parameters needed for the deployment (e.g. locations 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 root directory of your revision (the same folder that contains your application code). Written in AML.
  • Lambda supports YAML or JSON
  • Try to remember the run order of hooks (listed above).