Setting up the AppSpec file

As mentioned earlier, the AppSpec, or the application specifications file, is basically a YAML or JSON backed file used to define life cycle hooks for a particular deployment. In this case, with our EC2 instance prepped and ready with the CodeDeploy agent, we still need the AppSpec file to define a set of dependencies that will essentially install the necessary packages on the EC2 instance, start or stop the services, change permissions, and much more.

The AppSpec file comprises the following sections:

  • Version: The version of the AppSpec file. Currently, the version number supported with CodeDeploy is 0.0. Do not change this value.
  • OS: Specifies the operating system of the underlying EC2/on-premise instance.
  • Files: Specifies the files that need to be copied on the instance at the time of the deployment process. You can additionally specify a source and a destination folder as well for your applications here.
  • Hooks: Hooks essentially specify when a particular deployment life cycle has to be triggered. There are four main types of hooks: BeforeInstall, AfterInstall, ApplicationStart, and ApplicationStop. Each hook further requires additional parameters, such as the location of the scripts to execute, a timeout value, and how the scripts should be runas.

Here is a representation of a simple AppSpec file for our WordPress deployment. Since we are using the Amazon Linux-backed instance, the os value is set to linux along with other essential parameters, such as files and hooks. Note in our case, we are configuring the WordPress application files to be copied from their default location over to /var/www/html/WordPress as well:

version: 0.0 
os: linux 
files: 
  - source: / 
    destination: /var/www/html/WordPress 
hooks: 
  BeforeInstall: 
    - location: scripts/install_dependencies.sh 
      timeout: 300 
      runas: root 
  AfterInstall: 
    - location: scripts/change_permissions.sh 
      timeout: 300 
      runas: root 
  ApplicationStart: 
    - location: scripts/start_server.sh 
      timeout: 300 
      runas: root 
  ApplicationStop: 
    - location: scripts/stop_server.sh 
      timeout: 300 
      runas: root 

To create this file, login to your development server and not your CodeDeploy instance. Once logged into the development server, open the WordPress application directory that we used during the CodeCommit sections of this chapter. This is the same directory that we used to sync with our master CodeCommit repository as well:

  1. Here, at the application's root directory, create a blank appspec.yml, and copy-paste the appspec contents explained earlier:
# vi appspec.yml 
  1. With the AppSpec file created, we now move on to create the individual files for the life cycle hooks. To do so, create a folder named scripts within the WordPress application directory and create each of these individual shell scripts within it:
# mkdir scripts
  1. Create the install_dependencies.sh script that will essentially install the necessary packages required to run WordPress on an instance:
# vi scripts/install_dependencies.sh 
#!/bin/bash 
sudo yum install -y httpd mysql mysql-server php 
sudo yum install -y php-mysql 
  1. Similarly, create the change_permissions.sh script that modifies the permissions of the files present in the scripts folder to executable:
# vi scripts/change_permissions.sh 
#!/bin/bash 
chmod -R 755 /var/www/html/WordPress 
  1. And, finally, create the start_server.sh and the stop_server.sh scripts that will start and stop the httpd and mysql services on the deployment EC2 instances:
# vi scripts/start_server.sh 
#!/bin/bash 
service httpd start 
service mysqld start 

# vi scripts/stop_server.sh #!/bin/bash isExistApp=`pgrep httpd` if [[ -n $isExistApp ]]; then service httpd stop fi isExistApp=`pgrep mysqld` if [[ -n $isExistApp ]]; then service mysqld stop fi

Got this far? Awesome! We are almost done with the AppSpec files, with just one small step left: uploading these changes to our CodeCommit repository!

To do so, run the following set of commands from the deployment server:

# git add * 
# git commit -m "Added scripts directory with AppSpec file!" 
# git push -u origin <NEW_BRANCH_NAME>

Et voila! The WordPress application and our deployment scripts are all uploaded to our CodeCommit branch and ready for deployment! In the next section, we will create and configure an application and deployment group for our CodeDeploy.

..................Content has been hidden....................

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