Customize Java Deployments with Platform Hooks¶
This recipe explains how to run custom scripts during the Elastic Beanstalk deployment lifecycle using .platform/hooks/. Use platform hooks when you need deterministic operating-system-level changes that do not belong inside the application JAR.
Prerequisites¶
- Running Java Elastic Beanstalk environment on Amazon Linux 2023.
- Familiarity with deployment bundles and source-controlled configuration.
- Shell scripting basics for Linux deployment hooks.
What You'll Build¶
You will build:
- Prebuild, predeploy, or postdeploy scripts for the Java platform.
- Repeatable customization for directories, permissions, or runtime-side files.
- A safe pattern that keeps application code separate from host configuration logic.
flowchart TD
A[Source Bundle] --> B[.platform/hooks/prebuild]
B --> C[.platform/hooks/predeploy]
C --> D[Deploy Spring Boot App]
D --> E[.platform/hooks/postdeploy] Steps¶
- Create the hook directory structure.
- Add a predeploy script that prepares an application directory.
#!/bin/bash
set -euo pipefail
mkdir -p /var/app/current/runtime
chown webapp:webapp /var/app/current/runtime
- Add a postdeploy script that records the deployed version.
- Make sure scripts are executable before packaging.
chmod +x .platform/hooks/predeploy/10-prepare-runtime.sh
chmod +x .platform/hooks/postdeploy/50-record-deploy.sh
- Deploy the updated application bundle.
Verification¶
Use these checks after deployment:
Expected outcomes:
- Hook scripts run in the expected lifecycle order.
- The deployment succeeds without manual instance changes.
- Files created by the hooks exist on the host after deployment.
- Customization remains in source control with the application bundle.