How do I use platform hooks to customize my Elastic Beanstalk environment?

2 minute read
0

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

Resolution

Note: Platform hooks aren't supported on Amazon Linux Amazon Machine Image (AMI) platform versions earlier than Amazon Linux 2. Elastic Beanstalk automatically grants execute permissions to all of the platform hook scripts for Amazon AL2023 and Amazon Linux 2.

Set up your .platform directory

Complete the following steps:

  1. In the root of your application bundle, create a hidden directory that's named .platform/hooks for application deployment platform hooks or .platform/confighooks for configuration deployment platform hooks.
  2. Based on the instance provisioning stage when the platform hook files run, you must put the hook files in either the prebuild, predeploy, or postdeploy subdirectory.
    For more information about each subdirectory, see Application deployment platform hooks.
  3. Your application source bundle must be 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

Hook files can be binary files or script files that start with a #! line, and contain their interpreter path, such as #!/bin/bash.

Example of contents inside a custom-postdeploy-script.sh script file:

#!/bin/bash
sudo su
cd /var/app/current
echo "Hello from AWS hook" >> 'test.txt'

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

Apply the platform hooks to your application

Complete the following steps:

  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 the folders visible, include the 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 7 months ago
3 Comments

from https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/platforms-linux-extend.html#platforms-linux-extend.hooks For all Amazon Linux 2023 and Amazon Linux 2 based platforms versions that were released on or after April 29, 2022, Elastic Beanstalk automatically grants execute permissions to all of the platform hook scripts. In this case you don't have to manually grant execute permissions.

replied 8 months ago

Thank you for your comment. We'll review and update the Knowledge Center article as needed.

profile pictureAWS
MODERATOR
replied 8 months ago

Where should I look for the echo logs printed from the hook script. For example if I have the line echo "this is a log" in the script and have cloudwatch agent setup, under which log group will I find that echo output?

replied 7 months ago