Sceptre中如何将变量/解析器作为钩子参数?如何用CloudFormation输出传参给!cmd钩子?
Hey there! Let's break down your two Sceptre questions clearly—they're both common scenarios when working with hooks, so I’ll walk you through each with concrete examples.
You absolutely can pass variables or resolvers as arguments to Sceptre hooks! Sceptre evaluates hooks within the same template rendering context as your stack config, so you can use the familiar template variable syntax ({{ variable_name }}) or resolvers directly in your hook commands.
Example with Variables
First, define your variables in your stack config file:
variables: service_name: my-api deployment_stage: production
Then reference them directly in your hook:
hooks: before_deploy: - !cmd "echo Deploying {{ service_name }} to {{ deployment_stage }} environment"
When Sceptre runs this hook, it’ll replace the placeholders with the actual variable values automatically.
Example with Resolvers
Resolvers work the same way—they get resolved before the hook executes, so you can use them to pull dynamic values (like from dependent stacks) into your hook commands. For example, using !stack_output to fetch an output from a network stack:
hooks: after_create: - !cmd "echo Connecting to VPC ID: {{ !stack_output network/vpc::VpcId }}"
This will resolve the VpcId output from the network/vpc stack and inject it into the echo command.
Yes, this is totally feasible! The trick is using the !stack_output resolver to reference the CloudFormation output, and ensuring you run the hook at the right stage (after the stack has been created/updated, so outputs exist).
Using Outputs from the Current Stack
To reference an output from the same stack you’re deploying, use . as the stack path in !stack_output (this represents the current stack). For example, if your CloudFormation template has an output named InstancePublicIp:
hooks: after_create: - !cmd "echo The public IP of your new instance is: {{ !stack_output .::InstancePublicIp }}"
The after_create hook runs after the stack is fully created, so the output will be available at this point.
Using Outputs from Another Stack
If you need an output from a different stack, just specify the stack’s path (relative to your Sceptre project root) in the !stack_output resolver:
hooks: after_update: - !cmd "echo Backing up data to S3 bucket: {{ !stack_output storage/data-bucket::BucketName }}"
This pulls the BucketName output from the storage/data-bucket stack and uses it in your command.
Key Tips
- Hook Timing: Don’t try to reference current stack outputs in
before_createorbefore_updatehooks—those outputs won’t exist yet until the stack is created/updated. Stick toafter_createorafter_updatefor current stack outputs. - Validate with
sceptre plan: Runsceptre plan <stack-path>first to see the rendered hook command (with resolved values) before executing it. This helps catch any syntax issues or missing outputs early.
内容的提问来源于stack exchange,提问作者Scott Odle

