Question

Difficulty: MediumAWS Elastic Beanstalk

A developer is deploying an update to a Python application hosted on AWS Elastic Beanstalk using the Amazon Linux 2023 platform. The deployment requires running a database migration script that is packaged inside the application source code. This script must run after the application source archive is extracted to the staging directory, but before the application version is deployed and the web server is restarted. Which approach should the developer use to run the script at the correct stage?

  1. Place the migration script in the .platform/hooks/predeploy/ directory of the application source bundle.Answer
  2. B
    Place the migration script in the .ebextensions/ directory and configure it under the commands section of a configuration file.
  3. C
    Place the migration script inside a directory named ebextensions/ (without a leading dot) at the root of the source bundle.
  4. D
    Place the migration script in the root directory and specify its execution in the BeforeInstall hook of an appspec.yml file.

Answer

Place the migration script in the .platform/hooks/predeploy/ directory of the application source bundle.
The correct answer is to place the script in the .platform/hooks/predeploy/ directory. In modern Elastic Beanstalk platforms (Amazon Linux 2 and Amazon Linux 2023), developers can run custom scripts at specific lifecycle events using platform hooks. Scripts placed in the .platform/hooks/predeploy/ folder are executed after the application archive is extracted to the staging folder but before the application version is deployed and the web server is restarted. This is the correct phase for running database migrations or setting up environment-specific configuration files.

Step-by-Step Solution

1
Identify the target platform environment and configuration requirements.
The application runs on Amazon Linux 2023, which supports platform hooks and .ebextensions.
Platform hooks provide a structured way to run scripts at specific lifecycle events on modern Elastic Beanstalk platforms.
2
Evaluate the execution timing of different hooks.
The predeploy platform hook runs after the application source archive is extracted but before the application is deployed.
This matches the requirement to run the migration script on the extracted source files before the new version becomes active.
3
Package the script in the correct directory.
Creating the directory .platform/hooks/predeploy/ and putting the executable script in it ensures execution at the correct stage.
Elastic Beanstalk automatically executes any scripts located in this specific platform directory during deployment.

Key Concept

AWS Elastic Beanstalk Platform Hooks
Estimated Time:1m 30s
Rate this question