How do I customize my Elastic Beanstalk environment using platform hooks?

3 minute read
0

I want to customize my AWS Elastic Beanstalk environment using platform hooks to create custom scripts or other executable files. I want to deploy these on my Amazon Elastic Compute Cloud (Amazon EC2) instances running in the environment.

Short description

You can use platform hooks to extend the functionality of the Elastic Beanstalk environment to run custom scripts and executable files. Elastic Beanstalk can also run these hooks during various instance provisioning phases.

Note: Platform hooks aren't supported on Amazon Linux AMI platform versions (preceding Amazon Linux 2).

There are two types of platform hooks:

  • Application deployment platform hooks run during an application deployment when you provide a new source bundle for deployment. They also run when you make a configuration change that requires termination and recreation of all environment instances.
  • Configuration deployment platform hooks run during a configuration deployment when you make configuration changes that only update environment instances without recreating them.

Resolution

Set up your .platform directory

1.    In the root of your application bundle, create a hidden directory that's named ‘.platform/hooks’or ‘.platform/confighooks’for application deployment and configuration deployment platform hooks respectively.

2.    Depending on the instance provisioning stage when they run, you must place hook files in one of the following subdirectories:

  • prebuild
  • predeploy
  • postdeploy

To identify the subdirectories that are required for your use case, see Extending Elastic Beanstalk Linux platforms.

3.    Your application source bundle must look similar to the following bundle:

~/workspace/my-application/
|-- .platform
    |--hooks
       |--prebuild
          |-- custom-prebuild-script.sh
       |--predeploy
          |-- custom-predeploy-script.sh
       |--postdeploy
          |-- custom-postdeploy-script.sh
|-- .ebextensions
|-- index.php
         `-- styles.css

Specify the hook files

When creating the hook files, keep the following information in mind:

  • Hook files can be binary files or script files that start with a ‘#!’ line, and contain their interpreter path, such as ‘#!/bin/bash’. For an example of contents inside a script file, such as ‘custom-postdeploy-script.sh, see the following file:
#!/bin/bash
sudo su
cd /var/app/current
echo "Hello from AWS hook" >> 'test.txt'
  • Use “chmod +x” on your local machine to set execute permissions on your hook files. Or, you can use ‘.ebextensions’ to provide the permissions. See the following example file:
container_commands:
  01_chmod1:
    command: "chmod +x .platform/hooks/postdeploy/custom-postdeploy-script.sh"

For more information, see How do I customize my Elastic Beanstalk environment using .ebextensions?

Apply the platform hooks to your application

1.    Create an application source bundle that includes the preceding platform hooks and configuration files.

Note: File browsers can hide folders that start with a period, such as .platform and .ebextensions. To keep these folders visible, include these folders in the root of your application bundle when you create your application source bundle.

2.    Deploy your updated Elastic Beanstalk application.


AWS OFFICIAL
AWS OFFICIALUpdated a year ago