Configure mount scripts in application lifecycles

更新时间:
复制 MD 格式

When you deploy applications on Elastic Compute Service (ECS) instances, you often need to run setup or cleanup tasks at specific lifecycle stages -- for example, pulling configuration before startup or flushing logs after shutdown. Mount scripts in Enterprise Distributed Application Service (EDAS) let you attach custom shell scripts to six lifecycle events. EDAS runs each script automatically when the corresponding event occurs.

Common use cases:

  • Set up environment variables or host configurations when an instance joins an application

  • Pull configuration from a config server before the application starts

  • Register or deregister a service with a service registry during startup or shutdown

  • Flush logs or clean up temporary files after the application stops

Lifecycle stages and script types

The following diagram shows the execution flow across all lifecycle stages.

Script execution flow

EDAS provides six script types, each triggered at a different lifecycle stage:

Script typeTrigger timingFrequencyTypical use case
Prepare Instance ScriptAfter an ECS instance is added to an applicationOnce per instanceSet environment variables, update /etc/hosts
Pre-launch ScriptBefore each application container start (for example, before start.sh launches Tomcat)Every startPull configuration files, validate dependencies
Post-launch ScriptAfter each application container start. If a health check URL is configured, runs after the health check passes. If no health check URL is configured, runs after the port opens.Every startRegister with a service registry, send notifications
Pre-stop ScriptBefore each application container stopEvery stopDeregister from a service registry, flush in-memory data
Post-stop ScriptAfter each application container stopEvery stopClean up temporary files, flush logs
Destroy Instance ScriptAfter an ECS instance is removed from an applicationOnce per instanceRemove application-specific configurations
Note

The Prepare Instance Script provides an application-level trigger point, unlike cloud-init which runs during VM initialization. Use it for application-specific setup that depends on the instance having joined the application.

Important

For the Post-launch Script, do not rely on the port-open trigger. The application container typically opens its port before the application finishes starting, so the script may run before the application is fully ready. Configure a health check URL for a more reliable trigger.

Each script runs in a separate process from the application process. Scripts do not block the lifecycle transition of other instances -- EDAS waits for the script to complete on the current instance before proceeding with that instance's lifecycle.

Limits

ItemLimit
UserScripts run as the root user
Timeout600 seconds. Scripts that exceed this limit fail with a timeout error.
Size8 KB. For larger scripts, upload them to Object Storage Service (OSS) and download them at runtime. See the example below: download and run a large script from OSS.
IsolationEach script runs in a separate process from the application process
Execution methodScripts run through the ECS Send Remote Commands feature using the CoCustomHookScript command
IdempotencyAvoid duplicate configuration items in a script to prevent repeated execution

Error handling

A script succeeds when it returns exit code 0 (Linux/UNIX standard) and fails when it returns a non-zero exit code.

Failure ignoring settingBehavior on script failure
EnabledThe failed script does not interrupt the application change process.
DisabledThe change process pauses. If the health check URL passes, EDAS retries the script repeatedly.

When failure ignoring is disabled, use one of these methods to resume the change process:

  • Fix the script and retry the change task.

  • For Post-launch Script only: click Skip to enable failure ignoring and continue the change process.

Configure a mount script

Before you begin, make sure that you have:

  • An EDAS application deployed on ECS instances

  • Access to the EDAS console with permissions to manage the target application

To configure a mount script:

  1. Log on to the EDAS console.

  2. In the left-side navigation pane, choose Application Management > Applications. In the top navigation bar, select a region. In the upper part of the Applications page, select a microservices namespace from the Microservices Namespace drop-down list. Then, click the name of the application that you want to manage.

  3. Open the mount script settings:

    • At the application level: On the Basic Information tab, click Mount Script in the upper-right corner of the Application Settings section.

    • At the instance level: Click the Instance Information tab, and select Mount Script from the Group Settings drop-down list.

  4. In the Mount Script dialog box, click the tab for the script type, enter the script content in the text box, and then click Modify.

Mount script configuration dialog

Example: set environment variables with a Prepare Instance Script

#!/bin/bash
# Set application-specific environment variables when an instance joins

# Add environment variables to .bashrc so they persist after application start
echo 'export APP_ENV=production' >> /home/admin/.bashrc
echo 'export LOG_LEVEL=info' >> /home/admin/.bashrc

# Apply the changes
source /home/admin/.bashrc

exit 0
Note

To configure environment variables that persist after the application starts, add them to the /home/admin/.bashrc file.

Example: download and run a large script from OSS

If the script exceeds the 8 KB size limit, host it externally and download it at runtime:

#!/bin/bash
# Download and run a script from OSS
curl -o /tmp/setup.sh https://<your-bucket>.<region-id>.aliyuncs.com/scripts/setup.sh
chmod +x /tmp/setup.sh
/tmp/setup.sh

exit $?

Replace the following placeholders with actual values:

PlaceholderDescriptionExample
<your-bucket>OSS bucket namemy-scripts-bucket
<region-id>Region of the OSS bucketcn-hangzhou

Example: deregister from a service registry with a Pre-stop Script

#!/bin/bash
# Deregister the current instance from the service registry before shutdown

INSTANCE_IP=$(hostname -I | awk '{print $1}')
SERVICE_NAME="my-service"
REGISTRY_URL="http://registry.example.com/api/deregister"

curl -s -X POST "${REGISTRY_URL}" \
  -H "Content-Type: application/json" \
  -d "{\"service\": \"${SERVICE_NAME}\", \"ip\": \"${INSTANCE_IP}\"}"

exit $?

View execution results

After you modify a mount script, view the application changes to check the execution result.

The following figure shows the execution result of a Prepare Instance Script.

Prepare Instance Script execution result

Check the InvocationId field in the change logs for the execution status of the remote command.

FAQ

How do I handle scripts larger than 8 KB?

Upload the script to an external storage service such as OSS, then write a short mount script that downloads and runs it. See the example below: download and run a large script from OSS.

What happens when a script fails?

The behavior depends on the failure ignoring setting. When enabled, the change process continues regardless of the script result. When disabled, the change process pauses and EDAS retries the script. See the Error handling section above.

How do I find the cause of a script failure?

View the application changes for the script execution result. The InvocationId field in the change logs links to the remote command execution status.