Skip to content

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

  1. Create the hook directory structure.
.platform/
└── hooks/
    ├── prebuild/
    ├── predeploy/
    └── postdeploy/
  1. 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
  1. Add a postdeploy script that records the deployed version.
#!/bin/bash
set -euo pipefail

date --iso-8601=seconds > /var/app/current/runtime/deployed-at.txt
  1. 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
  1. Deploy the updated application bundle.
eb deploy --staged

Verification

Use these checks after deployment:

eb logs --all
eb events

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.

See Also

Sources