Lesson 4 of 5 AWS CloudFormation

Extending CloudFormation with Nested Stacks, Custom Resources and Hooks

This lesson explains how larger environments split templates into reusable pieces, extend CloudFormation with custom logic, and enforce rules before resources are provisioned.

Learning outcomes

After completing this lesson, you will be able to:

  • Use nested stacks.
  • Use custom resources and modules.
  • Enforce rules with Hooks and Guard.

Nested stacks

A nested stack is an AWS::CloudFormation::Stack resource that points to another template in S3. The parent passes parameters and reads the child's outputs with !GetAtt Child.Outputs.Name. Nested stacks package reusable components, such as a standard VPC or load balancer, and let large stacks exceed per-template resource limits (500 resources per stack). Operations are performed on the root stack; updating a child directly is not recommended.

Custom resources and modules

A custom resource (Custom::Name or AWS::CloudFormation::CustomResource) invokes a Lambda function or SNS topic during create, update and delete, letting templates perform actions CloudFormation does not support, such as emptying a bucket before deletion or looking up a value from an external system. The function must send a response to the pre-signed S3 URL in the request; if it crashes without responding, the stack waits until the timeout (up to an hour by default, configurable with ServiceTimeout). Modules package reusable resource configurations in the registry, and third-party resource types extend CloudFormation to other providers.

The following diagram shows a custom resource's request and response.

How a custom resource works. During create, update or delete, CloudFormation invokes a Lambda function with a request that includes a pre-signed S3 URL. The function does the work and must send its response to that URL. If it crashes without responding, the stack waits until the timeout, up to an hour by default, configurable with ServiceTimeout.
The resource name is illustrative. Functions should catch errors and still send a FAILED response.
True or false

Custom resources

Exam skills 3.1.2 · 3.1.3

Check how custom resources work.

Log in to try this activity Interactive activities are free with an account.

Hooks and Guard

CloudFormation Hooks run checks before resources are created, updated or deleted and can fail or warn about non-compliant configurations, for example blocking unencrypted volumes. Hooks can be written with CloudFormation Guard rules, Lambda functions or managed proactive controls from AWS Control Tower. Guard can also validate templates in pipelines before deployment. The following table summarises these extension mechanisms.

MechanismPurpose
Nested stacksReuse templates and stay within resource limits
Custom resourcesRun custom logic during stack operations
Modules and registry typesReusable or third-party resource definitions
Hooks / GuardBlock non-compliant resources before provisioning
Sort into groups

Proactive or detective?

Exam skills 3.1.2 · 4.1.5

Decide whether each control checks resources before or after deployment.

Log in to try this activity Interactive activities are free with an account.
Worked example

A stack stuck deleting

Deleting a stack hangs for an hour on a custom resource, then reports DELETE_FAILED. The custom resource's Lambda function was deleted earlier by another team, so no response is ever sent. The operator deletes the stack again, choosing to retain the custom resource, which CloudFormation then skips, and the stack deletes. Future custom resources use a ServiceTimeout of five minutes and functions that always send a response, even on errors.

Check yourself

How should a nested stack be updated?

Show answer

Through its root stack, not directly.

Exam Tips

Remember for the exam

  • Nested stacks reuse templates; manage them through the root stack.
  • Custom resources must send a response to CloudFormation, or the stack waits until timeout.
  • Hooks and Guard block non-compliant resources before they are provisioned.