Configure node lifecycle actions in AWS PCS
You define node lifecycle actions in your compute node group configuration. This topic describes how to define a script, control whether it re-runs on reboot, store it, pass it arguments, handle failures, cache it, validate its integrity, and read its logs.
Agent version requirement
Node lifecycle actions require AWS PCS agent version 1.5.0-1 or later. If your compute nodes use a custom AMI with an older agent version, update the agent before you configure lifecycle actions. For more information, see AWS PCS agent versions.
How to define a script
Each script has a name, a scriptSource, optional
arguments, an onError policy, and an executionPolicy. The
location and integrity fields are grouped under scriptSource.
{ "name": "My script", "scriptSource": { "scriptLocation": "s3://my-bucket/my-script.sh", "s3VersionId": "optional, S3 only", "checksum": "optional 64-char SHA-256 hex" }, "arguments": ["arg1", "arg2"], "onError": "TERMINATE", "executionPolicy": "FIRST_BOOT_ONLY" }
Add lifecycle actions to a compute node group
You can add lifecycle actions when you create or update a compute node group. Use the AWS Management Console or the AWS CLI.
Controlling reboot behavior
Set executionPolicy per script to control whether it re-runs on reboot.
Value |
Behavior |
When to use |
|---|---|---|
|
Run the script once, on the node's first boot. Skip it on every subsequent reboot. |
One-time setup, such as installing packages, joining a domain, or initializing storage. You don't have to make these scripts idempotent. |
|
Run the script on first boot and on every reboot. |
Configuration that must be reapplied after a restart, such as re-mounting an ephemeral file system or reasserting node state. |
Scripts set to FIRST_BOOT_ONLY run once and are skipped on reboot. Scripts
set to EVERY_BOOT run on every boot and should be idempotent (safe to run more than
once with the same result).
Script storage locations
You can store scripts in Amazon S3 or serve them over HTTPS. Scripts can't be uploaded inline through the API, and each script must be no larger than 2 MiB — the agent rejects any script that exceeds this size.
-
Amazon S3 (
s3://) — The instance IAM profile must havebucket/keys3:GetObjecton the object. With an S3 gateway VPC endpoint, nodes in private subnets can retrieve scripts through the VPC endpoint. Pin a specific version withscriptSource.s3VersionId(S3 locations only). -
HTTPS (
https://) — The host must be publicly readable (no authentication) and the node needs outbound internet access (internet gateway, NAT gateway, or HTTP proxy). This is useful for scripts hosted on GitHub or other public repositories.hostname/path
External storage provides version control, auditability, and reuse across teams. Inline or uploaded scripts are not supported.
Passing arguments to scripts
Pass arguments as an ordered array. They reach your script as positional command-line parameters.
arguments: ["fs-12345678", "/shared", "nfs4"] # script.sh fs-12345678 /shared nfs4 ($1=fs-12345678, $2=/shared, $3=nfs4)
The agent exports environment variables to every script. These variables contain cluster and node metadata.
Variable |
Description |
|---|---|
|
Cluster identifier / name |
|
Compute node group identifier / name |
|
Node identifier |
|
|
#!/usr/bin/env bash echo "Configuring node $PCS_NODE_ID in cluster $PCS_CLUSTER_NAME"
Error handling
Each script has an onError field that controls what happens when
a script exits non-zero or is killed by a signal.
Value |
Behavior |
When to use |
|---|---|---|
|
Mark the node failed and terminate it. |
Critical setup (storage mounts, Active Directory join). Prevents paying for broken nodes. |
|
Stop the remaining scripts in the stage; leave the node running. |
Debugging — inspect the instance after a failure. |
|
Log the error and run the next script. |
Optional or best-effort tasks. |
A script fails if it exits non-zero or is killed by a signal (for example,
SIGSEGV). Scripts are not retried. If an
operation might experience transient failures, add retry logic inside the script.
Script retrieval (download), however, is retried — up to 3 attempts with
exponential backoff (approximately 17 seconds maximum) — before the onError
behavior applies.
Script caching and updates
The agent downloads each script to the instance on first boot and stores it locally.
Two fields control behavior on reboot: scriptCachingPolicy controls
re-download and executionPolicy controls re-execution.
-
CACHE_ONCE(default) — Download once at first boot; never refetch. Behavior is identical across reboots. Updating script content requires instance replacement. -
REFRESH_ON_REBOOT— Re-download on every reboot, overwriting the cache. This lets you ship script fixes through a reboot. It requires network access on each boot; if a refresh fails, the download is treated as a retrieval failure and the script'sonErrorbehavior applies (there is no fallback to the cached copy). A refreshed script only actually re-runs on reboot if itsexecutionPolicyisEVERY_BOOT.
The lifecycle configuration (which scripts run, their arguments, error
handling, and execution policy) is always immutable per instance. Changing it through
UpdateComputeNodeGroup affects only new instances
and triggers the DRAIN strategy so running jobs finish before nodes are
replaced.
Script integrity (checksums)
Optionally provide a SHA-256 checksum in a script's scriptSource,
as a 64-character hexadecimal string. The agent validates it on download; a mismatch is treated
as a download failure and triggers onError. We recommend a checksum for production,
especially for scripts from shared or external sources.
sha256sum mount-efs.sh # use the 64-char hex hash as the checksum value
Logging and debugging
Each script writes to its own log file, and the agent keeps its own operational log.
# agent: download, caching, checksum, orchestration /var/log/amazon/pcs/lifecycle/actions/executor.log # each script's stdout/stderr /var/log/amazon/pcs/lifecycle/actions/<stage>/<script-name>.log
The log filename uses the script name as you defined it. Spaces are preserved. For example, a
script named Mount EFS home directory writes to Mount EFS home
directory.log. Connect with SSH or
AWS Systems Manager Session Manager to read them — both are available by the
nodeBootstrapped stage. Because nodes that fail with TERMINATE are replaced,
forward logs off-instance for debugging after termination: add a node bootstrapped script that configures the
Amazon CloudWatch agent to ship the lifecycle log directory to Amazon CloudWatch Logs. The AWS-maintained
configure-cloudwatch-logs.sh script does this. For more information, see Use AWS-maintained scripts for node lifecycle actions in AWS PCS.