How to do it...

The high-level structure of the AppSpec.yml file is as follows:

version: 0.0
os: operating-system-name
files:
source-destination-files-mappings
permissions:
permissions-specifications
hooks:
deployment-lifecycle-event-mappings

Let's look into all the directives in detail:

  • version: This directive specifies the version of the AppSpec file, which is currently 0.0. This value is not supposed to change by any means because it is reserved by CodeDeploy for future use.
  • os: By the name itself, we can understand that it refers to the OS of the instance where it will get deployed. It has two possible values: Linux ( Amazon Linux, Ubuntu Server, or RHEL) or Windows Server.
  • files: This directive is basically for the files, which need to copy from the source code directory and are placed somewhere on the instance. So, by default, it has two parameters, that is, source and destination, for example:
files:
- source: configuration/config.txt
destination: /var/Config

   In this example, the following operation will be performed during the install event:

  • configuration/config.txt : This is placed in the source code directory and will be copied to the /var/Config/config.txt path on the instance.
  • permissions: This directive gives permission to the file which get copied to the instance. The following is an example of giving permission to an object:
 - object: /tmp/webapp/src
group: wheel
mode: 564
type:
- directory

For the folder /tmp/webapp/src, group = wheel and mode = 564 (dr-xrw-r--), so the permission of that folder will be:  

dr-xrw-r-- root wheel src
  • hooks: This directive contains the mapping of the deployment life cycle event with respective scripts. If any event hook is not present, that means there is no script or operation for execution for that event. This section is used only when you need to run any script on the instances. The following is the list of life cycle event hooks:
    • ApplicationStop:  An AppSpec file does not exist on an instance before you deploy to it. For this reason, the ApplicationStop hook will not run the first time you deploy to the instance. You can use the ApplicationStop hook the second time you deploy to an instance.
DownloadBundle is the second lifecycle event in which the AWS CodeDeploy agent pulls the application revision to the temporary location ( /opt/codedeploy-agent/deployment-root/deployment-group-id/deployment-id/ ) . This event can't be mapped with any script, because it is reserved for the AWS CodeDeploy. 
  • BeforeInstall: This deployment life cycle event can be used for preinstalling tasks, such as decrypting files and creating a backup of the current version.
  • Install: During this life cycle event, the CodeDeploy agent copies the revision files from the temporary location to the final destination folder. This event can't be used to run scripts.
  • AfterInstall: This life cycle event can be mapped to a script, which will perform tasks such as configuring your application or changing file permissions.
  • ApplicationStart: This life cycle event is used to restart services that were stopped during ApplicationStop.
  • ValidateServiceThis life cycle event is used to verify that the deployment was completed successfully.

By seeing the preceding syntax, we can write one sample AppSpec.yml as follows:

version: 0.0 
os: linux
files:
- source: /
destination: /srv/mynodeapp
hooks:
ApplicationStop:
- location: scripts/stopnode.sh
timeout: 300
runas: root
ApplicationStart:
- location: scripts/start_nodeserver.sh
timeout: 300
runas: root
ValidateService:
- location: scripts/MonitorService.sh
timeout: 3600
runas: root
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.189.182.96