Sub topic 3
Assets, protection, and delivery with AWS CDK
Assume you know basic S3, Lambda, IAM, TypeScript or Python, and terminal commands. This chapter focuses on the choices around a CDK application after you define its first resources.
Assets: bundling Lambda code and files
An asset is a local file or directory that CDK uploads for a resource. For Lambda, Code.fromAsset("lambda") packages the lambda directory and publishes it through the CDK deployment process. The function's handler must exist inside that directory.
For dependencies, bundle them during synthesis with a tool such as NodejsFunction, or build the directory before running cdk deploy. Keep generated files out of the asset unless the function needs them.
Protecting stateful resources
A removal policy controls what happens when CDK removes a resource from the stack. Stateful resources include S3 buckets, databases, and queues containing important data.
const bucket = new s3.Bucket(this, "Uploads", {
versioned: true,
removalPolicy: cdk.RemovalPolicy.RETAIN,
});RETAIN protects data, but the retained resource can remain after the stack is deleted. Use DESTROY only for disposable development resources.
Sharing values across stacks
Pass a resource or property between stacks when stacks have a clear dependency. CDK creates CloudFormation references for values such as bucket names. These values are tokens: placeholders that resolve during synthesis or deployment rather than ordinary strings in your program.
Avoid excessive cross-stack references. They make independent deployment and deletion harder. Prefer a small shared stack or explicit configuration for broadly shared values.
Reusable constructs
A custom construct groups resources and their defaults behind a simple interface. For example, a SecureBucket construct can always enable encryption, block public access, and require SSL. Expose only the properties that consumers need.
Configuration and tagging
Use context, environment variables, or stack properties for account-specific settings. Do not put credentials in context files. Set the target account and region explicitly for production deployments.
Tags applied to a construct tree can add cost-center or owner metadata to supported resources. Aspects can inspect or modify many constructs, but test their effect because not every resource supports every tag or property.
Testing and delivery
The assertions module synthesizes a stack in memory and checks its template without deploying it. Test security settings, important properties, and resource counts.
A CDK Pipeline uses a source repository and deployment stages to synthesize, test, and deploy stacks automatically. Add approval steps before production.
Best practices
- Keep logical IDs stable unless replacement is intentional.
- Grant permissions with resource grant methods.
- Review synthesized templates and pipeline changes.
- Use separate environments and least-privilege roles.
- Test custom constructs and protect production data.
Exercise
Why might RETAIN be safer than DESTROY for a production S3 bucket?
Show answer
RETAIN keeps the bucket and its data when the stack is deleted, allowing recovery or deliberate cleanup later.