<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Kenta Goto</title>
    <description>The latest articles on DEV Community by Kenta Goto (@k_goto).</description>
    <link>https://dev.to/k_goto</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1044356%2F0bb8e3c0-34a0-4252-9527-6d317162f17c.JPG</url>
      <title>DEV Community: Kenta Goto</title>
      <link>https://dev.to/k_goto</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/k_goto"/>
    <language>en</language>
    <item>
      <title>Supercharge AWS Emulator for AWS CDK Development with cdkd</title>
      <dc:creator>Kenta Goto</dc:creator>
      <pubDate>Sun, 15 Nov 2026 06:36:20 +0000</pubDate>
      <link>https://dev.to/aws-heroes/supercharge-aws-emulator-for-aws-cdk-development-with-cdkd-2bbd</link>
      <guid>https://dev.to/aws-heroes/supercharge-aws-emulator-for-aws-cdk-development-with-cdkd-2bbd</guid>
      <description>&lt;h2&gt;
  
  
  TL;DR
&lt;/h2&gt;

&lt;p&gt;When you use AWS Emulator for development with AWS CDK, combining it with cdkd gives you the following benefits:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;cdkd automatically configures the connection to AWS Emulator, and environment variables for Lambda and other resources are resolved automatically from your CDK code, so you don't need a &lt;code&gt;.env&lt;/code&gt; file&lt;/li&gt;
&lt;li&gt;You can freely switch the connection target per service, for example using real AWS for secrets and reference data/files while having the app write to AWS Emulator (no need to recreate real AWS data locally)&lt;/li&gt;
&lt;li&gt;Application code such as Lambda functions runs directly from source without being deployed to AWS Emulator, so you can try out changes right away without redeploying&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  AWS Emulator and cdkd (CDK Direct)
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/aws/aws-emulator" rel="noopener noreferrer"&gt;AWS Emulator&lt;/a&gt; is a tool released in November 2026 that emulates AWS APIs locally. It is an emulator that lets you use various AWS resources virtually, without creating them in real AWS.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/go-to-k/cdkd" rel="noopener noreferrer"&gt;cdkd (CDK Direct)&lt;/a&gt;, on the other hand, is a tool that &lt;strong&gt;deploys CDK apps quickly by calling the AWS SDK directly, without using CloudFormation&lt;/strong&gt;. It is an alternative to the CDK CLI for deploying to real AWS environments, but it also provides a subcommand called &lt;code&gt;cdkd local&lt;/code&gt;, which lets you run application resources defined in your CDK app, such as Lambda, ECS, API Gateway, CloudFront, and Bedrock AgentCore, locally. For more details on cdkd, see the following article.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/aws-heroes/i-built-cdkd-deploy-your-existing-aws-cdk-code-up-to-15x-faster-4amg"&gt;I Built cdkd: Deploy Your Existing AWS CDK Code Up to 15x Faster&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With the &lt;code&gt;cdkd local&lt;/code&gt; command, only the application code runs locally. For the AWS resources the app accesses, you can either use your own emulators or mocks, or connect to resources in a real AWS environment. The latter works because &lt;strong&gt;cdkd automatically retrieves and resolves information about the resources the app accesses from your CDK code and deployed stacks&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Two Ways to Use AWS Emulator
&lt;/h2&gt;

&lt;p&gt;There are two main ways to use AWS Emulator. One is to create Lambda functions and other compute inside AWS Emulator and run them there. The other is to run the application code locally and have it access resources, such as tables and buckets, created inside AWS Emulator.&lt;/p&gt;

&lt;p&gt;With the former, Lambda in AWS Emulator runs on a built-in implementation rather than the official AWS runtime, so its behavior may differ from real Lambda. Also, although deployments themselves are fast, you have to redeploy to AWS Emulator every time you change the code. With the latter, on the other hand, you need to pass the connection settings for AWS Emulator and information such as the names of the tables to access to the app via environment variables.&lt;/p&gt;

&lt;p&gt;When you combine AWS Emulator with cdkd, you use the latter approach, and the responsibilities are split as follows: cdkd creates the resources inside AWS Emulator, and the application code runs in the &lt;code&gt;cdkd local&lt;/code&gt; runtime. The results of trying the former approach are summarized in the appendix at the end.&lt;/p&gt;




&lt;h2&gt;
  
  
  How to Use AWS Emulator + cdkd
&lt;/h2&gt;

&lt;p&gt;In this article, as an example, we use a stack &lt;code&gt;MyStack&lt;/code&gt; consisting of a Lambda function that reads from and writes to a DynamoDB table and an S3 bucket, with an API Gateway in front of it.&lt;/p&gt;

&lt;p&gt;We use AWS Emulator v1.0.322 and cdkd (&lt;code&gt;@cdkd/cli&lt;/code&gt;) v0.349.0. You can install cdkd with the following command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm install -g @cdkd/cli
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 1: Start AWS Emulator
&lt;/h3&gt;

&lt;p&gt;This step has nothing to do with cdkd. It covers installing and starting AWS Emulator itself (exactly as described in the official documentation).&lt;/p&gt;

&lt;h4&gt;
  
  
  Linux
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl -fsSLo aws-emulator.deb \
  https://downloads.aws.com/aws-emulator/packages/aws-emulator/LTS/latest/aws-emulator_amd64.deb
sudo apt-get install -y ./aws-emulator.deb

aws-emulator -b 0.0.0.0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  macOS
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="c"&gt;# Dockerfile (use aws-emulator-x86_64.rpm on Intel Macs)&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; public.ecr.aws/amazonlinux/amazonlinux:2023-minimal
&lt;span class="k"&gt;ADD&lt;/span&gt; https://downloads.aws.com/aws-emulator/packages/aws-emulator/LTS/latest/aws-emulator-aarch64.rpm /tmp/aws-emulator.rpm
&lt;span class="k"&gt;RUN&lt;/span&gt; rpm -i /tmp/aws-emulator.rpm &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; rm /tmp/aws-emulator.rpm \
    &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; mkdir -p /home/emulator &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; chown 1000 /home/emulator
&lt;span class="k"&gt;USER&lt;/span&gt; 1000
&lt;span class="k"&gt;ENV&lt;/span&gt; &lt;span class="nv"&gt;HOME&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;/home/emulator
&lt;span class="k"&gt;EXPOSE&lt;/span&gt; 9297
&lt;span class="k"&gt;ENTRYPOINT&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"aws-emulator"&lt;/span&gt;, &lt;span class="s2"&gt;"-b"&lt;/span&gt;, &lt;span class="s2"&gt;"0.0.0.0"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker build -t aws-emulator .
mkdir -p ~/.aws/emulator
docker run --rm -p 127.0.0.1:9297:9297 \
  -e &lt;span class="nv"&gt;HOME&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"$HOME"&lt;/span&gt; -v &lt;span class="s2"&gt;"$HOME/.aws/emulator:$HOME/.aws/emulator"&lt;/span&gt; \
  aws-emulator -p 9297
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 2: Create Resources in AWS Emulator
&lt;/h3&gt;

&lt;p&gt;To prepare for running the app code with &lt;code&gt;cdkd local&lt;/code&gt; in Step 3, create the resources the app uses inside AWS Emulator. AWS Emulator is empty right after it starts, so you need to create the tables, buckets, and other resources your code reads from and writes to in advance.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;cdkd deploy --aws-emulator
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To use AWS Emulator, you would normally need to set a number of environment variables for the connection, such as the endpoint, credentials, and certificates (see the &lt;a href="https://docs.aws.amazon.com/emulator/latest/userguide/point-sdk-to-emulator.html#point-sdk-env" rel="noopener noreferrer"&gt;official documentation&lt;/a&gt; for details). &lt;code&gt;--aws-emulator&lt;/code&gt; takes care of all of these settings automatically.&lt;/p&gt;

&lt;p&gt;In addition, when you pass &lt;code&gt;--aws-emulator&lt;/code&gt;, bootstrapping AWS Emulator is also done automatically under the hood, so you don't need to run &lt;code&gt;cdkd bootstrap&lt;/code&gt; (when deploying with cdkd to a real AWS environment instead of AWS Emulator, you need to run &lt;code&gt;cdkd bootstrap&lt;/code&gt; once beforehand).&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 3: Run the App with cdkd local
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;cdkd local&lt;/code&gt; synthesizes the CDK app, then loads the application code from your local source and runs it. It also reads handlers and API routes automatically from your CDK code, so no dedicated configuration file is needed. By passing &lt;code&gt;--aws-emulator&lt;/code&gt;, AWS calls from the app are sent to AWS Emulator.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# To start an API server (API Gateway) on localhost&lt;/span&gt;
cdkd &lt;span class="kd"&gt;local&lt;/span&gt; start-api --from-state --aws-emulator
curl &lt;span class="s1"&gt;'http://127.0.0.1:3000/notes?id=hello'&lt;/span&gt;

&lt;span class="c"&gt;# To invoke a Lambda function on its own&lt;/span&gt;
cdkd &lt;span class="kd"&gt;local&lt;/span&gt; invoke MyStack/Handler --from-state --aws-emulator --event event.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In practice, the AWS resources a Lambda function accesses are often defined in its environment variables. However, in CDK code (or in a CloudFormation template), those environment variables usually contain references (such as CloudFormation &lt;code&gt;Ref&lt;/code&gt; or &lt;code&gt;GetAtt&lt;/code&gt;) rather than the actual values of the target AWS resources (the real ARNs or URLs).&lt;/p&gt;

&lt;p&gt;So when you specify &lt;code&gt;--from-state&lt;/code&gt;, cdkd automatically resolves these references to their actual values using the information recorded in the cdkd stack state during the deployment in Step 2. In other words, &lt;strong&gt;developers don't need to define the information about the resources the app accesses in environment variables themselves&lt;/strong&gt; to run the app locally against the emulator.&lt;/p&gt;




&lt;h2&gt;
  
  
  Benefits of AWS Emulator + cdkd
&lt;/h2&gt;

&lt;p&gt;From here, I'll explain the three points listed at the beginning (&lt;code&gt;TL;DR&lt;/code&gt;).&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Connection Settings Are Configured Automatically, and Environment Variables for Lambda and Others Are Resolved from CDK Code, So No &lt;code&gt;.env&lt;/code&gt; Is Needed
&lt;/h3&gt;

&lt;p&gt;As briefly mentioned in Step 2 above, to use AWS Emulator, you need to set credentials, certificates, and other settings for connecting to it via environment variables (see the &lt;a href="https://docs.aws.amazon.com/emulator/latest/userguide/point-sdk-to-emulator.html#point-sdk-env" rel="noopener noreferrer"&gt;official documentation&lt;/a&gt; for details).&lt;/p&gt;

&lt;p&gt;With cdkd, you just add &lt;code&gt;--aws-emulator&lt;/code&gt; to &lt;code&gt;cdkd deploy&lt;/code&gt; or &lt;code&gt;cdkd local&lt;/code&gt;, and cdkd configures all of these automatically.&lt;/p&gt;

&lt;p&gt;Furthermore, as mentioned in Step 3 above, cdkd also automatically resolves the values of environment variables for application resources such as Lambda and ECS.&lt;/p&gt;

&lt;p&gt;If you run your app code locally without cdkd, you need to look up auto-generated resource names, ARNs, and so on, copy them into a &lt;code&gt;.env&lt;/code&gt; file, and rewrite it every time you recreate the resources. &lt;code&gt;cdkd local&lt;/code&gt; reads handlers and environment variables from your CDK code, and with &lt;code&gt;--from-state&lt;/code&gt;, it automatically converts references such as table names into their actual values.&lt;/p&gt;

&lt;p&gt;Thanks to this mechanism, using cdkd eliminates the need to prepare a &lt;code&gt;.env&lt;/code&gt; file. Even if you pass values such as passwords directly via environment variables, cdkd fetches them from Secrets Manager and injects them before startup, so you don't have to keep secret values in local files.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Freely Switch Between AWS Emulator and Real AWS Per Service
&lt;/h3&gt;

&lt;p&gt;With &lt;code&gt;cdkd local&lt;/code&gt;, you can switch the connection target for each AWS service or database the app accesses. That is, you can achieve flexible setups locally: everything on AWS Emulator, only some resources on real AWS, or all resources on real AWS with only the app code running locally.&lt;/p&gt;

&lt;p&gt;Passing &lt;code&gt;--aws-emulator&lt;/code&gt; makes every AWS service the app accesses point to AWS Emulator. On top of that, for only the services you want to connect to real AWS, set the value of &lt;code&gt;AWS_ENDPOINT_URL_{resource type}&lt;/code&gt; to &lt;code&gt;"real-aws"&lt;/code&gt; in the JSON file specified with &lt;code&gt;--env-vars&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For example, in the following example, only Secrets Manager and Cognito use real AWS resources, while DynamoDB, SQS, SNS, S3, and others keep accessing AWS Emulator. You can also split the settings per Lambda function or other unit (write the function name as the key instead of &lt;code&gt;Parameters&lt;/code&gt;).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"Parameters"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;"AWS_ENDPOINT_URL_SECRETS_MANAGER"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"real-aws"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nl"&gt;"AWS_ENDPOINT_URL_COGNITO_IDENTITY_PROVIDER"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"real-aws"&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As explained earlier, with &lt;code&gt;--from-state&lt;/code&gt;, table names, database endpoints, secret ARNs, and so on are resolved according to the connection target of each service. In the example above, information such as secret ARNs is resolved automatically from real AWS, while table names, queue names, and so on are resolved from AWS Emulator.&lt;/p&gt;

&lt;p&gt;Therefore, when you specify &lt;code&gt;"real-aws"&lt;/code&gt;, you need to run &lt;code&gt;cdkd deploy&lt;/code&gt; against real AWS as well beforehand and set up information such as secrets in advance.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Deploy the stack to both AWS Emulator and real AWS&lt;/span&gt;
cdkd deploy --aws-emulator
cdkd deploy &lt;span class="c"&gt;# After deploying, set values such as secrets&lt;/span&gt;

cdkd &lt;span class="kd"&gt;local&lt;/span&gt; invoke MyStack/Handler --from-state --aws-emulator --env-vars routing.json --event event.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In fact, by using &lt;code&gt;--from-cfn-stack&lt;/code&gt; instead of &lt;code&gt;--from-state&lt;/code&gt;, you can retrieve information from a CloudFormation stack deployed with the regular CDK CLI (&lt;code&gt;cdk deploy&lt;/code&gt;) rather than a stack deployed with cdkd. This is for those who have just installed cdkd and want to retrieve information from stacks they have been deploying and operating with the regular CDK CLI.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;cdkd &lt;span class="kd"&gt;local&lt;/span&gt; invoke MyStack/Handler --from-cfn-stack --aws-emulator --event event.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this resource switching feature, cdkd enables setups such as the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Switch the database between local and a development RDS

&lt;ul&gt;
&lt;li&gt;Keep separate JSON files for connecting to a local MySQL (Docker container) and to a development RDS, and switch between them with &lt;code&gt;--env-vars&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;To connect to something other than AWS resources, such as a local MySQL, write its endpoint in the &lt;code&gt;--env-vars&lt;/code&gt; file&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Use real AWS only for secrets

&lt;ul&gt;
&lt;li&gt;Read API keys for external services such as Stripe or OpenAI from Secrets Manager in real AWS&lt;/li&gt;
&lt;li&gt;No need to put the actual keys in &lt;code&gt;.env&lt;/code&gt; or load them into AWS Emulator, and no hassle of reloading them every time you restart AWS Emulator&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Use real AWS only for test data and test users

&lt;ul&gt;
&lt;li&gt;Use Cognito test users and S3 files from real AWS&lt;/li&gt;
&lt;li&gt;Write to DynamoDB and SQS on AWS Emulator&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Use real models with AI agents

&lt;ul&gt;
&lt;li&gt;Conversation state in DynamoDB (AWS Emulator), models in Bedrock (real AWS)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Currently, switching is done per AWS service, so you can't split it per table within the same DynamoDB. Also, connecting from your local machine to an RDS instance in a private subnet requires a separate tunnel, such as SSM port forwarding.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. No Redeployment Needed When You Modify App Code
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;cdkd local&lt;/code&gt; reads handlers, environment variables, API routes, and so on directly from your CDK code, and runs application code such as Lambda functions directly from source in Docker, without deploying it to AWS Emulator.&lt;/p&gt;

&lt;p&gt;When you emulate Lambda functions entirely inside AWS Emulator, they run on a copy of the code taken at deploy time, so you need to redeploy every time you make a fix. With &lt;code&gt;cdkd local&lt;/code&gt;, that isn't necessary, so you can rerun immediately after making changes.&lt;/p&gt;




&lt;h2&gt;
  
  
  Details of cdkd local + AWS Emulator
&lt;/h2&gt;

&lt;p&gt;For a more detailed explanation of the AWS Emulator integration, see the official cdkd documentation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://cdkd.dev/aws-emulator/index.html" rel="noopener noreferrer"&gt;AWS Emulator integration - cdkd documentation&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  You Can Do the Same with cdk-local
&lt;/h2&gt;

&lt;p&gt;The feature of &lt;code&gt;cdkd local&lt;/code&gt; that runs your code locally is also published as a standalone tool, &lt;a href="https://github.com/go-to-k/cdk-local" rel="noopener noreferrer"&gt;cdk-local&lt;/a&gt; (command name: &lt;code&gt;cdkl&lt;/code&gt;). If you use the CDK CLI and not cdkd, you can use this tool with AWS Emulator in the same way.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm install -g cdk-local

cdkl invoke MyStack/Handler --from-cfn-stack --aws-emulator --event event.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Resources are created with the regular &lt;code&gt;cdk deploy&lt;/code&gt;, and with &lt;code&gt;--from-cfn-stack&lt;/code&gt;, environment variables are resolved automatically from the deployed CDK stack (CloudFormation stack).&lt;/p&gt;




&lt;h2&gt;
  
  
  Appendix: Running Lambda Inside AWS Emulator
&lt;/h2&gt;

&lt;p&gt;Inside AWS Emulator, you can create and invoke Lambda functions, and you can also send HTTP requests to an API Gateway inside it (you specify the API with the &lt;code&gt;Host&lt;/code&gt; header, like &lt;code&gt;curl --cacert ~/.aws/emulator/9297/ca.pem -H 'Host: &amp;lt;api-id&amp;gt;.execute-api.&amp;lt;region&amp;gt;.amazonaws.com' https://127.0.0.1:9297/prod/...&lt;/code&gt;). However, those Lambda functions could not connect to anything other than the AWS services provided by AWS Emulator. It currently seems to be designed so that they can't connect to external APIs, local databases, or real AWS.&lt;/p&gt;

&lt;p&gt;In addition, as far as I tested with AWS Emulator 1.0.322, the following did not work either:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Container image Lambda functions

&lt;ul&gt;
&lt;li&gt;The AWS Emulator registry does not accept image pushes&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;ECS tasks and services, and AgentCore agents

&lt;ul&gt;
&lt;li&gt;The containers are not executed&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Some standard features of languages (runtimes), such as Node.js &lt;code&gt;fetch&lt;/code&gt;, &lt;code&gt;node:zlib&lt;/code&gt;, and &lt;code&gt;https&lt;/code&gt;, and Python &lt;code&gt;asyncio&lt;/code&gt; and &lt;code&gt;ssl&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;Lambda in AWS Emulator runs on a built-in implementation rather than the official AWS runtimes&lt;/li&gt;
&lt;li&gt;Also, there is only one built-in implementation per language, not one per version (confirmed for Python, and probably the same for Node.js)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Some CDK custom resources (&lt;code&gt;Custom::S3AutoDeleteObjects&lt;/code&gt; from &lt;code&gt;autoDeleteObjects: true&lt;/code&gt;, &lt;code&gt;BucketDeployment&lt;/code&gt;, and &lt;code&gt;cr.Provider&lt;/code&gt;)

&lt;ul&gt;
&lt;li&gt;The Lambda functions that perform the processing fail to load Node.js &lt;code&gt;https&lt;/code&gt; or Python &lt;code&gt;_signal&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;However, when you deploy with &lt;code&gt;--aws-emulator&lt;/code&gt;, cdkd runs these Lambda functions on the official runtimes in local Docker instead of inside AWS Emulator, so they work as is (Docker is required)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These findings are as of the time of writing (2026/11/13). All of AWS Emulator's code is written by autonomous AI agents, and development moves so fast that thousands of issues are fixed per day (&lt;a href="https://www.aboutamazon.com/news/aws/how-autonomous-ai-agents-built-aws-emulator" rel="noopener noreferrer"&gt;reference&lt;/a&gt;), so this may well change in the future.&lt;/p&gt;




&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;When you use AWS Emulator for development with AWS CDK, combining it with cdkd gives you the following benefits (repeated from above):&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;cdkd automatically configures the connection to AWS Emulator, and environment variables for Lambda and other resources are resolved automatically from your CDK code, so you don't need a &lt;code&gt;.env&lt;/code&gt; file&lt;/li&gt;
&lt;li&gt;You can freely switch the connection target per service, for example using real AWS for secrets and reference data/files while having the app write to AWS Emulator (no need to recreate real AWS data locally)&lt;/li&gt;
&lt;li&gt;Application code such as Lambda functions runs directly from source without being deployed to AWS Emulator, so you can try out changes right away without redeploying&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;cdkd has not only fast deployment but also local execution features, so combining it with AWS Emulator makes it even more powerful and extremely convenient.&lt;/p&gt;

&lt;p&gt;I also plan to keep adding new features whenever I come up with good ideas.&lt;/p&gt;

</description>
      <category>aws</category>
      <category>awscdk</category>
      <category>cdk</category>
      <category>awsemulator</category>
    </item>
    <item>
      <title>I Built cdkd: Deploy Your Existing AWS CDK Code Up to 15x Faster</title>
      <dc:creator>Kenta Goto</dc:creator>
      <pubDate>Mon, 02 Nov 2026 06:39:09 +0000</pubDate>
      <link>https://dev.to/aws-heroes/i-built-cdkd-deploy-your-existing-aws-cdk-code-up-to-15x-faster-4amg</link>
      <guid>https://dev.to/aws-heroes/i-built-cdkd-deploy-your-existing-aws-cdk-code-up-to-15x-faster-4amg</guid>
      <description>&lt;p&gt;I built an OSS CLI tool called cdkd (CDK Direct) that speeds up deployments by &lt;strong&gt;up to 15x&lt;/strong&gt; without changing a single line of your existing AWS CDK code.&lt;/p&gt;

&lt;p&gt;You can use it just by replacing &lt;code&gt;cdk deploy&lt;/code&gt; with &lt;code&gt;cdkd deploy&lt;/code&gt;. What makes it special is that it achieves this speedup &lt;strong&gt;by deploying directly through the AWS SDK, without going through CloudFormation&lt;/strong&gt;. On top of that, it can also run Lambda / API Gateway / ECS / CloudFront / Bedrock AgentCore and more locally.&lt;/p&gt;

&lt;p&gt;And CloudFormation isn't the only thing it beats on speed. In a head-to-head benchmark against Terraform—which, like cdkd, provisions directly through the AWS SDK—&lt;strong&gt;cdkd came out faster on most test cases&lt;/strong&gt; (more on this below).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/go-to-k/cdkd" rel="noopener noreferrer"&gt;go-to-k/cdkd&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There is also a &lt;a href="https://cdkd.dev" rel="noopener noreferrer"&gt;documentation site&lt;/a&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  What is cdkd?
&lt;/h2&gt;

&lt;p&gt;cdkd is a tool that &lt;strong&gt;rebuilds the AWS CDK CLI completely from scratch&lt;/strong&gt;. Just by replacing &lt;code&gt;cdk deploy&lt;/code&gt; with &lt;code&gt;cdkd deploy&lt;/code&gt;, you can use it &lt;strong&gt;without changing any of your existing CDK app code at all&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;There are three main features.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;Up to 15x faster deployments&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Provisions directly with the AWS SDK, without going through CloudFormation&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;CDK-compatible with no rewrites&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Your existing CDK app code runs as-is&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;Local execution of CDK apps&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Run Lambda / API Gateway / ECS and more locally without deploying&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The GIF below shows the same CDK app &lt;strong&gt;actually deployed to AWS&lt;/strong&gt; with &lt;code&gt;cdkd deploy&lt;/code&gt; and &lt;code&gt;cdk deploy&lt;/code&gt;, side by side. By the time cdkd finishes, CDK is still in the middle of creating the CloudFormation ChangeSet.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgithub.com%2Fuser-attachments%2Fassets%2F881f67d1-5ee6-4ecf-afc2-212aeed46e4b" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgithub.com%2Fuser-attachments%2Fassets%2F881f67d1-5ee6-4ecf-afc2-212aeed46e4b" alt="Comparison of cdk deploy and cdkd deploy. The cdkd side finishes first" width="800" height="360"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;An &lt;strong&gt;Express mode&lt;/strong&gt; that speeds up CloudFormation itself has also been announced, but even so, &lt;strong&gt;cdkd is about 2x faster, and nearly 9x faster for stacks that include asynchronous resources&lt;/strong&gt; (see the benchmarks later for details).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://aws.amazon.com/about-aws/whats-new/2026/06/aws-cloudformation-cdk/" rel="noopener noreferrer"&gt;AWS CloudFormation and CDK now support faster deployments (What's New)&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That said, cdkd is not meant to replace the AWS CDK CLI, but rather to &lt;strong&gt;complement it&lt;/strong&gt;. The idea is to use each where it shines: cdkd for fast iteration during development and testing, and the AWS CDK CLI for production deployments where you want to leverage the strengths of CloudFormation. The two can be &lt;strong&gt;migrated bidirectionally&lt;/strong&gt; with &lt;code&gt;cdkd import&lt;/code&gt; / &lt;code&gt;cdkd export&lt;/code&gt;, so you can freely move back and forth later (more on this below).&lt;/p&gt;

&lt;p&gt;It also pairs well with AI-driven development. cdkd isn't just fast at deploying—&lt;code&gt;cdkd destroy&lt;/code&gt; is fast too, since it deletes directly through the AWS SDK without going through CloudFormation, so you can keep each build-and-tear-down cycle short.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why I built it
&lt;/h2&gt;

&lt;p&gt;CDK's &lt;strong&gt;slow deployments&lt;/strong&gt; quietly add up during development and testing. Because CDK internally goes through CloudFormation, every change carries the overhead of CFn.&lt;/p&gt;

&lt;p&gt;The CDK CLI does have a fast mode, &lt;code&gt;cdk deploy --hotswap&lt;/code&gt;, which updates things like Lambda code directly without going through CloudFormation. However, it &lt;strong&gt;causes drift&lt;/strong&gt; between the CloudFormation stack state and the actual environment.&lt;/p&gt;

&lt;p&gt;So cdkd was born to speed up deployments by calling the AWS SDK directly instead of going through CloudFormation—&lt;strong&gt;while keeping your existing CDK app code as-is&lt;/strong&gt;—and, as a bonus, to even provide local execution using CDK's synthesis output.&lt;/p&gt;




&lt;h2&gt;
  
  
  Feature 1: Up to 15x faster deployments
&lt;/h2&gt;

&lt;p&gt;The biggest selling point of cdkd is right here: &lt;strong&gt;speed&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;cdkd provisions resources by calling the AWS SDK directly, without going through CloudFormation. It reproduces what CloudFormation does from scratch, and adds its own optimizations on top, achieving blazing-fast deployments.&lt;/p&gt;

&lt;p&gt;The main mechanisms are as follows.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Hybrid provisioning&lt;/strong&gt;: Major resources go through a fast, dedicated SDK Provider, while the rest fall back to the Cloud Control API to cover a wide range of resource types&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Parallel execution via a dependency graph (DAG)&lt;/strong&gt;: Analyzes the &lt;code&gt;Ref&lt;/code&gt; / &lt;code&gt;Fn::GetAtt&lt;/code&gt; dependencies within the template and deploys resources with no dependencies in parallel&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;--no-wait&lt;/code&gt;&lt;/strong&gt;: Skips the wait time for asynchronous resources like CloudFront / RDS / ElastiCache / NAT Gateway that "take several minutes to fully stabilize," moving on to the next step as soon as the Create call returns&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Benchmarks
&lt;/h3&gt;

&lt;p&gt;These measurements cover &lt;strong&gt;only the deploy phase&lt;/strong&gt;, excluding synth time (since both cdkd and CDK go through the same &lt;code&gt;aws-cdk-lib&lt;/code&gt; for synth).&lt;/p&gt;

&lt;p&gt;In addition to normal CloudFormation (Normal), we also include the newly announced &lt;strong&gt;Express mode&lt;/strong&gt; in the comparison. Express mode is a fast mode that skips waiting for resources to stabilize, with behavior close to cdkd's &lt;code&gt;--no-wait&lt;/code&gt;.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Stack&lt;/th&gt;
&lt;th&gt;Normal&lt;/th&gt;
&lt;th&gt;Express&lt;/th&gt;
&lt;th&gt;cdkd&lt;/th&gt;
&lt;th&gt;cdkd &lt;code&gt;--no-wait&lt;/code&gt;
&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;VPC + Lambda + SQS + CloudFront&lt;/td&gt;
&lt;td&gt;562&lt;/td&gt;
&lt;td&gt;366&lt;/td&gt;
&lt;td&gt;197&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;40&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;DynamoDB&lt;/td&gt;
&lt;td&gt;34&lt;/td&gt;
&lt;td&gt;34&lt;/td&gt;
&lt;td&gt;19&lt;/td&gt;
&lt;td&gt;15&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;DynamoDB + KMS&lt;/td&gt;
&lt;td&gt;71&lt;/td&gt;
&lt;td&gt;55&lt;/td&gt;
&lt;td&gt;27&lt;/td&gt;
&lt;td&gt;27&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;EC2&lt;/td&gt;
&lt;td&gt;44&lt;/td&gt;
&lt;td&gt;31&lt;/td&gt;
&lt;td&gt;27&lt;/td&gt;
&lt;td&gt;26&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Lambda&lt;/td&gt;
&lt;td&gt;55&lt;/td&gt;
&lt;td&gt;34&lt;/td&gt;
&lt;td&gt;23&lt;/td&gt;
&lt;td&gt;22&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;S3&lt;/td&gt;
&lt;td&gt;39&lt;/td&gt;
&lt;td&gt;22&lt;/td&gt;
&lt;td&gt;23&lt;/td&gt;
&lt;td&gt;24&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;SQS&lt;/td&gt;
&lt;td&gt;83&lt;/td&gt;
&lt;td&gt;22&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;9&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;9&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;SQS + CloudWatch&lt;/td&gt;
&lt;td&gt;87&lt;/td&gt;
&lt;td&gt;44&lt;/td&gt;
&lt;td&gt;30&lt;/td&gt;
&lt;td&gt;31&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;(Fastest of three runs, in seconds, us-west-2)&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;VPC + Lambda + SQS + CloudFront&lt;/code&gt; stack consists of a VPC (2 AZs, NAT Gateway, public/private subnets) + VPC Lambda + Lambda Function URL + CloudFront Distribution + SQS + EventSourceMapping + Consumer Lambda.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;"up to 15x"&lt;/strong&gt; in the title comes from this VPC stack. Against Normal CFn's 562 seconds, cdkd &lt;code&gt;--no-wait&lt;/code&gt; finishes in 40 seconds—&lt;strong&gt;over 14x faster&lt;/strong&gt; (achieved by letting AWS handle CloudFront propagation and NAT Gateway stabilization in the background). Even the default without &lt;code&gt;--no-wait&lt;/code&gt; is about 3x faster (197 seconds), by running CloudFront / Lambda URL / VPC Lambda concurrently with NAT Gateway propagation.&lt;/p&gt;

&lt;p&gt;Even for a simple SQS stack, cdkd finishes in 9 seconds against Normal's 83 seconds—&lt;strong&gt;about 9x faster&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Compared to Express mode as well, cdkd comes out faster than Express on most stacks. It finishes about 1.5–2x faster than Express, and about 2.4x faster for SQS.&lt;/p&gt;

&lt;p&gt;And if you combine &lt;code&gt;--no-wait&lt;/code&gt;, then even for stacks that include asynchronous resources such as VPC—Express's strong suit—cdkd is &lt;strong&gt;about 9x faster than Express&lt;/strong&gt; (Express 366 seconds → cdkd &lt;code&gt;--no-wait&lt;/code&gt; 40 seconds).&lt;/p&gt;

&lt;p&gt;One thing to note: cdkd tends to be faster for resources where the dedicated SDK Provider applies. For stacks that mostly consist of resources going through the Cloud Control API fallback (Athena WorkGroup, SSM Document, etc.), the speedup may land in the range of about 1.5–3x.&lt;/p&gt;

&lt;h3&gt;
  
  
  Speed comparison with Terraform
&lt;/h3&gt;

&lt;p&gt;The benchmarks above were against CloudFormation, but I also compared speed against Terraform, which—like cdkd—provisions by calling AWS APIs directly through the AWS SDK. I expressed the same logical configuration in both AWS CDK and Terraform, and compared the median of 7 runs across 6 test cases.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Test case&lt;/th&gt;
&lt;th&gt;cdkd&lt;/th&gt;
&lt;th&gt;Terraform&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;wide&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;20.0&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;46.1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;serverless&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;25.9&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;57.5&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;ec2&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;29.1&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;35.9&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;webapp&lt;/td&gt;
&lt;td&gt;109.7&lt;/td&gt;
&lt;td&gt;127.3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;ecs&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;162.8&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;209.5&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;cloudfront&lt;/td&gt;
&lt;td&gt;174.7&lt;/td&gt;
&lt;td&gt;177.5&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;(Median of 7 runs, in seconds; bold marks the faster of cdkd / Terraform)&lt;/p&gt;

&lt;p&gt;The result: &lt;strong&gt;cdkd was fastest in 4 of the 6 test cases, coming out 1.23–2.31x faster than Terraform&lt;/strong&gt; (the remaining two, webapp and cloudfront, were within measurement noise and are treated as ties).&lt;/p&gt;

&lt;p&gt;The code I used for the benchmarks is available in the repository below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/go-to-k/cdkd-bench-terraform" rel="noopener noreferrer"&gt;go-to-k/cdkd-bench-terraform&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Feature 2: Your existing CDK code runs as-is
&lt;/h2&gt;

&lt;p&gt;cdkd has no IaC language or syntax of its own. Its approach is to &lt;strong&gt;use the CDK app code you're already writing, exactly as-is, without changing anything&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Mechanically, it reads &lt;code&gt;--app&lt;/code&gt; from &lt;code&gt;cdk.json&lt;/code&gt;, runs the CDK app as a subprocess, and parses the Cloud Assembly (= CloudFormation templates) that &lt;code&gt;aws-cdk-lib&lt;/code&gt; generates. In other words, the synth part goes through exactly the same code as CDK. What cdkd builds on its own is the engine that comes after: how to deploy the template.&lt;/p&gt;

&lt;p&gt;It supports the standard CloudFormation features: context provider loops such as &lt;code&gt;Vpc.fromLookup()&lt;/code&gt; and SSM / HostedZone, uploading S3 file assets and Docker image (ECR) assets, intrinsic functions like &lt;code&gt;Ref&lt;/code&gt; / &lt;code&gt;Fn::GetAtt&lt;/code&gt;, cross-stack references, custom resources, and more.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# In your existing CDK app directory, just replace cdk with cdkd&lt;/span&gt;
cdkd deploy
cdkd diff
cdkd destroy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;--app&lt;/code&gt; is read automatically from &lt;code&gt;cdk.json&lt;/code&gt;, so in most cases you don't even need to specify the option.&lt;/p&gt;




&lt;h2&gt;
  
  
  Feature 3: Local execution of CDK apps
&lt;/h2&gt;

&lt;p&gt;Using a CDK app's synth output, cdkd can also run Lambda / API Gateway / ECS and more on local Docker &lt;strong&gt;without deploying to AWS&lt;/strong&gt;. It feels the same as &lt;code&gt;sam local *&lt;/code&gt;, but you don't need to prepare a separate &lt;code&gt;template.yaml&lt;/code&gt; or pipe things together with &lt;code&gt;cdk synth | sam ...&lt;/code&gt;. It reuses cdkd's own synth, asset, and route-resolution mechanisms directly.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Subcommand&lt;/th&gt;
&lt;th&gt;What it emulates&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;cdkd local invoke &amp;lt;target&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;One-off Lambda execution (via the AWS Lambda Runtime Interface Emulator)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;cdkd local start-api&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;A local HTTP server bundling REST v1 / HTTP API / Function URL routes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;cdkd local run-task &amp;lt;target&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Launches all containers in an ECS task definition on a local docker network&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;cdkd local start-service &amp;lt;target&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Long-running ECS Service emulation (replicas equal to &lt;code&gt;DesiredCount&lt;/code&gt;)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Run a Lambda one-off&lt;/span&gt;
cdkd &lt;span class="nb"&gt;local &lt;/span&gt;invoke MyStack/Handler &lt;span class="nt"&gt;--event&lt;/span&gt; events/get.json

&lt;span class="c"&gt;# Serve API Gateway / HTTP API / Function URL routes on a local server&lt;/span&gt;
cdkd &lt;span class="nb"&gt;local &lt;/span&gt;start-api

&lt;span class="c"&gt;# Launch an ECS task locally&lt;/span&gt;
cdkd &lt;span class="nb"&gt;local &lt;/span&gt;run-task MyStack/MyService/TaskDef
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It supports all current Lambda runtimes, including Node.js / Python / Ruby / Java / .NET / &lt;code&gt;provided.*&lt;/code&gt;. Adding &lt;code&gt;--from-state&lt;/code&gt; lets you run locally with &lt;strong&gt;deployed environment variables and secrets&lt;/strong&gt; resolved (values expressed by intrinsic functions like &lt;code&gt;table.tableName&lt;/code&gt; are properly resolved to their actual values, even locally).&lt;/p&gt;

&lt;h3&gt;
  
  
  It works even on stacks operated with &lt;code&gt;cdk deploy&lt;/code&gt; (CloudFormation)
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;--from-state&lt;/code&gt; above is a mechanism for stacks deployed with cdkd, but you can do the same thing for stacks deployed with the AWS CDK CLI's &lt;code&gt;cdk deploy&lt;/code&gt; (CloudFormation) using &lt;code&gt;--from-cfn-stack&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# For a stack deployed with cdkd&lt;/span&gt;
cdkd &lt;span class="nb"&gt;local &lt;/span&gt;invoke MyStack/Handler &lt;span class="nt"&gt;--from-state&lt;/span&gt;

&lt;span class="c"&gt;# For a stack operated with cdk deploy (CFn), you can run it locally the same way&lt;/span&gt;
cdkd &lt;span class="nb"&gt;local &lt;/span&gt;invoke MyStack/Handler &lt;span class="nt"&gt;--from-cfn-stack&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Put another way, even if you didn't deploy with cdkd, you can run Lambda / API Gateway / ECS and more locally &lt;strong&gt;just by using &lt;code&gt;cdkd local *&lt;/code&gt; against a stack you're already operating with &lt;code&gt;cdk deploy&lt;/code&gt;&lt;/strong&gt;. &lt;strong&gt;No migration to cdkd is required.&lt;/strong&gt; I think this can also serve as an entry point for AWS CDK CLI users who feel, "I'm not ready to try cdkd's fast deployments yet, but I'd like to adopt just the local execution experience."&lt;/p&gt;

&lt;p&gt;By the way, I'm also building &lt;code&gt;cdk-local&lt;/code&gt; (&lt;code&gt;cdkl&lt;/code&gt;) as &lt;strong&gt;a separate tool dedicated to local execution&lt;/strong&gt;. It's positioned as a standalone spin-off of cdkd's &lt;code&gt;local&lt;/code&gt; feature, and it additionally comes with a &lt;strong&gt;browser-based console, &lt;code&gt;cdkl studio&lt;/code&gt;&lt;/strong&gt; (where you can view invocations, responses, and container logs on a timeline). If you don't need cdkd's deployment features and purely want to adopt local execution, this one is more lightweight.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/go-to-k/cdk-local" rel="noopener noreferrer"&gt;go-to-k/cdk-local&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  How cdkd works
&lt;/h2&gt;

&lt;p&gt;The overall flow looks like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;┌─────────────────┐
│  Your CDK App   │  (aws-cdk-lib)
└────────┬────────┘
         │
         ▼
┌─────────────────┐
│ cdkd Synthesis  │  Subprocess execution + Cloud Assembly parsing
└────────┬────────┘
         │
         ▼
┌─────────────────┐
│ CloudFormation  │
│   Template      │
└────────┬────────┘
         │
         ▼
┌─────────────────┐
│ Asset Build &amp;amp;   │  S3 ZIP upload / ECR image build and push
│    Publish      │
└────────┬────────┘
         │
         ▼
┌─────────────────┐
│ cdkd Engine     │
│ - DAG analysis  │  Build the dependency graph
│ - Diff calc     │  Diff against existing resources
│ - Parallel exec │  Dispatch immediately when pending deps hit 0
└────────┬────────┘
         │
    ┌────┴────┐
    ▼         ▼
┌────────┐ ┌────────┐
│  SDK   │ │ Cloud  │
│Provider│ │Control │  Covers many additional
│        │ │  API   │  types via fallback
└────────┘ └────────┘
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The key point is that the structure keeps everything up to synth exactly the same as CDK, and cdkd's own engine takes over from there. Asset build and publish, the diff calculation that compares the template against the current resource state, and the parallel execution driven by dependency analysis are all implemented in-house.&lt;/p&gt;

&lt;p&gt;For state management, it uses S3 and acquires locks via S3 conditional writes (a &lt;code&gt;cdkd-state-{accountId}&lt;/code&gt; bucket). If a deployment fails midway, like CloudFormation, it &lt;strong&gt;rolls back only the resources created in that run&lt;/strong&gt; to keep state consistent (you can also leave the intermediate state behind by adding &lt;code&gt;--no-rollback&lt;/code&gt;).&lt;/p&gt;




&lt;h2&gt;
  
  
  Migrating between CloudFormation and cdkd
&lt;/h2&gt;

&lt;p&gt;You can &lt;strong&gt;migrate bidirectionally&lt;/strong&gt; between cdkd and CloudFormation.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;cdkd import --migrate-from-cloudformation&lt;/code&gt;: Takes a stack that's already managed by CloudFormation via &lt;code&gt;cdk deploy&lt;/code&gt; into cdkd management, without recreating resources&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;cdkd export&lt;/code&gt;: Hands a cdkd-managed stack over to CloudFormation (it uses &lt;code&gt;ChangeSetType=IMPORT&lt;/code&gt;, so AWS resources are not recreated)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can switch whenever you feel like migrating.&lt;/p&gt;




&lt;h2&gt;
  
  
  Installation and usage
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Prerequisites
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Node.js&lt;/strong&gt; &amp;gt;= 22.12.0&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;AWS credentials that can create the resources your stacks deploy.&lt;/strong&gt; cdkd calls each service's API directly, so it needs the permissions you would need to create those resources by hand. It also needs a small set of permissions for the S3 bucket for state management and the place to store assets (S3, ECR).&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Installation
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm i &lt;span class="nt"&gt;-g&lt;/span&gt; @cdkd/cli
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Quick start
&lt;/h3&gt;

&lt;p&gt;cdkd requires &lt;code&gt;cdkd bootstrap&lt;/code&gt; only the first time, which creates an S3 bucket for state management and a place to store assets (you don't need &lt;code&gt;cdk bootstrap&lt;/code&gt;—this one alone is enough).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Create the S3 bucket for state management (once per AWS account)&lt;/span&gt;
cdkd bootstrap

&lt;span class="c"&gt;# List the stacks in the CDK app&lt;/span&gt;
cdkd list

&lt;span class="c"&gt;# Deploy&lt;/span&gt;
cdkd deploy

&lt;span class="c"&gt;# Check the diff&lt;/span&gt;
cdkd diff

&lt;span class="c"&gt;# Destroy&lt;/span&gt;
cdkd destroy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;--app&lt;/code&gt; is auto-resolved from &lt;code&gt;cdk.json&lt;/code&gt;, and the state bucket is auto-resolved from your AWS account ID, so basically this is all you need.&lt;/p&gt;




&lt;h2&gt;
  
  
  Skill for AI Coding Agents
&lt;/h2&gt;

&lt;p&gt;The repository also ships a &lt;strong&gt;Skill&lt;/strong&gt; for using cdkd safely.&lt;/p&gt;

&lt;p&gt;With Claude Code, running the following in a session installs it as a plugin.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/plugin marketplace add go-to-k/cdkd
/plugin install cdkd-skills@cdkd
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After that, the Skill kicks in automatically when you ask something like "deploy this CDK stack with cdkd," and you can also invoke it directly with &lt;code&gt;/cdkd&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For agents that support the SKILL.md format, such as Codex and Cursor, you can install it with the GitHub CLI (v2.90 or later) or &lt;code&gt;npx skills&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;gh skill &lt;span class="nb"&gt;install &lt;/span&gt;go-to-k/cdkd cdkd
&lt;span class="c"&gt;# or&lt;/span&gt;
npx skills add go-to-k/cdkd &lt;span class="nt"&gt;--skill&lt;/span&gt; cdkd
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Beyond usage—from installation to checking ahead with &lt;code&gt;cdkd diff&lt;/code&gt;, deploying, verifying, and running locally—the Skill also spells out safety-side rules: use cdkd for development and testing rather than production, and never run &lt;code&gt;cdkd deploy&lt;/code&gt; against a CloudFormation-managed stack implicitly.&lt;/p&gt;




&lt;h2&gt;
  
  
  Wrapping up
&lt;/h2&gt;

&lt;p&gt;I built cdkd because I wanted to improve two experiences that are unglamorous but pay off every day—deployment speed during development and local execution—while keeping the "declarative goodness" of CDK intact. You can try it &lt;strong&gt;just by replacing &lt;code&gt;cdk&lt;/code&gt; with &lt;code&gt;cdkd&lt;/code&gt;, without changing a single line of your existing CDK code&lt;/strong&gt;, so the barrier to entry should be low.&lt;/p&gt;

&lt;p&gt;That said, cdkd is still in development and is not intended for production deployments. I'd be happy if you try it out in development and testing. I think it's easiest to see the value in situations like "deployments during development are painfully slow" or "I want to quickly run Lambda or ECS locally."&lt;/p&gt;

&lt;p&gt;For detailed documentation (supported resource types, import / export procedures, the finer behaviors of local execution, and so on), please check the README on GitHub or the &lt;a href="https://cdkd.dev" rel="noopener noreferrer"&gt;documentation site&lt;/a&gt;. &lt;strong&gt;If you like it, please give it a star!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/go-to-k/cdkd" rel="noopener noreferrer"&gt;go-to-k/cdkd&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;By the way, there's also a sister tool dedicated to local execution, &lt;code&gt;cdk-local&lt;/code&gt; (&lt;code&gt;cdkl&lt;/code&gt;). If you only want to adopt local execution, do check this one out too.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/go-to-k/cdk-local" rel="noopener noreferrer"&gt;go-to-k/cdk-local&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Also, cdkd was developed by &lt;strong&gt;delegating nearly all of the implementation to a Coding Agent&lt;/strong&gt;. I've introduced markgate—a tool that mechanically supports that development—in a separate article, so please take a look if you're interested.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/go-to-k/markgate" rel="noopener noreferrer"&gt;go-to-k/markgate&lt;/a&gt;&lt;/p&gt;

</description>
      <category>aws</category>
      <category>cdk</category>
      <category>awscdk</category>
      <category>cloudformation</category>
    </item>
    <item>
      <title>markgate: Mechanically Catch Coding Agent Slip-ups and Enforce Non-Command Tasks</title>
      <dc:creator>Kenta Goto</dc:creator>
      <pubDate>Mon, 02 Nov 2026 06:25:37 +0000</pubDate>
      <link>https://dev.to/aws-heroes/markgate-mechanically-catch-coding-agent-slip-ups-and-enforce-non-command-tasks-3lmh</link>
      <guid>https://dev.to/aws-heroes/markgate-mechanically-catch-coding-agent-slip-ups-and-enforce-non-command-tasks-3lmh</guid>
      <description>&lt;p&gt;I built an OSS tool called markgate that &lt;strong&gt;mechanically catches and enforces&lt;/strong&gt; two kinds of gaps: when a Coding Agent (Claude Code, etc.) &lt;strong&gt;accidentally forgets to call&lt;/strong&gt; a Skill that runs build or tests after implementation and commits anyway, and when &lt;strong&gt;AI tasks that can't be reduced to a command&lt;/strong&gt; — like a &lt;code&gt;/check-docs&lt;/code&gt; Skill — can't be enforced through hooks.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/go-to-k/markgate" rel="noopener noreferrer"&gt;markgate&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;NOTE: markgate is meant to run inside a git repository.&lt;/p&gt;




&lt;h2&gt;
  
  
  markgate
&lt;/h2&gt;

&lt;p&gt;markgate is a CLI tool that acts as an &lt;strong&gt;enforcement layer wedged between the Coding Agent and your hooks&lt;/strong&gt;. By mechanically catching and enforcing the Coding Agent's mistakes and omissions, it addresses the following three failure patterns.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Pattern&lt;/th&gt;
&lt;th&gt;Failure pattern it addresses&lt;/th&gt;
&lt;th&gt;Solution&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;Catching forgotten calls and avoiding duplicate execution&lt;/td&gt;
&lt;td&gt;&lt;code&gt;markgate run&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;Enforcing non-command tasks&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;markgate set&lt;/code&gt; + &lt;code&gt;markgate verify&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;Scoping multiple tasks + aggregate verify&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;composes&lt;/code&gt; in &lt;code&gt;.markgate.yml&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Below, we'll walk through each pattern in turn.&lt;/p&gt;




&lt;h2&gt;
  
  
  Pattern 1: Catching Forgotten Calls and Avoiding Duplicate Execution (&lt;code&gt;markgate run&lt;/code&gt;)
&lt;/h2&gt;

&lt;p&gt;Suppose you have an implementation skill, and a check skill (&lt;code&gt;/check&lt;/code&gt;) that runs after it.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;/check&lt;/code&gt; runs verifications against the implemented code. The things you want to guarantee against your implementation will vary by project — tests, typecheck, lint, format, build, documentation consistency, coverage generation, image scans, and so on.&lt;/p&gt;

&lt;p&gt;However, even if you tell the Coding Agent "run &lt;code&gt;/check&lt;/code&gt; after implementing", &lt;strong&gt;the Coding Agent can forget and silently skip the step&lt;/strong&gt;. When that happens, your code gets committed without the checks you wanted to run.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faon47qn89uplahobd8ab.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faon47qn89uplahobd8ab.png" alt="The Coding Agent forgets /check and commits anyway" width="609" height="331"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, you set up a pre-commit hook to forcibly run the equivalent of &lt;code&gt;/check&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;But the Coding Agent doesn't always forget. In fact, forgetting is rare — usually it runs &lt;code&gt;/check&lt;/code&gt; just fine.&lt;/p&gt;

&lt;p&gt;That leads to &lt;strong&gt;"the skill already ran it → the hook runs it again", a duplicate execution&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl7pfyuyvdmjw2xdf6vcm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl7pfyuyvdmjw2xdf6vcm.png" alt="Duplicate execution between the skill and the hook" width="581" height="361"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If &lt;code&gt;/check&lt;/code&gt; is heavy, this slows down what should be a fast Coding Agent development cycle. If it's light you might not notice, but running it frequently still wastes a non-trivial amount of time.&lt;/p&gt;

&lt;p&gt;Furthermore, if you skip the check after the implementation skill and put all check logic only in the hook, you can't run checks when you're not yet ready to commit. Running a post-edit hook on every file change isn't a great answer either, since the checks then run on every edit and that takes time.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ficb2ww0r7qgsgg29dy51.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ficb2ww0r7qgsgg29dy51.png" alt="Hook-only check is also problematic" width="581" height="351"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqzimf8hsetbwk7fj6q46.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqzimf8hsetbwk7fj6q46.png" alt="A post-edit hook runs on every file change" width="581" height="287"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Solved with &lt;code&gt;markgate run&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;markgate keeps both the &lt;code&gt;/check&lt;/code&gt; skill and the hook in place, and makes it so that &lt;strong&gt;the hook fires only when the Coding Agent forgets &lt;code&gt;/check&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In other words, when the Coding Agent runs &lt;code&gt;/check&lt;/code&gt; properly, &lt;strong&gt;the hook is skipped&lt;/strong&gt;, so duplicate execution doesn't happen.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvepp58d3t40fh6cen6ra.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvepp58d3t40fh6cen6ra.png" alt="With markgate, the hook fires only when the Coding Agent forgets" width="616" height="307"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Usage
&lt;/h3&gt;

&lt;p&gt;The basic flow is &lt;strong&gt;no config file required&lt;/strong&gt; — just call the command.&lt;/p&gt;

&lt;p&gt;For example, suppose your &lt;code&gt;/check&lt;/code&gt; skill and pre-commit hook from above both run &lt;code&gt;pnpm build&lt;/code&gt;. And suppose the pre-commit hook is wired up via Claude Code's &lt;code&gt;PreToolUse&lt;/code&gt; hook firing on &lt;code&gt;git commit*&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  1. &lt;code&gt;/check&lt;/code&gt; skill
&lt;/h4&gt;

&lt;p&gt;In the &lt;code&gt;/check&lt;/code&gt; skill that runs first, wrap the &lt;code&gt;pnpm build&lt;/code&gt; command using &lt;code&gt;markgate run -- &amp;lt;command&amp;gt;&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;// ... inside the skill definition
&lt;span class="gd"&gt;- pnpm build
&lt;/span&gt;&lt;span class="gi"&gt;+ markgate run -- pnpm build
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  2. hook
&lt;/h4&gt;

&lt;p&gt;In the pre-commit hook, rewrite &lt;code&gt;pnpm build&lt;/code&gt; to &lt;code&gt;markgate run -- pnpm build&lt;/code&gt; in the same way.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;{
  "hooks": {
    "PreToolUse": [{
      "matcher": "Bash",
      "if": "Bash(git commit*)",
&lt;span class="gd"&gt;-     "hooks": [{ "type": "command", "command": "pnpm build" }]
&lt;/span&gt;&lt;span class="gi"&gt;+     "hooks": [{ "type": "command", "command": "markgate run -- pnpm build" }]
&lt;/span&gt;    }]
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With just this, you get the behavior where &lt;strong&gt;the hook fires only when the Coding Agent forgets &lt;code&gt;/check&lt;/code&gt;&lt;/strong&gt;, running &lt;code&gt;pnpm build&lt;/code&gt;. When the Coding Agent runs &lt;code&gt;/check&lt;/code&gt; properly, &lt;strong&gt;the latter hook is skipped, so duplicate execution doesn't happen.&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Pattern 2: Enforcing Non-command Tasks (&lt;code&gt;set&lt;/code&gt; + &lt;code&gt;verify&lt;/code&gt;)
&lt;/h2&gt;

&lt;p&gt;Up to this point, &lt;code&gt;markgate run&lt;/code&gt; has meant writing the target command (&lt;code&gt;pnpm build&lt;/code&gt;, etc.) directly into the hook configuration.&lt;/p&gt;

&lt;p&gt;In practice, though, there are also &lt;strong&gt;tasks that can't be reduced to a command&lt;/strong&gt;. For example, "is the documentation consistent with src?", "does the PR description match the diff?", "is naming consistent with the existing code?" — these are &lt;strong&gt;reviews that need LLM judgment&lt;/strong&gt; and can't be expressed as a shell command.&lt;/p&gt;

&lt;p&gt;You want to run this kind of LLM task in the Coding Agent's &lt;strong&gt;own session&lt;/strong&gt;, because you want to leverage the &lt;strong&gt;context the Coding Agent has built up&lt;/strong&gt; (conversation history, open files, past decisions). Technically, you could launch a separate session from a hook via &lt;code&gt;claude -p&lt;/code&gt;, but since &lt;strong&gt;the context isn't shared&lt;/strong&gt;, it doesn't act as a continuation of the work so far. As a result, the hook alone can't tell whether the Coding Agent ran an LLM task like &lt;code&gt;/check-docs&lt;/code&gt;, which means &lt;strong&gt;there's no way to enforce it&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Building a Gate with &lt;code&gt;set&lt;/code&gt; + &lt;code&gt;verify&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;That's where the &lt;strong&gt;gate pattern&lt;/strong&gt; — combining &lt;code&gt;markgate set&lt;/code&gt; and &lt;code&gt;markgate verify&lt;/code&gt; — comes in. The check itself stays on the skill side; &lt;code&gt;set&lt;/code&gt; records "the moment the check passed" as a &lt;strong&gt;marker&lt;/strong&gt;, and on the hook side, &lt;code&gt;verify&lt;/code&gt; acts as a &lt;strong&gt;gate&lt;/strong&gt; that judges whether "the recorded marker matches the current marker".&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;markgate set&lt;/code&gt;: records the moment the check passed as a &lt;strong&gt;marker&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;markgate verify&lt;/code&gt;: exits 0 if the recorded marker matches the current marker, and exits 1 to stop things if they don't match&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Since the marker records the code state at the time of &lt;code&gt;set&lt;/code&gt;, when the code changes afterward &lt;strong&gt;the recorded marker and the current marker no longer match&lt;/strong&gt;, and &lt;code&gt;verify&lt;/code&gt; stops things (we'll cover what's inside a marker later).&lt;/p&gt;

&lt;h3&gt;
  
  
  Usage
&lt;/h3&gt;

&lt;p&gt;At the end of where the check actually runs (a skill like &lt;code&gt;/check-docs&lt;/code&gt;), call &lt;code&gt;markgate set&lt;/code&gt;. On the hook side, just call &lt;code&gt;markgate verify&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# At the end of the /check-docs skill definition&lt;/span&gt;
markgate &lt;span class="nb"&gt;set&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="err"&gt;//&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;.claude/settings.json&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;(hook&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;side)&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"hooks"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"PreToolUse"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"matcher"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Bash"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"if"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Bash(git commit*)"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"hooks"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
          &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"command"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"command"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"markgate verify || { echo 'run /check-docs' &amp;gt;&amp;amp;2; exit 1; }"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The whole chain of check operations stays defined on the skill side, and the hook just looks at the marker. If the marker is missing or doesn't match the code, the hook blocks with exit 1, and the Coding Agent gets both the error and a hint (&lt;code&gt;run /check-docs&lt;/code&gt;), so &lt;strong&gt;when the gate blocks things, recovery can be left to the Coding Agent and it can loop again&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Catching Both "First-time Non-command Triggers" and "Slip-ups"
&lt;/h3&gt;

&lt;p&gt;This gate pattern is effective in both of the following cases.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;First-time non-command trigger&lt;/strong&gt;: The Coding Agent doesn't even know &lt;code&gt;/check-docs&lt;/code&gt; exists and tries to commit. → Since there's no marker, the hook blocks, and after reading the error message (&lt;code&gt;run /check-docs&lt;/code&gt;), the Coding Agent runs &lt;code&gt;/check-docs&lt;/code&gt; for the first time.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Slip-up&lt;/strong&gt;: The Coding Agent runs &lt;code&gt;/check-docs&lt;/code&gt; and the marker is set. But later, when the code is edited again and you want to &lt;strong&gt;re-check&lt;/strong&gt;, the Coding Agent forgets to re-run &lt;code&gt;/check-docs&lt;/code&gt;. → Since the marker no longer matches the code, the hook blocks and prompts the Coding Agent to re-run.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In short, markgate uniformly catches both &lt;strong&gt;never-run-before cases&lt;/strong&gt; and &lt;strong&gt;failure to re-run after a slip-up&lt;/strong&gt;, using the same logic: "does the marker match the current code state?".&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8ekz5na41rk9v3x6gs7i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8ekz5na41rk9v3x6gs7i.png" alt="Run /check-docs on the skill side, verify on the hook side" width="626" height="464"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Pattern 3: Scoping and Aggregate Verify (&lt;code&gt;.markgate.yml&lt;/code&gt; + &lt;code&gt;composes&lt;/code&gt;)
&lt;/h2&gt;

&lt;p&gt;So far we've assumed "one skill, one hook", but in practice you often have multiple checks lined up.&lt;/p&gt;

&lt;p&gt;For example, suppose at pre-commit you want to run light checks (&lt;code&gt;check&lt;/code&gt;: typecheck / lint / build / unit test) plus a heavier check that uses an LLM to verify documentation consistency (&lt;code&gt;docs&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;The problem here is &lt;strong&gt;wasted runs&lt;/strong&gt;. If you bundle them into a single gate, even on a commit that only touches tests, the LLM consistency check runs even though nothing in the documentation-related files changed. That would mean re-running the heavy LLM check every time you change any code, which isn't practical.&lt;/p&gt;

&lt;h3&gt;
  
  
  Giving Each Task Its Own Scope
&lt;/h3&gt;

&lt;p&gt;That's where &lt;code&gt;.markgate.yml&lt;/code&gt; comes in: give each check its &lt;strong&gt;"own scope"&lt;/strong&gt; (= the set of files specified in &lt;code&gt;include&lt;/code&gt;). Whether a re-run is needed is judged by whether the code change falls within that scope.&lt;/p&gt;

&lt;p&gt;On top of that, using &lt;code&gt;composes&lt;/code&gt; to create an &lt;strong&gt;aggregate gate&lt;/strong&gt; that bundles multiple gates means the hook side can judge "do all the bundled gates match the current code?" with just a single line of &lt;code&gt;verify&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuavi5dw70z22nm5ghrge.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuavi5dw70z22nm5ghrge.png" alt="Each gate updates its marker via set, and the aggregate gate's verify takes the AND" width="425" height="211"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Usage
&lt;/h3&gt;

&lt;p&gt;Create a &lt;code&gt;.markgate.yml&lt;/code&gt; (&lt;code&gt;markgate init&lt;/code&gt; generates a template).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="c1"&gt;# .markgate.yml&lt;/span&gt;
&lt;span class="na"&gt;gates&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;check&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;hash&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;files&lt;/span&gt; &lt;span class="c1"&gt;# only the files specified in include are hashed&lt;/span&gt;
    &lt;span class="na"&gt;include&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;src/**"&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;tests/**"&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;package.json"&lt;/span&gt;
  &lt;span class="na"&gt;docs&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;hash&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;files&lt;/span&gt;
    &lt;span class="na"&gt;include&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;src/**"&lt;/span&gt; &lt;span class="c1"&gt;# changes in src also affect docs, so include in both&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;docs/**"&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;README.md"&lt;/span&gt;
  &lt;span class="na"&gt;pre-commit&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;composes&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;check&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;docs&lt;/span&gt;&lt;span class="pi"&gt;]&lt;/span&gt; &lt;span class="c1"&gt;# aggregate gate: AND of check and docs&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From the skill side, specify the gate name as an argument and &lt;code&gt;set&lt;/code&gt; each gate individually.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# At the end of the /check skill definition&lt;/span&gt;
pnpm typecheck &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; pnpm lint &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; pnpm &lt;span class="nb"&gt;test&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; markgate &lt;span class="nb"&gt;set &lt;/span&gt;check
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# At the end of the /check-docs skill definition&lt;/span&gt;
markgate &lt;span class="nb"&gt;set &lt;/span&gt;docs
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, in the pre-commit hook, &lt;code&gt;verify&lt;/code&gt; the &lt;code&gt;pre-commit&lt;/code&gt; aggregate gate in a single line. When it doesn't match, &lt;code&gt;markgate status pre-commit&lt;/code&gt; shows "which gate doesn't match", and the Coding Agent reads that and re-loops the corresponding skill.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="err"&gt;//&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;.claude/settings.json&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"hooks"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"PreToolUse"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"matcher"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Bash"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"if"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Bash(git commit*)"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"hooks"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
          &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"command"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"command"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"markgate verify pre-commit || { markgate status pre-commit &amp;gt;&amp;amp;2; exit 1; }"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The key point is that &lt;strong&gt;only the checks needed for the edited scope get re-run&lt;/strong&gt;. Each gate's marker only hashes the files in its own &lt;code&gt;include&lt;/code&gt;, so on a commit that only touches one side's scope, the other gate's marker stays the same as when it was set and still matches, meaning no re-run is needed.&lt;/p&gt;

&lt;p&gt;For example, a commit that only modifies tests re-runs only the light checks; a commit that only touches files outside any &lt;code&gt;include&lt;/code&gt; (CI config, etc.) skips both.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Edited files&lt;/th&gt;
&lt;th&gt;&lt;code&gt;check&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;docs&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;Checks that need to re-run&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;tests/**&lt;/code&gt; only&lt;/td&gt;
&lt;td&gt;stale&lt;/td&gt;
&lt;td&gt;fresh&lt;/td&gt;
&lt;td&gt;light checks only&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;docs/**&lt;/code&gt; / &lt;code&gt;README.md&lt;/code&gt; only&lt;/td&gt;
&lt;td&gt;fresh&lt;/td&gt;
&lt;td&gt;stale&lt;/td&gt;
&lt;td&gt;heavy check only&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;src/**&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;stale&lt;/td&gt;
&lt;td&gt;stale&lt;/td&gt;
&lt;td&gt;both&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;None of the above (CI config, editor config, etc.)&lt;/td&gt;
&lt;td&gt;fresh&lt;/td&gt;
&lt;td&gt;fresh&lt;/td&gt;
&lt;td&gt;none (commit passes)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Note that the &lt;code&gt;pre-commit&lt;/code&gt; aggregate gate in this example is designed without a command of its own; it just performs the AND judgment — fresh if all bundled gates are fresh, stale if any one is stale. &lt;strong&gt;This shape — without its own command — can't be written with &lt;code&gt;markgate run&lt;/code&gt; (which wraps a single command); only the gate pattern (&lt;code&gt;set&lt;/code&gt; + &lt;code&gt;verify&lt;/code&gt;) can express it.&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Other Use Case Examples
&lt;/h2&gt;

&lt;p&gt;Beyond the basic usage above, markgate has various advanced patterns.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Check documentation consistency before creating a PR (&lt;code&gt;/check-docs&lt;/code&gt; → &lt;code&gt;gh pr create&lt;/code&gt; hook)&lt;/li&gt;
&lt;li&gt;Vulnerability scan before pushing an image (&lt;code&gt;trivy image&lt;/code&gt; → &lt;code&gt;docker push&lt;/code&gt; hook)&lt;/li&gt;
&lt;li&gt;Generate a coverage report before pushing (&lt;code&gt;go test -cover&lt;/code&gt; → &lt;code&gt;git push&lt;/code&gt; hook)&lt;/li&gt;
&lt;li&gt;Ensure E2E tests have just been re-run before merging a PR (&lt;code&gt;pnpm test:e2e&lt;/code&gt; → &lt;code&gt;gh pr merge&lt;/code&gt; hook)&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  How It Works: Managing State with Markers
&lt;/h2&gt;

&lt;p&gt;Let's look at what the "marker" we've been mentioning is actually made of.&lt;/p&gt;

&lt;p&gt;markgate works by saving the &lt;strong&gt;"state"&lt;/strong&gt; at the time the target command (or task) succeeded as a marker, and on the next call of the same command (or task), &lt;strong&gt;if the marker matches the current state, the run is skipped; if not, it runs (&lt;code&gt;markgate run&lt;/code&gt;) / blocks (&lt;code&gt;markgate verify&lt;/code&gt;)&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In practice, this "state" is &lt;strong&gt;a hash of the code content&lt;/strong&gt;. You can choose between two hash targets: "a specific set of files" (&lt;code&gt;hash: files&lt;/code&gt; + &lt;code&gt;include&lt;/code&gt;) or "the repository's git status as a whole" (&lt;code&gt;hash: git-tree&lt;/code&gt;, the default). Either way, &lt;strong&gt;as long as the code content hasn't changed, the command is skipped&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# First run: no marker yet, so pnpm build runs and a marker is saved on success&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;markgate run &lt;span class="nt"&gt;--&lt;/span&gt; pnpm build
building...
passed &lt;span class="k"&gt;in &lt;/span&gt;7.2s

&lt;span class="c"&gt;# Second run: no file changes → execution is skipped&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;markgate run &lt;span class="nt"&gt;--&lt;/span&gt; pnpm build

&lt;span class="c"&gt;# After editing a file: the marker is invalidated, so the command runs again&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s1"&gt;'// fix typo'&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; src/foo.ts
&lt;span class="nv"&gt;$ &lt;/span&gt;markgate run &lt;span class="nt"&gt;--&lt;/span&gt; pnpm build
building...
passed &lt;span class="k"&gt;in &lt;/span&gt;7.1s
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By default, the marker file is stored inside &lt;code&gt;.git&lt;/code&gt; (&lt;code&gt;.git/markgate/default.json&lt;/code&gt;), so you don't need to create a marker file yourself, and it isn't tracked by git even without an explicit &lt;code&gt;.gitignore&lt;/code&gt; entry. (There's also an option to explicitly change the marker file path and share markers between local and CI; see the &lt;a href="https://github.com/go-to-k/markgate#sharing-markers-across-machines-ci--teammates" rel="noopener noreferrer"&gt;README&lt;/a&gt; for that.)&lt;/p&gt;




&lt;h2&gt;
  
  
  Installation
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Homebrew&lt;/span&gt;
brew &lt;span class="nb"&gt;install &lt;/span&gt;go-to-k/tap/markgate

&lt;span class="c"&gt;# shell script&lt;/span&gt;
curl &lt;span class="nt"&gt;-fsSL&lt;/span&gt; https://raw.githubusercontent.com/go-to-k/markgate/main/install.sh | bash

&lt;span class="c"&gt;# go install&lt;/span&gt;
go &lt;span class="nb"&gt;install &lt;/span&gt;github.com/go-to-k/markgate/cmd/markgate@latest

&lt;span class="c"&gt;# mise (ubi backend)&lt;/span&gt;
mise use &lt;span class="s2"&gt;"ubi:go-to-k/markgate@0.3.3"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Using markgate from Other Hook Managers
&lt;/h2&gt;

&lt;p&gt;So far I've used the Claude Code hook as the example, but markgate is &lt;strong&gt;hook-manager agnostic&lt;/strong&gt;. With husky / lefthook / the pre-commit framework, or any other hook manager, you can use it just by writing a single line of &lt;code&gt;markgate run -- &amp;lt;command&amp;gt;&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# .husky/pre-commit&lt;/span&gt;
markgate run &lt;span class="nt"&gt;--&lt;/span&gt; pnpm &lt;span class="nb"&gt;test&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="c1"&gt;# lefthook.yml&lt;/span&gt;
&lt;span class="na"&gt;pre-commit&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;commands&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;check&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;markgate run -- pnpm test&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="c1"&gt;# .pre-commit-config.yaml (pre-commit framework)&lt;/span&gt;
&lt;span class="na"&gt;repos&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;repo&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;local&lt;/span&gt;
    &lt;span class="na"&gt;hooks&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;markgate-check&lt;/span&gt;
        &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;markgate check&lt;/span&gt;
        &lt;span class="na"&gt;entry&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;markgate run -- pnpm test&lt;/span&gt;
        &lt;span class="na"&gt;language&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;system&lt;/span&gt;
        &lt;span class="na"&gt;pass_filenames&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;Personally, I think this has turned into a tool I'll lean on in the AI era. In particular, the idea of &lt;strong&gt;"mechanically catching AI mistakes and omissions"&lt;/strong&gt; feels like one of the important points when working with AI.&lt;/p&gt;

&lt;p&gt;For the other features (&lt;code&gt;exclude&lt;/code&gt; / &lt;code&gt;state_dir&lt;/code&gt; settings in &lt;code&gt;.markgate.yml&lt;/code&gt;, sharing markers between local and CI, strict gate dependencies via &lt;code&gt;requires&lt;/code&gt;, etc.), see the README in the &lt;a href="https://github.com/go-to-k/markgate" rel="noopener noreferrer"&gt;GitHub repository&lt;/a&gt;. If you like it, I'd be glad if you'd give the repository a ⭐.&lt;/p&gt;

&lt;p&gt;Also, as a project that actually uses markgate in practice, there's cdkd (CDK Direct) — a from-scratch rewrite of the AWS CDK CLI that's made it blazingly fast. &lt;strong&gt;Nearly all of the implementation is delegated to a Coding Agent&lt;/strong&gt;, and markgate mechanically backs that up against missed runs and non-command tasks. Take a look if you're interested.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/go-to-k/cdkd" rel="noopener noreferrer"&gt;cdkd&lt;/a&gt;&lt;/p&gt;

</description>
      <category>agents</category>
      <category>agentskills</category>
      <category>claudecode</category>
      <category>opensource</category>
    </item>
    <item>
      <title>How to Keep AI from Breaking Your Environment with AWS CDK</title>
      <dc:creator>Kenta Goto</dc:creator>
      <pubDate>Tue, 20 Oct 2026 11:54:44 +0000</pubDate>
      <link>https://dev.to/aws-heroes/how-to-keep-ai-from-breaking-your-environment-with-aws-cdk-48l1</link>
      <guid>https://dev.to/aws-heroes/how-to-keep-ai-from-breaking-your-environment-with-aws-cdk-48l1</guid>
      <description>&lt;h2&gt;
  
  
  How to Keep AI from Breaking Your Environment with AWS CDK
&lt;/h2&gt;

&lt;p&gt;More and more people are letting AI write their AWS CDK code. At the same time, some may feel uneasy about AI breaking their environment on its own.&lt;/p&gt;

&lt;p&gt;For those people, this article introduces a way to safely stop the process only when a change to your CDK code contains a &lt;strong&gt;dangerous diff&lt;/strong&gt; that could break your environment.&lt;/p&gt;

&lt;h3&gt;
  
  
  Destructive Changes
&lt;/h3&gt;

&lt;p&gt;So what kind of dangerous diff can break an environment?&lt;/p&gt;

&lt;p&gt;It is a &lt;strong&gt;destructive change&lt;/strong&gt;, where a resource is deleted or replaced (recreated).&lt;/p&gt;

&lt;p&gt;Simply adding a resource or changing a property is usually recoverable. But deleting or replacing a resource can affect your system, causing unexpected downtime or data loss.&lt;/p&gt;

&lt;p&gt;Especially these days, some people let AI not only write CDK code but also deploy the resources. You absolutely want to avoid handing CDK development over to AI, running iteration after iteration, and then finding that an important resource has been deleted.&lt;/p&gt;

&lt;p&gt;Of course, even if you don't let AI handle the deployment, you probably deploy through CI/CD, especially for production, and the same thing applies to automated deployments in a pipeline.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Difficulty of Judging Destructive Changes
&lt;/h3&gt;

&lt;p&gt;Some of you may already have been showing diffs to AI and having it judge whether an operation is dangerous. When you leave the judgment to AI, however, it inevitably becomes non-deterministic, which makes it hard to meet a requirement like reliably detecting and preventing destructive changes every single time.&lt;/p&gt;

&lt;p&gt;The reason is this: just by looking at the diff of the CloudFormation template that CDK generates, you can tell whether a resource itself will be deleted.&lt;/p&gt;

&lt;p&gt;But some AWS resources are replaced, that is, recreated, when a specific property is changed.&lt;/p&gt;

&lt;p&gt;So even if the template diff shows no resource deletion, you cannot determine whether the change is destructive unless you can also judge changes to the properties that trigger replacement.&lt;/p&gt;

&lt;h3&gt;
  
  
  Mechanical Detection of Destructive Changes
&lt;/h3&gt;

&lt;p&gt;The approach introduced here is now possible as a feature of AWS CDK itself: you can &lt;strong&gt;detect destructive changes mechanically and deterministically&lt;/strong&gt;. Compared with having AI judge the diff, this gives you certainty.&lt;/p&gt;

&lt;p&gt;In other words, you can now &lt;strong&gt;let AI drive development autonomously while reliably blocking only destructive changes for a human to judge, achieving both autonomy and safety&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This enables a workflow where a human decides whether the destructive change is expected, and approves it only in that case so the CI pipeline can move forward.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to Do This in AWS CDK
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://github.com/aws/aws-cdk-cli/releases/tag/aws-cdk%40v2.1145.0" rel="noopener noreferrer"&gt;AWS CDK CLI v2.1145.0&lt;/a&gt; made the following available on the &lt;code&gt;cdk diff&lt;/code&gt; and &lt;code&gt;cdk deploy&lt;/code&gt; commands. These are the measures for detecting and preventing destructive changes.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;cdk diff --fail-on destructive&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;cdk deploy --require-approval destructive&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  cdk diff --fail-on destructive
&lt;/h4&gt;

&lt;p&gt;The &lt;code&gt;--fail-on&lt;/code&gt; option makes the command fail when a diff occurs and matches the condition you specify.&lt;/p&gt;

&lt;p&gt;This option itself did not exist before; it was added in this release. Until now, there was only &lt;code&gt;--fail&lt;/code&gt;, which fails the command whenever a diff occurs.&lt;/p&gt;

&lt;p&gt;You can pass the following four values to &lt;code&gt;--fail-on&lt;/code&gt;. These values are in fact the same ones you can pass to &lt;code&gt;cdk deploy --require-approval&lt;/code&gt;, which is described next.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;broadening: fails when there are changes that broaden permissions from a security standpoint, such as adding IAM statements or security group rules&lt;/li&gt;
&lt;li&gt;any-change: fails whenever a diff occurs, no matter what the change is (= &lt;code&gt;--fail&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;never: never fails, no matter what the change is&lt;/li&gt;
&lt;li&gt;destructive: fails when there is a &lt;strong&gt;destructive change&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Running &lt;code&gt;cdk diff&lt;/code&gt; with &lt;code&gt;destructive&lt;/code&gt; passed to &lt;code&gt;--fail-on&lt;/code&gt; makes it exit with an error &lt;strong&gt;only when there are destructive changes such as resource deletions or replacements&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ npx cdk diff --fail-on destructive
...(omitted)
...
Resources
[~] AWS::Lambda::Function ChatBackend/HistoryFn ChatBackendHistoryFnCCDE0D46 replace
 └─ [+] FunctionName (requires replacement)
     └─ changed-name
...

✨  Number of stacks with differences: 1

❌  Found 5 destructive change(s) (--fail-on=destructive):
  MyStack: AWS::Lambda::Function ChatBackend/HistoryFn ChatBackendHistoryFnCCDE0D46 will be replaced
  ...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  cdk deploy --require-approval destructive
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;--require-approval&lt;/code&gt; is an option that prompts the user for confirmation when a diff at deploy time matches the condition you specify.&lt;/p&gt;

&lt;p&gt;As with &lt;code&gt;cdk diff&lt;/code&gt; above, specifying &lt;code&gt;destructive&lt;/code&gt; now lets you detect destructive changes and prompt for confirmation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ npx cdk deploy --require-approval destructive
...(omitted)
...
Destructive changes:
  MyStack: AWS::Lambda::Function ChatBackend/HistoryFn ChatBackendHistoryFnCCDE0D46 will be replaced
  ...

Stack includes destructive updates and "--require-approval" is set to 'destructive'.
Do you wish to deploy these changes? (y/n)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Putting It into Practice
&lt;/h3&gt;

&lt;p&gt;Whether you let AI drive development autonomously or not — in CI/CD, for example — it is a good idea to run &lt;code&gt;cdk diff --fail-on destructive&lt;/code&gt; before you deploy.&lt;/p&gt;

&lt;p&gt;With AI, have the command run automatically from within a skill or a hook; with CI/CD, insert a stage right before the deploy. The basic idea is to keep the process running, and when a destructive change does appear, approve it and move on if the change is what you intended.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fwjulxfiwdjasmswdv3bi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fwjulxfiwdjasmswdv3bi.png" alt="A development flow that fails only on destructive changes and goes back to implementation when they are unexpected" width="800" height="432"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With this in place, the process stops only on destructive changes inside an autonomous, automated loop, which gives you a workflow that ensures safety at a low cost.&lt;/p&gt;

&lt;h3&gt;
  
  
  Contributing to the AWS CDK CLI
&lt;/h3&gt;

&lt;p&gt;This feature actually started as an idea I had and wanted for myself. So I implemented it in the upstream repository, opened PRs, and had them merged. It ended up as the following three PRs, making it a fairly large feature addition.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/aws/aws-cdk-cli/pull/2020" rel="noopener noreferrer"&gt;feat(cli): add &lt;code&gt;--fail-on&lt;/code&gt; to &lt;code&gt;cdk diff&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/aws/aws-cdk-cli/pull/2011" rel="noopener noreferrer"&gt;feat(cli): add &lt;code&gt;destructive&lt;/code&gt; to &lt;code&gt;cdk diff --fail-on&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/aws/aws-cdk-cli/pull/2021" rel="noopener noreferrer"&gt;feat(cli): add &lt;code&gt;destructive&lt;/code&gt; to &lt;code&gt;cdk deploy --require-approval&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Shipped First in cdkd
&lt;/h3&gt;

&lt;p&gt;This feature was in fact shipped first in an OSS CLI tool called cdkd that I develop on my own, and was added to the CDK CLI afterwards. (Only a few days apart, though.)&lt;/p&gt;

&lt;p&gt;cdkd is a from-scratch reimplementation of the CDK CLI. It lets you &lt;strong&gt;deploy CDK up to 15 times faster without changing a single line of your CDK code&lt;/strong&gt;. If you are interested, I would be happy if you took a look.&lt;/p&gt;

&lt;p&gt;GitHub: &lt;a href="https://github.com/go-to-k/cdkd" rel="noopener noreferrer"&gt;go-to-k/cdkd&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  P.S. A Follow-up PR (Added on 2026/10/21)
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;destructive&lt;/code&gt; check described above flags a deletion or replacement of any resource.&lt;/p&gt;

&lt;p&gt;However, some services replace resources on every code change or deployment, such as API Gateway deployments, Lambda aliases (versions), and Lambda Web Functions revisions. In those cases, the check fires every time, which makes this approach hard to adopt.&lt;/p&gt;

&lt;p&gt;So I later submitted another PR that adds an option to exclude specific resources from the check:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/aws/aws-cdk-cli/pull/2069" rel="noopener noreferrer"&gt;feat(cli): add &lt;code&gt;--ignore-destructive&lt;/code&gt; to exclude resources from the destructive check&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once this is merged, you will be able to apply the approach introduced here even to resource configurations where routine replacements are the normal use case.&lt;/p&gt;

&lt;h3&gt;
  
  
  Final Thoughts
&lt;/h3&gt;

&lt;p&gt;As AI becomes widespread, the way we develop with AWS CDK keeps changing. Still, building infrastructure comes with side effects, so fully automating it remains a little unnerving.&lt;/p&gt;

&lt;p&gt;I think the approach introduced here is a well-balanced hybrid that leaves as much as possible to AI while having humans cover only the genuinely dangerous and important parts.&lt;/p&gt;

</description>
      <category>aws</category>
      <category>cdk</category>
      <category>awscdk</category>
      <category>ai</category>
    </item>
    <item>
      <title>4 Ways to Safely Refactor AWS CDK Code Without Resource Replacement</title>
      <dc:creator>Kenta Goto</dc:creator>
      <pubDate>Thu, 08 Oct 2026 08:58:05 +0000</pubDate>
      <link>https://dev.to/aws-heroes/4-ways-to-safely-refactor-aws-cdk-code-without-resource-replacement-405o</link>
      <guid>https://dev.to/aws-heroes/4-ways-to-safely-refactor-aws-cdk-code-without-resource-replacement-405o</guid>
      <description>&lt;p&gt;In AWS CDK, refactoring such as extracting resource definitions into a custom construct can cause resources to be replaced at deployment time — that is, deleted and recreated. For stateful resources, this means the data they held is lost.&lt;/p&gt;

&lt;p&gt;In this article, I will introduce four ways to refactor safely without triggering this replacement.&lt;/p&gt;

&lt;p&gt;NOTE: This article is based on AWS CDK (aws-cdk-lib) v2.263.0 and AWS CDK CLI (aws-cdk) v2.1139.0. To use the &lt;code&gt;cdk refactor&lt;/code&gt; and &lt;code&gt;cdk orphan&lt;/code&gt; commands introduced here, not only the CDK CLI but also the bootstrap stack must support both commands. If these commands result in an error, update the CDK CLI and then re-run &lt;code&gt;cdk bootstrap&lt;/code&gt; to update the bootstrap stack.&lt;/p&gt;




&lt;h2&gt;
  
  
  Construct Tree and Path
&lt;/h2&gt;

&lt;p&gt;Let's start with the mechanism by which refactoring causes resource replacement. When you define a resource in AWS CDK, you write something like &lt;code&gt;new Queue(this, 'Queue')&lt;/code&gt;, passing the parent construct as the first argument and a string called the construct ID as the second. The first argument is usually &lt;code&gt;this&lt;/code&gt;, which points to the stack or construct being defined. The construct ID is a string that identifies a construct among those under the same parent. The hierarchy in which constructs are connected as parents and children, with the App at the top, is called the construct tree, and a position in the tree is represented by a path such as &lt;code&gt;MyStack/Queue&lt;/code&gt;, formed by joining the construct IDs from the parent down.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fky3mtpzvbml8l6qtc3p6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fky3mtpzvbml8l6qtc3p6.png" alt="Example of a construct tree" width="441" height="271"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Code Before Refactoring
&lt;/h2&gt;

&lt;p&gt;Now let's look at an example where replacement actually occurs. Consider the following code, which defines three resources directly under the stack: an Amazon SQS queue, an Amazon SNS topic, and an AWS Lambda function. The queue holds notification messages waiting to be processed, and deleting it would affect the running system. The topic, on the other hand, is a stateless resource, and we will assume its replacement is acceptable. Note that &lt;code&gt;Code.fromAsset&lt;/code&gt;, which specifies the Lambda function code, deploys the contents of the local &lt;code&gt;lambda&lt;/code&gt; directory as the function code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyStack&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Stack&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;scope&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Construct&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nx"&gt;StackProps&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;scope&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Queue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Queue&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Topic&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Topic&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Function&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;runtime&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Runtime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;NODEJS_24_X&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;index.handler&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;code&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Code&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fromAsset&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;lambda&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Code After Refactoring
&lt;/h2&gt;

&lt;p&gt;Of these three resources, we extract the queue and the topic, which make up the notification feature, into a custom construct called &lt;code&gt;Notifications&lt;/code&gt;, and have the stack instantiate it. The Lambda function stays directly under the stack. The actual AWS resource configuration to be deployed remains the same.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Notifications&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Construct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;scope&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Construct&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;scope&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Queue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Queue&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Topic&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Topic&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyStack&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Stack&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;scope&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Construct&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nx"&gt;StackProps&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;scope&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Notifications&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Notifications&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Function&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;runtime&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Runtime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;NODEJS_24_X&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;index.handler&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;code&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Code&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fromAsset&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;lambda&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Logical ID Changes and Resource Replacement
&lt;/h2&gt;

&lt;p&gt;AWS CDK automatically generates the logical IDs in the AWS CloudFormation template based on paths. For example, the &lt;code&gt;Queue&lt;/code&gt; before refactoring gets a logical ID like &lt;code&gt;Queue4A7E3555&lt;/code&gt;, a path-derived string with a hash appended. In other words, when refactoring changes a construct's position in the tree or its construct ID, the logical ID changes along with the path.&lt;/p&gt;

&lt;p&gt;In our example as well, the path of &lt;code&gt;Queue&lt;/code&gt; changes from &lt;code&gt;MyStack/Queue&lt;/code&gt; to &lt;code&gt;MyStack/Notifications/Queue&lt;/code&gt;, and its logical ID changes from &lt;code&gt;Queue4A7E3555&lt;/code&gt; to &lt;code&gt;NotificationsQueue91395D8F&lt;/code&gt;. Similarly, &lt;code&gt;Topic&lt;/code&gt; changes from &lt;code&gt;TopicBFC7AF6E&lt;/code&gt; to &lt;code&gt;NotificationsTopicAE679CBD&lt;/code&gt;. Meanwhile, the Lambda function left directly under the stack keeps its path, so its logical ID stays the same.&lt;/p&gt;

&lt;p&gt;CloudFormation identifies resources by their logical IDs. Therefore, if you deploy in this state, the queue and the topic, whose logical IDs have changed, are treated as different resources: they are deleted and created anew. We assumed the topic's replacement was acceptable, but the notification messages accumulated in the queue would be lost.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fctv760j9lb6sbormjx1c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fctv760j9lb6sbormjx1c.png" alt="Logical ID changes caused by refactoring" width="800" height="278"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Four Ways to Refactor Safely
&lt;/h2&gt;

&lt;p&gt;To prevent this replacement, I will introduce the following four methods. All of them carry the deployed resources over to the refactored code without recreating them.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The &lt;code&gt;cdk refactor&lt;/code&gt; command&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;cdk orphan&lt;/code&gt; + &lt;code&gt;cdk import&lt;/code&gt; commands&lt;/li&gt;
&lt;li&gt;Setting the construct ID to &lt;code&gt;Default&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Overriding the logical ID&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  1. The cdk refactor Command
&lt;/h2&gt;

&lt;p&gt;The first method is the &lt;a href="https://docs.aws.amazon.com/cdk/v2/guide/ref-cli-cmd-refactor.html" rel="noopener noreferrer"&gt;cdk refactor&lt;/a&gt; command. When you run it, resources whose positions in the construct tree have changed are detected and moved to their new positions without being recreated. It supports moves both within the same stack and to a different stack. It can also apply multiple resource moves at once, as in our example, so not only the queue but also the topic is carried over without replacement.&lt;/p&gt;

&lt;h3&gt;
  
  
  1-1. How to Use cdk refactor
&lt;/h3&gt;

&lt;p&gt;After refactoring your code, run &lt;code&gt;cdk refactor&lt;/code&gt; before deploying. With the &lt;code&gt;--dry-run&lt;/code&gt; flag, it only displays the list of resources detected as moved, without applying anything. Once you confirm that the moves are as intended, run the command again without &lt;code&gt;--dry-run&lt;/code&gt;. After you answer the confirmation prompt, the moves are applied, and finally a deployment runs automatically to align template details, such as path metadata, with your code. As described later, &lt;code&gt;cdk refactor&lt;/code&gt; fails when the changes include resource additions, removals, or property changes, so this deployment modifies neither the moved resources nor any other resources.&lt;/p&gt;

&lt;p&gt;NOTE: The command requires the &lt;code&gt;--unstable=refactor&lt;/code&gt; flag, and its options and behavior may change in the future.&lt;/p&gt;

&lt;p&gt;Here is an example run with &lt;code&gt;--dry-run&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;npx cdk refactor &lt;span class="nt"&gt;--unstable&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;refactor &lt;span class="nt"&gt;--dry-run&lt;/span&gt;

The following resources were moved or renamed:

┌─────────────────┬────────────────────────┬──────────────────────────────────────┐
│ Resource Type   │ Old Construct Path     │ New Construct Path                   │
├─────────────────┼────────────────────────┼──────────────────────────────────────┤
│ AWS::SQS::Queue │ MyStack/Queue/Resource │ MyStack/Notifications/Queue/Resource │
├─────────────────┼────────────────────────┼──────────────────────────────────────┤
│ AWS::SNS::Topic │ MyStack/Topic/Resource │ MyStack/Notifications/Topic/Resource │
└─────────────────┴────────────────────────┴──────────────────────────────────────┘
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  1-2. Cases Where cdk refactor Cannot Be Used
&lt;/h3&gt;

&lt;p&gt;While convenient, the command cannot be used in cases like the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Moves that involve resource additions, removals, or property changes&lt;/li&gt;
&lt;li&gt;Moves of resources whose properties contain strings auto-generated from the construct tree path (such as the family name of an Amazon ECS task definition or the origin ID of an Amazon CloudFront distribution)&lt;/li&gt;
&lt;li&gt;Moves of unsupported resources (such as custom resources)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Regarding the first case, &lt;code&gt;cdk refactor&lt;/code&gt; handles only resource moves. If other changes are involved, split your code changes into two steps: first perform only the move in your code and apply it with &lt;code&gt;cdk refactor&lt;/code&gt;, then add resources or change properties and apply those with &lt;code&gt;cdk deploy&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The second case actually comes down to the same reason as the first. A path-derived string changes its value along with the path. As a result, even if your code does nothing but move the resource, the change is treated as a move involving a property change.&lt;/p&gt;

&lt;p&gt;As for the third case, the CloudFormation &lt;a href="https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/stack-refactoring.html" rel="noopener noreferrer"&gt;stack refactoring&lt;/a&gt; feature that &lt;code&gt;cdk refactor&lt;/code&gt; uses internally has unsupported resources. A typical example is custom resources, and L2 constructs that use custom resources internally, such as &lt;code&gt;BucketDeployment&lt;/code&gt;, cannot be moved either.&lt;/p&gt;

&lt;p&gt;The error displayed when the changes involve more than moves is as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;A refactor operation cannot add, remove or update resources.
Only resource moves and renames are allowed.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  2. The cdk orphan + cdk import Commands
&lt;/h2&gt;

&lt;p&gt;The second method is the combination of the &lt;a href="https://docs.aws.amazon.com/cdk/v2/guide/ref-cli-cmd-orphan.html" rel="noopener noreferrer"&gt;cdk orphan&lt;/a&gt; and &lt;a href="https://docs.aws.amazon.com/cdk/v2/guide/ref-cli-cmd-import.html" rel="noopener noreferrer"&gt;cdk import&lt;/a&gt; commands, which works even in configurations where &lt;code&gt;cdk refactor&lt;/code&gt; cannot be used. The flow is to temporarily detach the resource you want to protect from replacement from the stack, and then bring it back into its new position after refactoring. The steps are as follows:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Use &lt;code&gt;cdk orphan&lt;/code&gt; to detach the resource you want to protect from replacement from the stack&lt;/li&gt;
&lt;li&gt;Refactor your code&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;cdk import&lt;/code&gt; to bring the detached resource into its new position&lt;/li&gt;
&lt;li&gt;Apply the remaining changes with &lt;code&gt;cdk deploy&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  2-1. Running cdk orphan
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;cdk orphan&lt;/code&gt; is a command that removes a resource from the stack's management without deleting it. Internally, it sets the CloudFormation &lt;code&gt;DeletionPolicy&lt;/code&gt; to &lt;code&gt;Retain&lt;/code&gt; so that the target resource is not deleted, replaces references from other resources with actual values such as the queue URL or ARN, and then removes the resource from the template. As a result, the resource leaves the stack's management but continues to exist in your AWS account.&lt;/p&gt;

&lt;p&gt;NOTE: The command requires the &lt;code&gt;--unstable=orphan&lt;/code&gt; flag, and its options and behavior may change in the future.&lt;/p&gt;

&lt;p&gt;Specify the target resource by its path and run the command as shown below. It displays the list of resources to be detached, and the process starts after you answer the confirmation prompt. In our example, the queue is the only resource we want to protect from replacement, so we specify only the queue. We leave the topic attached and let the final &lt;code&gt;cdk deploy&lt;/code&gt; in the steps replace it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;npx cdk orphan &lt;span class="nt"&gt;--unstable&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;orphan MyStack/Queue

Stack: MyStack
Resources to orphan &lt;span class="o"&gt;(&lt;/span&gt;1&lt;span class="o"&gt;)&lt;/span&gt;:
  Queue4A7E3555 &lt;span class="o"&gt;(&lt;/span&gt;AWS::SQS::Queue&lt;span class="o"&gt;)&lt;/span&gt; - /MyStack/Queue/Resource
Do you wish to orphan these resources? This will perform 3 CloudFormation deployments. &lt;span class="o"&gt;(&lt;/span&gt;y/n&lt;span class="o"&gt;)&lt;/span&gt; y

...

✅ Resources orphaned from MyStack
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2-2. Running cdk import
&lt;/h3&gt;

&lt;p&gt;Once &lt;code&gt;cdk orphan&lt;/code&gt; is complete, refactor your code and then run &lt;code&gt;cdk import&lt;/code&gt;. A prompt asks for the identifier of each resource to import: enter the URL for the queue, and for the topic, press Enter without typing anything to skip it. This brings the detached queue into the stack under its new logical ID while keeping its messages. Here is an example run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;npx cdk import

...

Perform import? &lt;span class="o"&gt;(&lt;/span&gt;y/n&lt;span class="o"&gt;)&lt;/span&gt; y

MyStack/Notifications/Queue/Resource &lt;span class="o"&gt;(&lt;/span&gt;AWS::SQS::Queue&lt;span class="o"&gt;)&lt;/span&gt;: enter QueueUrl &lt;span class="o"&gt;(&lt;/span&gt;empty to skip&lt;span class="o"&gt;)&lt;/span&gt; https://sqs.us-east-1.amazonaws.com/123456789012/MyStack-Queue4A7E3555-pNg4kbUdhvRE
MyStack/Notifications/Topic/Resource &lt;span class="o"&gt;(&lt;/span&gt;AWS::SNS::Topic&lt;span class="o"&gt;)&lt;/span&gt;: enter TopicArn &lt;span class="o"&gt;(&lt;/span&gt;empty to skip&lt;span class="o"&gt;)&lt;/span&gt;
Skipping import of MyStack/Notifications/Topic/Resource

 ✅  MyStack
Some resources were skipped. Run another cdk import or a cdk deploy to bring the stack up-to-date with your CDK app definition.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2-3. Final cdk deploy and Points to Note
&lt;/h3&gt;

&lt;p&gt;Finally, run &lt;code&gt;cdk deploy&lt;/code&gt; to apply the remaining changes, including the topic's replacement, and the whole procedure is complete.&lt;/p&gt;

&lt;p&gt;Note that this method has the following three caveats:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;cdk import&lt;/code&gt; can only bring in resource types that support CloudFormation resource import&lt;/li&gt;
&lt;li&gt;Always finish refactoring your code before running &lt;code&gt;cdk import&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Do not run a regular deployment between &lt;code&gt;cdk orphan&lt;/code&gt; and &lt;code&gt;cdk import&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Regarding the first point, the list of supported resource types is available in the CloudFormation &lt;a href="https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/resource-import-supported-resources.html" rel="noopener noreferrer"&gt;resource import&lt;/a&gt; documentation.&lt;/p&gt;

&lt;p&gt;As for the second point, &lt;code&gt;cdk import&lt;/code&gt; synthesizes your local code to determine where to import resources. If you run it with the pre-refactoring code, the resource is imported back into its original position.&lt;/p&gt;

&lt;p&gt;Regarding the third point, if you deploy in between, the queue definition in your code is created as a new, empty resource separate from the existing queue.&lt;/p&gt;




&lt;h2&gt;
  
  
  3. Setting the Construct ID to Default
&lt;/h2&gt;

&lt;p&gt;The third method is to set the resource's construct ID to &lt;code&gt;Default&lt;/code&gt; inside the custom construct you extract to. Since logical IDs are generated from paths, the idea is to assign construct IDs so that the path stays the same after refactoring. Unlike the two methods so far, no additional commands are needed.&lt;/p&gt;

&lt;h3&gt;
  
  
  3-1. L1 Constructs and Paths
&lt;/h3&gt;

&lt;p&gt;As background for this method, let's take a closer look at the path of the queue in our example. Inside an L2 construct such as &lt;code&gt;Queue&lt;/code&gt;, an L1 construct that corresponds one-to-one with the CloudFormation resource is actually defined, with the construct ID &lt;code&gt;Resource&lt;/code&gt;. Therefore, the actual path used to calculate the logical ID is &lt;code&gt;MyStack/Queue/Resource&lt;/code&gt;, and &lt;code&gt;Queue4A7E3555&lt;/code&gt; was generated from it. This is also why the paths in the example run in 1-1 ended with &lt;code&gt;/Resource&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  3-2. Keeping the Logical ID with Default
&lt;/h3&gt;

&lt;p&gt;When AWS CDK determines a logical ID, it excludes the string &lt;code&gt;Default&lt;/code&gt; from the path components used in the calculation. Taking advantage of this behavior, set the construct ID of the queue whose logical ID you want to keep to &lt;code&gt;Default&lt;/code&gt; inside the extracted custom construct. Then, give the &lt;code&gt;Notifications&lt;/code&gt; construct the same construct ID as the pre-extraction queue, namely &lt;code&gt;Queue&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;With this, the path of the queue's L1 construct becomes &lt;code&gt;MyStack/Queue/Default/Resource&lt;/code&gt;. Since &lt;code&gt;Default&lt;/code&gt; is excluded from the calculation, the logical ID remains &lt;code&gt;Queue4A7E3555&lt;/code&gt;, the same as before the extraction. All that's left is to run &lt;code&gt;cdk deploy&lt;/code&gt; as usual. In this deployment, nothing happens to the queue, whose logical ID is unchanged, and only the other changes, such as the topic's replacement, are applied. The code is as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Notifications&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Construct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;scope&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Construct&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;scope&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// The path becomes MyStack/Queue/Default/Resource, so the logical ID does not change&lt;/span&gt;
    &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Queue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Default&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// The topic's logical ID changes, but we accept its replacement&lt;/span&gt;
    &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Topic&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Topic&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Give the construct the same ID as the pre-extraction queue, 'Queue'&lt;/span&gt;
&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Notifications&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Queue&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3-3. Conditions for Applying This Method
&lt;/h3&gt;

&lt;p&gt;To apply this method, the following two conditions must be met:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Only one resource inside the extracted custom construct needs to keep its logical ID&lt;/li&gt;
&lt;li&gt;The construct ID of the extracted custom construct is the same as the original ID of the resource or construct being moved&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Regarding the first condition, only one &lt;code&gt;Default&lt;/code&gt; can exist under the same parent. In the example above as well, the queue is the only resource whose logical ID is kept, and the topic's replacement is accepted as assumed. On the other hand, you can have any number of resources that do not need to keep their logical IDs inside the construct. Along with newly added resources, you can also move existing resources whose replacement is acceptable, like the topic in our example.&lt;/p&gt;

&lt;p&gt;As for the second condition, in the example above we gave &lt;code&gt;Notifications&lt;/code&gt; the construct ID &lt;code&gt;Queue&lt;/code&gt;, so this method cannot be used if you want to give the construct a different name as part of the extraction.&lt;/p&gt;

&lt;p&gt;This method is effective for extractions centered on a single resource you want to protect from replacement. The relationship between paths and logical IDs so far is summarized below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fb1z4gm7w5ci0kzpex9gq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fb1z4gm7w5ci0kzpex9gq.png" alt="Paths and logical IDs when the construct ID is set to Default" width="799" height="283"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  4. Overriding the Logical ID
&lt;/h2&gt;

&lt;p&gt;The last method is to pin the logical ID, which AWS CDK normally determines automatically, to its pre-refactoring value. As long as the logical ID does not change, CloudFormation sees the resource as the same one. So all you need to do is apply the refactored code with a regular &lt;code&gt;cdk deploy&lt;/code&gt;. During the deployment, nothing happens to the resources whose logical IDs are pinned, and only the other changes are applied. Since this method also works for resource types not supported by resource import, it serves as a last resort when the other methods cannot be used. There are two ways to pin the logical ID.&lt;/p&gt;

&lt;h3&gt;
  
  
  4-1. Pinning with overrideLogicalId
&lt;/h3&gt;

&lt;p&gt;AWS CDK has a mechanism called an escape hatch, which lets you take out the L1 construct inside an L2 construct and manipulate it directly. As shown below, retrieve the L1 construct through &lt;code&gt;defaultChild&lt;/code&gt; of the &lt;code&gt;node&lt;/code&gt; property that every construct has, and pass the pre-refactoring logical ID to &lt;code&gt;overrideLogicalId&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Notifications&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Construct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;scope&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Construct&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;scope&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;queue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Queue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Queue&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// Pin the logical ID to its pre-refactoring value (the topic is not pinned, accepting its replacement)&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cfnQueue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;defaultChild&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;CfnQueue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;cfnQueue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;overrideLogicalId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Queue4A7E3555&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Topic&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Topic&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  4-2. Pinning with renameLogicalId
&lt;/h3&gt;

&lt;p&gt;The other way is the stack's &lt;code&gt;renameLogicalId&lt;/code&gt; method. This one operates on the logical ID itself, without going through constructs. Therefore, it also works for resources that cannot be retrieved with &lt;code&gt;defaultChild&lt;/code&gt;, such as related resources that an L2 construct generates internally.&lt;/p&gt;

&lt;p&gt;Use it in the following order. First, run &lt;code&gt;cdk synth&lt;/code&gt; once on the code with only the refactoring applied, and check the newly generated logical ID in the output template. Then, as shown below, add a &lt;code&gt;renameLogicalId&lt;/code&gt; call with the logical ID you checked as the first argument and the pre-refactoring logical ID you want to keep as the second.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyStack&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Stack&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;scope&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Construct&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nx"&gt;StackProps&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;scope&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// Resource definitions omitted&lt;/span&gt;

    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;renameLogicalId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;NotificationsQueue91395D8F&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Queue4A7E3555&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  4-3. Downside: The Pinning Code Remains
&lt;/h3&gt;

&lt;p&gt;The downside of this method is that the code pinning the logical ID remains after the refactoring. Since it leaves a value that AWS CDK normally determines automatically explicitly specified, it is a good idea to leave a comment explaining why the ID is pinned.&lt;/p&gt;




&lt;h2&gt;
  
  
  How to Choose Among the Methods
&lt;/h2&gt;

&lt;p&gt;If no resource you want to protect from replacement is moved, no countermeasure is needed and you can simply deploy. If one is needed, first consider &lt;code&gt;cdk refactor&lt;/code&gt;, which completes with a single command. If your configuration does not allow it, consider &lt;code&gt;cdk orphan&lt;/code&gt; + &lt;code&gt;cdk import&lt;/code&gt;. If that is not an option either, use the construct ID &lt;code&gt;Default&lt;/code&gt; approach when the extraction you want to perform meets the conditions in 3-3, and otherwise use logical ID overriding as a last resort.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F7gxvznft67pps12cdf8g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F7gxvznft67pps12cdf8g.png" alt="Decision flow for choosing a method" width="691" height="701"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Appendix 1. Accepting Replacement of Stateless Resources
&lt;/h2&gt;

&lt;p&gt;As mentioned earlier, you do not need to prevent replacement for every resource. So which resources can tolerate replacement? Stateless resources, such as Lambda functions and the SNS topic in our example, hold no data, so replacing them is generally not a problem. What mainly needs protection are resources that lose their data when replaced, such as Amazon DynamoDB tables, Amazon S3 buckets, and the queue in our example. Accepting replacement for everything else greatly reduces the effort of refactoring.&lt;/p&gt;




&lt;h2&gt;
  
  
  Appendix 2. Leaving Physical Names Unspecified
&lt;/h2&gt;

&lt;p&gt;When you accept replacement, be careful with how resource physical names are handled. A physical name is the actual resource name on AWS, such as a bucket name or a function name. In a CloudFormation replacement, the new resource is created first, and the old one is deleted afterward. Consequently, if your code specifies a physical name, the original resource with the same name still exists at creation time, and the deployment fails with an "already exists" error.&lt;/p&gt;

&lt;p&gt;The basic way to avoid this is not to specify physical names. If omitted, a unique name is generated automatically, so names never collide during replacement. Leaving physical names unspecified also gives you more flexibility for refactoring.&lt;/p&gt;




&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this article, I introduced the following four methods for refactoring safely in AWS CDK:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The &lt;code&gt;cdk refactor&lt;/code&gt; command&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;cdk orphan&lt;/code&gt; + &lt;code&gt;cdk import&lt;/code&gt; commands&lt;/li&gt;
&lt;li&gt;Setting the construct ID to &lt;code&gt;Default&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Overriding the logical ID&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Knowing these methods lets you clean up your code with confidence, even for stacks that are already released. Alongside them, accepting the replacement of stateless resources and leaving physical names unspecified are also effective policies. I hope you will incorporate these techniques into your daily development.&lt;/p&gt;

</description>
      <category>aws</category>
      <category>awscdk</category>
      <category>cdk</category>
      <category>cloudformation</category>
    </item>
    <item>
      <title>Understanding AWS Blocks as a CDK Developer</title>
      <dc:creator>Kenta Goto</dc:creator>
      <pubDate>Wed, 17 Jun 2026 10:45:55 +0000</pubDate>
      <link>https://dev.to/aws-heroes/understanding-aws-blocks-as-a-cdk-developer-5gpo</link>
      <guid>https://dev.to/aws-heroes/understanding-aws-blocks-as-a-cdk-developer-5gpo</guid>
      <description>&lt;h2&gt;
  
  
  Understanding AWS Blocks as a CDK Developer
&lt;/h2&gt;

&lt;p&gt;I tried out &lt;a href="https://docs.aws.amazon.com/blocks/latest/devguide/what-is-blocks.html" rel="noopener noreferrer"&gt;AWS Blocks&lt;/a&gt;, announced as a preview on June 17, 2026, from the perspective of someone who normally writes AWS CDK.&lt;/p&gt;

&lt;h3&gt;
  
  
  What Is AWS Blocks?
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://docs.aws.amazon.com/blocks/latest/devguide/what-is-blocks.html" rel="noopener noreferrer"&gt;AWS Blocks&lt;/a&gt; is a toolkit for quickly building the backend of a full-stack app. It was announced as a preview on June 17, 2026, and its source code is published as open source at &lt;a href="https://github.com/aws-devtools-labs/aws-blocks" rel="noopener noreferrer"&gt;aws-devtools-labs/aws-blocks&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;At its center is a component called a &lt;strong&gt;Building Block&lt;/strong&gt;. It is a unit of functionality such as a data store or authentication, and a single Building Block plays the following three roles at the same time.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;A CDK Construct&lt;/strong&gt; (the infrastructure definition at deploy time)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A runtime implementation&lt;/strong&gt; (AWS SDK calls running on Lambda)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A local mock&lt;/strong&gt; (runs with &lt;code&gt;npm run dev&lt;/code&gt;, no AWS environment needed)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In other words, the same Building Block description acts as a mock during local development, as a CDK Construct at deploy time, and as AWS SDK calls in production. As a result, a single piece of code can run locally on your machine without an AWS account, and can be deployed to AWS as is.&lt;/p&gt;

&lt;p&gt;The Building Blocks provided include &lt;code&gt;KVStore&lt;/code&gt; and &lt;code&gt;DistributedTable&lt;/code&gt; (data), &lt;code&gt;AuthBasic&lt;/code&gt; (authentication), &lt;code&gt;Realtime&lt;/code&gt; (real-time communication), &lt;code&gt;FileBucket&lt;/code&gt; (files), and &lt;code&gt;Database&lt;/code&gt; (SQL).&lt;/p&gt;

&lt;p&gt;Developers write both the infrastructure (such as data) and the API logic that uses it together in &lt;code&gt;aws-blocks/index.ts&lt;/code&gt; (which can also be split into multiple files).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// aws-blocks/index.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Scope&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;ApiNamespace&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;KVStore&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@aws-blocks/blocks&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;scope&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Scope&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;my-app&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cache&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;KVStore&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;scope&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cache&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;   &lt;span class="c1"&gt;// ← at deploy time, this becomes a DynamoDB table (infrastructure)&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;api&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ApiNamespace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;scope&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;api&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;getValue&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="c1"&gt;// ← API logic that uses that table&lt;/span&gt;
&lt;span class="p"&gt;}));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The infrastructure definition &lt;code&gt;new KVStore(...)&lt;/code&gt; and the API logic that calls it sit side by side in the same file. If CDK is about "declaring infrastructure as code (Infrastructure as Code)," then AWS Blocks is framed as "deriving infrastructure from application code (Infrastructure from Code, IfC)."&lt;/p&gt;

&lt;p&gt;The frontend simply calls this &lt;code&gt;api&lt;/code&gt; directly, like &lt;code&gt;import { api } from 'aws-blocks'&lt;/code&gt;, so that &lt;strong&gt;the frontend and backend share types without any code-generation step in between&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For type integration, type-safe clients are provided not only for web frameworks such as Next, Nuxt, React, and Vue, but also for native mobile targets such as Swift, Kotlin, and Dart/Flutter. In TypeScript you can load them directly via &lt;code&gt;import&lt;/code&gt;, whereas for native targets the client code is generated at build time from a spec called &lt;code&gt;blocks.spec.json&lt;/code&gt;, and the backend is called over JSON-RPC.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Same import Resolves to Different Files Depending on Context
&lt;/h3&gt;

&lt;p&gt;This is probably the most distinctive feature of AWS Blocks.&lt;/p&gt;

&lt;p&gt;Both the &lt;code&gt;import { api } from 'aws-blocks'&lt;/code&gt; you write on the frontend and the &lt;code&gt;new DistributedTable(...)&lt;/code&gt; you call inside the backend's &lt;code&gt;aws-blocks/index.ts&lt;/code&gt; look like ordinary TypeScript. Yet this &lt;code&gt;'aws-blocks'&lt;/code&gt; (and each Building Block) &lt;strong&gt;resolves to a completely different file depending on the context in which it runs&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This "resolves to something different depending on context" behavior is stated explicitly in the official README. It explains that the same &lt;code&gt;new KVStore(scope, 'todos')&lt;/code&gt; becomes a local store during development, an Amazon DynamoDB table at deploy time, and an SDK call in production (without changing a single line of code). Below, I'll trace how this works from the implementation.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Context&lt;/th&gt;
&lt;th&gt;Resolves to&lt;/th&gt;
&lt;th&gt;Role&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Local development (&lt;code&gt;npm run dev&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;Mock implementation&lt;/td&gt;
&lt;td&gt;Stores to disk (&lt;code&gt;.bb-data/&lt;/code&gt;). No AWS environment needed&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;CDK synth&lt;/td&gt;
&lt;td&gt;Infrastructure (CDK Construct)&lt;/td&gt;
&lt;td&gt;Defines DynamoDB tables, Lambdas, etc.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Lambda runtime&lt;/td&gt;
&lt;td&gt;Application implementation&lt;/td&gt;
&lt;td&gt;Calls the AWS SDK&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h4&gt;
  
  
  Mechanism: conditional exports + a global variable
&lt;/h4&gt;

&lt;p&gt;The mechanism consists of two elements.&lt;/p&gt;

&lt;p&gt;The first is Node.js conditional exports (the &lt;code&gt;exports&lt;/code&gt; field in &lt;code&gt;package.json&lt;/code&gt;). Looking at the &lt;code&gt;package.json&lt;/code&gt; of each Building Block, &lt;code&gt;exports&lt;/code&gt; is defined as follows.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json-doc"&gt;&lt;code&gt;&lt;span class="c1"&gt;// package.json of @aws-blocks/bb-distributed-table (exports excerpt)&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"exports"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"."&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"browser"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"./dist/index.browser.js"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"cdk"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"types"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"./dist/index.cdk.d.ts"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"default"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"./dist/index.cdk.js"&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"aws-runtime"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"./dist/index.aws.js"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"types"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"./dist/index.mock.d.ts"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"default"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"./dist/index.mock.js"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;exports&lt;/code&gt; is a mechanism that assigns a different file per condition; when an import is resolved, the file matching an active condition is loaded. &lt;code&gt;browser&lt;/code&gt; / &lt;code&gt;types&lt;/code&gt; / &lt;code&gt;default&lt;/code&gt; are standard Node.js conditions, whereas &lt;code&gt;cdk&lt;/code&gt; and &lt;code&gt;aws-runtime&lt;/code&gt; are conditions that AWS Blocks defines on its own, and they are not active unless explicitly specified at startup.&lt;/p&gt;

&lt;p&gt;And the reason the resolution target changes per context is that each command passes "which condition to activate" at startup.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;cdk synth&lt;/code&gt; runs with &lt;code&gt;NODE_OPTIONS=--conditions=cdk&lt;/code&gt;, so it resolves to &lt;code&gt;cdk&lt;/code&gt; (= Construct); the Lambda bundle passes &lt;code&gt;--conditions: aws-runtime&lt;/code&gt; to esbuild, so it resolves to &lt;code&gt;aws-runtime&lt;/code&gt; (= runtime). &lt;code&gt;npm run dev&lt;/code&gt; specifies no condition, so it resolves to &lt;code&gt;default&lt;/code&gt; (= mock), and for type checking TypeScript always looks at &lt;code&gt;types&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The second is the hand-off at synth time. Before dynamically importing the &lt;code&gt;aws-blocks/index.ts&lt;/code&gt; that the developer implemented, the CDK layer holds a reference to the stack instance currently being synthesized in a global variable. The &lt;code&gt;Scope&lt;/code&gt; and each Building Block created inside &lt;code&gt;index.ts&lt;/code&gt; are not given a parent stack explicitly, so they refer to this global variable to resolve which stack they belong to.&lt;/p&gt;

&lt;p&gt;NOTE: &lt;a href="https://github.com/aws-devtools-labs/aws-blocks/blob/fc099a173eb39e97ebe7290409abf8eb18f3b1ce/packages/core/src/cdk/blocks-backend.ts#L211-L248" rel="noopener noreferrer"&gt;&lt;code&gt;packages/core/src/cdk/blocks-backend.ts&lt;/code&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// packages/core/src/cdk/blocks-backend.ts (excerpt; the infrastructure-building parts are omitted)&lt;/span&gt;
&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;scope&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Construct&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;BlocksBackendProps&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;scope&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="c1"&gt;// Expose self to Building Blocks at CDK time&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;globalThis&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;CURRENT_BLOCKS_STACK&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// ← expose the current stack globally&lt;/span&gt;
  &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;scope&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Construct&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;BlocksBackendProps&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;assertCdkConditionActive&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;backend&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;BlocksBackend&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;scope&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;mod&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;backendCDKPath&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;?stack=&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;   &lt;span class="c1"&gt;// ← load index.ts&lt;/span&gt;
  &lt;span class="c1"&gt;// ...&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;backend&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In short, when &lt;code&gt;new DistributedTable(...)&lt;/code&gt; runs inside the &lt;code&gt;index.ts&lt;/code&gt; the developer implemented, that Construct gets the current stack from this global variable, creates a DynamoDB table, and grants the stack's Lambda read/write permissions on the table. This is how &lt;strong&gt;writing application code itself becomes the infrastructure definition&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Setting Up a Project
&lt;/h3&gt;

&lt;p&gt;An AWS Blocks app starts from npm's create command (Node.js 22 or later / npm 10 or later is required).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm create @aws-blocks/blocks-app@latest my-app
&lt;span class="nb"&gt;cd &lt;/span&gt;my-app
npm &lt;span class="nb"&gt;install
&lt;/span&gt;npm run dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;npm run dev&lt;/code&gt; starts a local server at &lt;code&gt;http://localhost:3000&lt;/code&gt;, and all Building Blocks run as mock implementations (no AWS account or credentials needed). You can also choose a template, like &lt;code&gt;--template react&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The generated directory looks roughly like this (the frontend contents vary by template, but the structure of &lt;code&gt;aws-blocks/&lt;/code&gt; is the same).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;my-app/
├── aws-blocks/            # the entire backend
│   ├── index.ts           #   backend definition (API, data, auth) ← what developers mainly write
│   ├── index.cdk.ts       #   CDK entry (assembles the stack with BlocksStack.create)
│   ├── index.handler.ts   #   entry for the Lambda handler at deploy time
│   ├── client.js          #   typed client for the frontend (auto-generated)
│   └── scripts/           #   helper scripts such as the dev server
├── src/                   # frontend (React / Next, etc., depending on the template)
├── cdk.json
└── package.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;aws-blocks/index.ts&lt;/code&gt; we've seen so far is the body of the backend definition, and &lt;code&gt;aws-blocks/index.cdk.ts&lt;/code&gt; is the entry point as CDK.&lt;/p&gt;

&lt;p&gt;Basically, what developers implement is &lt;code&gt;aws-blocks/index.ts&lt;/code&gt;, and there's no need to touch &lt;code&gt;aws-blocks/index.cdk.ts&lt;/code&gt;. However, as described later, you can also customize the CDK configuration by touching this file.&lt;/p&gt;

&lt;h3&gt;
  
  
  Implementing a Sample
&lt;/h3&gt;

&lt;p&gt;Here's what it looks like in practice. Using &lt;code&gt;AuthBasic&lt;/code&gt;, &lt;code&gt;DistributedTable&lt;/code&gt;, and &lt;code&gt;ApiNamespace&lt;/code&gt;, I've implemented a simple Todo app.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// aws-blocks/index.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;ApiNamespace&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Scope&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;AuthBasic&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;DistributedTable&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@aws-blocks/blocks&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;z&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;zod&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;scope&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Scope&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;todo-app&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;auth&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;AuthBasic&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;scope&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;auth&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;passwordPolicy&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;minLength&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;authApi&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;auth&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createApi&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;// The Zod schema serves as runtime validation + the TypeScript type + the DynamoDB table shape, all at once&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;todoSchema&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;z&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;object&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;z&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;string&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;    &lt;span class="c1"&gt;// partition key (isolated per user)&lt;/span&gt;
  &lt;span class="na"&gt;todoId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;z&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;string&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;    &lt;span class="c1"&gt;// sort key&lt;/span&gt;
  &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;z&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;string&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
  &lt;span class="na"&gt;completed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;z&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;boolean&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;todos&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;DistributedTable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;scope&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;todos&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;schema&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;todoSchema&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;partitionKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;userId&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;sortKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;todoId&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;api&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ApiNamespace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;scope&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;api&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nf"&gt;createTodo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;auth&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;requireAuth&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;   &lt;span class="c1"&gt;// ← this makes the method require authentication&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;todo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;username&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;todoId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;36&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
      &lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;completed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;todos&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;put&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;todo&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;todo&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;

  &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nf"&gt;listTodos&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;auth&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;requireAuth&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fromAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="nx"&gt;todos&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;query&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;where&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;equals&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;username&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;}));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This sample implementation shows the following characteristics as well.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Authentication is specified explicitly per method:&lt;/strong&gt; each method is a public RPC endpoint with no authentication by default. Call &lt;code&gt;auth.requireAuth(context)&lt;/code&gt; at the beginning of any method you want to require authentication.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reusing the Zod schema:&lt;/strong&gt; a single Zod schema can cover runtime validation, the TS type, and the DynamoDB table definition.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Also, the frontend can call the API definition implemented here in a typed way, like &lt;code&gt;import { api } from 'aws-blocks'&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Deploying
&lt;/h3&gt;

&lt;p&gt;Once it works locally, you can deploy the same code to AWS as is.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm run sandbox   &lt;span class="c"&gt;# to a per-developer temporary environment (backend on AWS, frontend served locally)&lt;/span&gt;
npm run deploy    &lt;span class="c"&gt;# full production deploy (including frontend hosting)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;npm run sandbox&lt;/code&gt; is a temporary stack for quickly checking behavior, and &lt;code&gt;npm run deploy&lt;/code&gt; is a production deploy that includes frontend hosting. Both run CDK internally (synth → deploy with &lt;code&gt;--conditions=cdk&lt;/code&gt;, plus generating client code with &lt;code&gt;--conditions=aws-runtime&lt;/code&gt;), and the first time, just like ordinary CDK, you need AWS credentials and &lt;code&gt;cdk bootstrap&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Whereas &lt;code&gt;npm run dev&lt;/code&gt; used local mocks to realize the app's behavior, here the app is deployed to a real environment without changing a single line of code. The earlier mechanism where "the same import resolves to different files depending on context" comes in handy here.&lt;/p&gt;

&lt;h3&gt;
  
  
  AWS Blocks from a CDK Perspective
&lt;/h3&gt;

&lt;p&gt;A distinctive feature of AWS Blocks is that you can implement with it even without being familiar with AWS or CDK, but as a CDK user you may sometimes want to extend the CDK definition. AWS Blocks supports those use cases too.&lt;/p&gt;

&lt;h4&gt;
  
  
  You Can Extend the CDK Definition
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;aws-blocks/index.cdk.ts&lt;/code&gt; is the CDK layer, where &lt;code&gt;BlocksStack&lt;/code&gt; is invoked. Here you can also define additional resources you want to create and pass them to &lt;code&gt;blocksStack.handler&lt;/code&gt; (the Lambda's &lt;code&gt;NodejsFunction&lt;/code&gt;).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// aws-blocks/index.cdk.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;sqs&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;aws-cdk-lib/aws-sqs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="c1"&gt;// ...&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;blocksStack&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;BlocksStack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;my-app&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;backendHandlerPath&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;__dirname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;index.handler.ts&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="na"&gt;backendCDKPath&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;__dirname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;index.ts&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="c1"&gt;// Create raw CDK resources in the same stack as Blocks, and pass permissions and environment variables to the Lambda&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;jobsQueue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;sqs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Queue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;blocksStack&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;JobsQueue&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;jobsQueue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;grantSendMessages&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;blocksStack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;blocksStack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEnvironment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;JOBS_QUEUE_URL&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;jobsQueue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;queueUrl&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;blocksStack.handler&lt;/code&gt; exposes methods like &lt;code&gt;addToRolePolicy&lt;/code&gt; (adding IAM) and &lt;code&gt;addEnvironment&lt;/code&gt; (injecting environment variables), and you can also grant the Lambda IAM permissions on your own CDK resources using &lt;code&gt;grant~&lt;/code&gt; and similar methods.&lt;/p&gt;

&lt;h4&gt;
  
  
  Checking the Generated CloudFormation with cdk synth
&lt;/h4&gt;

&lt;p&gt;Since AWS Blocks is ultimately AWS CDK under the hood, you can of course run synth too.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx cdk synth
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Among the files generated when you set up the project is &lt;code&gt;cdk.json&lt;/code&gt;, whose app command is &lt;code&gt;npx tsx -C cdk aws-blocks/index.cdk.ts&lt;/code&gt;. The point is this &lt;code&gt;-C cdk&lt;/code&gt; (= &lt;code&gt;--conditions=cdk&lt;/code&gt;), which makes each Building Block resolve to a CDK Construct.&lt;/p&gt;

&lt;p&gt;Conversely, if you run it without &lt;code&gt;--conditions=cdk&lt;/code&gt;, the Building Blocks would resolve as mocks, so AWS Blocks stops you with an explicit &lt;code&gt;Missing --conditions=cdk&lt;/code&gt; error.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Error: Missing --conditions=cdk: Building Blocks will silently load mock implementations instead of CDK constructs.

Fix: Set NODE_OPTIONS="--conditions=cdk" before running CDK synth:
  NODE_OPTIONS="--conditions=cdk" npx cdk synth
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Integration Patterns
&lt;/h4&gt;

&lt;p&gt;The &lt;a href="https://github.com/aws-devtools-labs/aws-blocks/blob/fc099a173eb39e97ebe7290409abf8eb18f3b1ce/docs/guides/extending-with-existing-aws-resources.md" rel="noopener noreferrer"&gt;documentation&lt;/a&gt; presents four patterns for integrating with existing infrastructure.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;CDK-in-Blocks:&lt;/strong&gt; connect raw CDK resources to the Blocks Lambda and use them (for resources that have no corresponding Building Block)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;fromExisting&lt;/code&gt;:&lt;/strong&gt; wrap already-deployed AWS resources with a Building Block and use them (for resources that have a corresponding Building Block)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Custom Block:&lt;/strong&gt; build your own Building Block&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Vendorize:&lt;/strong&gt; pull the Block's code into your own repository&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's look at a concrete example. SQS has no corresponding Building Block, so you connect it as raw CDK with CDK-in-Blocks. For instance, if you want to "send a message from the Blocks API to an existing SQS queue created by another stack," you reference the existing queue from &lt;code&gt;index.cdk.ts&lt;/code&gt; and pass permissions and environment variables to the Lambda.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// aws-blocks/index.cdk.ts — reference an existing queue owned by another stack and wire it to the Lambda&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;sqs&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;aws-cdk-lib/aws-sqs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;externalQueue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;sqs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fromQueueArn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nx"&gt;blocksStack&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ExternalQueue&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;arn:aws:sqs:ap-northeast-1:123456789012:my-queue&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;externalQueue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;grantSendMessages&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;blocksStack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;blocksStack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEnvironment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;EXTERNAL_QUEUE_URL&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;externalQueue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;queueUrl&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On the other hand, for resources that have a corresponding Building Block (existing DynamoDB tables, S3 buckets, RDS, Cognito, and so on), wrapping them with &lt;code&gt;fromExisting&lt;/code&gt; is a better fit. Just by passing the existing resource name to something like &lt;code&gt;table:&lt;/code&gt; when creating the Building Block, you can use the typed API and the local mock as is, and the Building Block grants IAM permissions automatically too. You write this on the &lt;code&gt;index.ts&lt;/code&gt; (backend definition) side.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// aws-blocks/index.ts — wrap an existing DynamoDB table with DistributedTable&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;todos&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;DistributedTable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;scope&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;todos&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;schema&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;todoSchema&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;partitionKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;userId&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;sortKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;todoId&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;table&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;DistributedTable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fromExisting&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;my-existing-todos-table&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="c1"&gt;// ← pass the existing table name&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  IAM Is Automatic and Least-Privilege per Block
&lt;/h4&gt;

&lt;p&gt;When you instantiate a Building Block, the Lambda is automatically given &lt;strong&gt;permissions limited to that resource&lt;/strong&gt;. For example, with &lt;code&gt;KVStore&lt;/code&gt;, read/write permissions on the generated DynamoDB table (and its indexes) are granted scoped to that table's ARN (internally equivalent to CDK's &lt;code&gt;grantReadWriteData&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;That is, it doesn't take the broad approach of granting &lt;code&gt;dynamodb:*&lt;/code&gt; on all tables, and it can't touch other tables or other resources. The reason least privilege is applied without you hand-writing IAM policies is that the Building Block makes good use of CDK's &lt;code&gt;grant~&lt;/code&gt; behind the scenes.&lt;/p&gt;

&lt;h3&gt;
  
  
  Constraints You Should Know
&lt;/h3&gt;

&lt;h4&gt;
  
  
  The Architecture Is a Lambda-lith (All Methods in a Single Lambda)
&lt;/h4&gt;

&lt;p&gt;When I actually ran &lt;code&gt;cdk synth&lt;/code&gt; and checked the contents, &lt;strong&gt;all the API methods coexisted in a single Lambda function&lt;/strong&gt; (memory 2048 MB / timeout 900 seconds). It's a configuration where API Gateway's proxy integration routes everything to a single Lambda (a so-called Lambda-lith).&lt;/p&gt;

&lt;p&gt;This comes with the following constraints.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You can't separate scaling, permission isolation, or deployment units per method&lt;/li&gt;
&lt;li&gt;Bundle size and cold starts grow in proportion to the size of the API&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As of now, it's not suited to use cases where you want to split functions in a microservices style. By contrast, for a prototype or a small-to-medium API, a single function works perfectly well, so this won't be a problem as long as you understand it as a design trade-off. That said, this configuration reflects how it's built at this point in time, and there seems to be room for it to change in future updates.&lt;/p&gt;

&lt;h4&gt;
  
  
  Side Effects of &lt;code&gt;new&lt;/code&gt;
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;new DistributedTable(...)&lt;/code&gt; and &lt;code&gt;new KVStore(...)&lt;/code&gt; are not just object creation; behind the scenes they are converted into CDK resource definitions. AWS Blocks is built on a philosophy that blurs the boundary between app and infrastructure even more than AWS CDK does, so those accustomed to recent, modern app development may feel some discomfort here.&lt;/p&gt;

&lt;p&gt;For example, suppose you &lt;code&gt;new&lt;/code&gt; a resource you access in your business logic within that logic, and then, in order to access the same resource from another endpoint, you also &lt;code&gt;new&lt;/code&gt; that resource definition in a different file. The same resource definition then ends up being &lt;code&gt;new&lt;/code&gt;-ed in the CDK code, and an Already Exists error occurs at the CDK layer. It's also worth keeping in the back of your mind that casually increasing instances as if they were ordinary classes unintentionally increases your infrastructure, and casually deleting them deletes the actual resources too.&lt;/p&gt;

&lt;p&gt;To avoid this, you need to declare the resource definition somewhere in a separate file (a class or a function) and have each piece of logic load it. With careful design and proper model separation, it's entirely possible to implement this without any awkwardness, but if you don't do it well, you end up with an implementation that has merely separated app and infrastructure, which can look like it goes against the philosophy of AWS Blocks.&lt;/p&gt;

&lt;p&gt;Conversely, if you have concerns about this area, I think you may be better off simply using AWS CDK.&lt;/p&gt;

&lt;h3&gt;
  
  
  When to Use It
&lt;/h3&gt;

&lt;p&gt;The first cases that come to mind are when you want to write code without being conscious of AWS or CDK, or when you don't have members who are very familiar with them. I also think it fits cases where the application you're building isn't very large—such as a prototype or internal tool you just want to launch quickly, or when you want to build something full-stack easily.&lt;/p&gt;

&lt;p&gt;As a CDK user too, I think it's a good fit, since—as mentioned earlier—you can also extend CDK in &lt;code&gt;index.cdk.ts&lt;/code&gt;. After all, CDK's flavor seeps through in how you write the code, with things like &lt;code&gt;scope&lt;/code&gt; and &lt;code&gt;id&lt;/code&gt;, so knowing CDK may let you implement smoothly.&lt;/p&gt;

</description>
      <category>aws</category>
      <category>awscdk</category>
      <category>cdk</category>
      <category>awsblocks</category>
    </item>
    <item>
      <title>AWS CDK Deployment Best Practices</title>
      <dc:creator>Kenta Goto</dc:creator>
      <pubDate>Sun, 05 Apr 2026 13:22:41 +0000</pubDate>
      <link>https://dev.to/aws-heroes/aws-cdk-deployment-best-practices-3doo</link>
      <guid>https://dev.to/aws-heroes/aws-cdk-deployment-best-practices-3doo</guid>
      <description>&lt;h2&gt;
  
  
  Best Practices for AWS CDK Deployment
&lt;/h2&gt;

&lt;p&gt;In AWS CDK, you can define and deploy stacks in various ways. However, with so many approaches available, it can be difficult to determine which one is best. In this article, I will share what I consider to be best practices for deploying with AWS CDK.&lt;/p&gt;

&lt;p&gt;Rather than covering resource composition or Construct implementation patterns, I have specifically focused the scope on &lt;strong&gt;deployment-related practices in CDK&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This article introduces the following four approaches:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Use Static Stack Creation + Stage&lt;/li&gt;
&lt;li&gt;Synthesize once, deploy many&lt;/li&gt;
&lt;li&gt;Separate the asset build/publish and deploy phases&lt;/li&gt;
&lt;li&gt;Commit cdk.context.json&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Disclaimer
&lt;/h2&gt;

&lt;p&gt;While this article is titled "Best Practices," &lt;strong&gt;the content reflects my own ongoing experimentation and represents just one example&lt;/strong&gt;. &lt;strong&gt;It is not necessarily the definitive answer&lt;/strong&gt;, so please consider it as a reference.&lt;/p&gt;

&lt;p&gt;Additionally, the most appropriate approach may vary depending on your team and environment. Please keep this in mind.&lt;/p&gt;




&lt;h2&gt;
  
  
  1. Use Static Stack Creation + Stage
&lt;/h2&gt;

&lt;p&gt;The first topic is about how to define stacks. I recommend using static stack creation combined with Stage.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;cdk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;App&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;MyStage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Dev&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nf"&gt;getProps&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Dev&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="na"&gt;env&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;region&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;us-east-1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;account&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;111111111111&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;MyStage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Stg&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nf"&gt;getProps&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Stg&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="na"&gt;env&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;region&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;us-east-1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;account&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;222222222222&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;MyStage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Prod&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nf"&gt;getProps&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Prod&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="na"&gt;env&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;region&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;us-east-1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;account&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;333333333333&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Static Stack Creation vs Dynamic Stack Creation
&lt;/h3&gt;

&lt;p&gt;There are two types of approaches to creating stacks: static and dynamic.&lt;/p&gt;

&lt;p&gt;Static stack creation is a method where you explicitly define the stack configuration for each environment (such as dev, stg, and prod) in your code. Specifically, you create a separate stack instance with &lt;code&gt;new&lt;/code&gt; for each environment.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;cdk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;App&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;MyStack&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;DevStack&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;MyStack&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;StgStack&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;MyStack&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ProdStack&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On the other hand, dynamic stack creation is a method where you create only a single stack instance with &lt;code&gt;new&lt;/code&gt; and pass environment information such as dev / stg / prod via the &lt;code&gt;--context&lt;/code&gt; (&lt;code&gt;-c&lt;/code&gt;) option of &lt;code&gt;cdk deploy&lt;/code&gt; to generate environment-specific stacks.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;cdk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;App&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;env&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;tryGetContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;env&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;MyStack&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;Stack`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Why Static Stack Creation Is Recommended
&lt;/h3&gt;

&lt;p&gt;There are several differences between the two approaches, but the main difference is &lt;strong&gt;whether all environments are synthesized at once&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;cdk deploy&lt;/code&gt; command internally executes a synthesis process equivalent to the &lt;code&gt;cdk synth&lt;/code&gt; command. And the &lt;code&gt;cdk synth&lt;/code&gt; command basically synthesizes all stacks in the entire CDK app. This means that &lt;strong&gt;with static stack creation, stacks for all environments such as dev / stg / prod are synthesized at once&lt;/strong&gt;. On the other hand, with dynamic stack creation, only the stack for the environment specified via the &lt;code&gt;--context&lt;/code&gt; option of the &lt;code&gt;cdk deploy&lt;/code&gt; command is synthesized.&lt;/p&gt;

&lt;p&gt;Because of this characteristic, static stack creation offers the advantage of &lt;strong&gt;detecting production environment errors at an earlier stage&lt;/strong&gt;. For example, if the dev code is correct but the prod code has an error, you can detect the error before actually deploying to prod.&lt;/p&gt;

&lt;p&gt;For this reason, I recommend static stack creation.&lt;/p&gt;

&lt;h3&gt;
  
  
  Cases Where Dynamic Stack Creation Is a Better Fit
&lt;/h3&gt;

&lt;p&gt;If you want to &lt;strong&gt;avoid having prod fail because of a dev error&lt;/strong&gt;, dynamic stack creation might be a better fit.&lt;/p&gt;

&lt;p&gt;Also, in large-scale projects where synthesizing stacks for all environments at once takes too long, dynamic stack creation might be the better choice.&lt;/p&gt;

&lt;p&gt;For more details about each approach, please refer to the article "&lt;a href="https://dev.to/aws-heroes/cdk-environment-management-static-vs-dynamic-stack-creation-383l"&gt;CDK Environment Management: Static vs Dynamic Stack Creation&lt;/a&gt;", which I co-authored with Thorsten Höger, an AWS Hero from Germany.&lt;/p&gt;

&lt;h3&gt;
  
  
  Hybrid Approach
&lt;/h3&gt;

&lt;p&gt;While I recommended static stack creation, you can also combine it with the dynamic stack creation approach using context.&lt;/p&gt;

&lt;p&gt;This is effective, for example, when &lt;strong&gt;multiple people share a single AWS account for development and you want to create personal verification environments&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;As shown below, by passing information to identify individuals in the stack name via the &lt;code&gt;--context&lt;/code&gt; (&lt;code&gt;-c&lt;/code&gt;) option of the &lt;code&gt;cdk deploy&lt;/code&gt; command, you can dynamically generate stacks for personal environments. If personal environments are only needed for Dev, you can keep the static stack creation code as-is for Stg and Prod.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx cdk deploy &lt;span class="nt"&gt;-c&lt;/span&gt; &lt;span class="nv"&gt;owner&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;goto Dev/&lt;span class="k"&gt;*&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;owner&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;tryGetContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;owner&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;devStageName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;owner&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="s2"&gt;`Dev-&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;owner&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Dev&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;MyStage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;devStageName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nf"&gt;getProps&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Dev&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="na"&gt;env&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;region&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;us-east-1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;account&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;111111111111&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="c1"&gt;// Stg and Prod are the same as before&lt;/span&gt;
&lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If each person has their own dedicated AWS environment, you can define stacks for personal environments by specifying your own AWS account ID in the &lt;code&gt;account&lt;/code&gt; field, without using context.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;cdk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;App&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;MyStage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Dev&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nf"&gt;getProps&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Dev&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="na"&gt;env&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;region&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;us-east-1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;account&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;AWS_ACCOUNT_ID&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;111111111111&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Benefits of Stage
&lt;/h3&gt;

&lt;p&gt;Stage is a concept positioned between App and Stack, serving as a unit for grouping multiple stacks. In applications composed of multiple stacks, using the static approach described earlier to explicitly create stack instances with &lt;code&gt;new&lt;/code&gt; for each environment would require creating instances for the combination of all environments and stacks, resulting in verbose code.&lt;/p&gt;

&lt;p&gt;By using Stage, you can create a Stage instance with &lt;code&gt;new&lt;/code&gt; for each environment and define multiple stacks within it, keeping your code clean. Even if you currently have only one stack, it is a good idea to use Stage from the beginning if there is a possibility that stacks will increase in the future.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyStage&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;cdk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Stage&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;scope&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;cdk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nx"&gt;cdk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;StageProps&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;scope&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;StackA&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;StackA&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;StackB&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;StackB&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;StackC&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;StackC&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;cdk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;App&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;MyStage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Dev&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;MyStage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Stg&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;MyStage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Prod&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For more details about Stage, please refer to the article "&lt;a href="https://dev.to/aws-heroes/how-to-use-aws-cdk-stage-and-when-to-choose-static-vs-dynamic-stack-creation-35h"&gt;How to Use AWS CDK Stage and When to Choose Static vs Dynamic Stack Creation&lt;/a&gt;."&lt;/p&gt;

&lt;h3&gt;
  
  
  Notes on Migrating to Stage
&lt;/h3&gt;

&lt;p&gt;Migrating non-Stage stacks to Stage is generally not difficult.&lt;/p&gt;

&lt;p&gt;When using Stage, stack names follow the rule &lt;code&gt;${stageName}-${Stack ID}&lt;/code&gt; (e.g., &lt;code&gt;MyStage-SampleStack&lt;/code&gt;), so simply introducing Stage will change the stack names. This means a new stack creation process will be triggered (a deletion process for the original stack will not be triggered).&lt;/p&gt;

&lt;p&gt;However, &lt;code&gt;StackProps&lt;/code&gt; has a property called &lt;code&gt;stackName&lt;/code&gt;. By explicitly specifying this (setting it to the previous stack name), you can prevent the &lt;code&gt;${stageName}-&lt;/code&gt; prefix from being added to the stack name, allowing you to maintain the existing stack without creating a new one.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Before migration&lt;/span&gt;
&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;StackA&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;DevStackA&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt; &lt;span class="c1"&gt;// Stack name is `DevStackA`&lt;/span&gt;
&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;StackB&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;DevStackB&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;StackC&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;DevStackC&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="c1"&gt;// After migration&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyStage&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;cdk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Stage&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;scope&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;cdk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nx"&gt;cdk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;StageProps&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;scope&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// Without specifying stackName, the stack name becomes `Dev-StackA`, triggering new stack creation&lt;/span&gt;
    &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;StackA&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;StackA&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;stackName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;StackA`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;StackB&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;StackB&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;stackName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;StackB`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;StackC&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;StackC&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;stackName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;StackC`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;devStage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;MyStage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Dev&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In most cases, migrating to Stage does not affect existing resources.&lt;/p&gt;

&lt;p&gt;However, it is important to note that &lt;strong&gt;destructive changes can occur with some resources&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For more details, please refer to the article "&lt;a href="https://dev.to/aws-heroes/aws-cdk-resources-and-causes-of-replacement-in-non-stage-to-stage-migration-hla"&gt;AWS CDK: Resources and Causes of Replacement in Non-Stage to Stage Migration&lt;/a&gt;."&lt;/p&gt;




&lt;h2&gt;
  
  
  2. Synthesize Once, Deploy Many
&lt;/h2&gt;

&lt;p&gt;This is a best practice related to CI/CD pipeline configuration.&lt;/p&gt;

&lt;p&gt;If you are experienced in application development, you may have heard the phrase "Build once, deploy many."&lt;/p&gt;

&lt;p&gt;In CDK, a similar concept is expressed as "Synthesize once, deploy many." This is the idea of &lt;strong&gt;synthesizing the CDK app only once and then deploying multiple times&lt;/strong&gt;. In other words, the key point is to &lt;strong&gt;synthesize the CDK app only once&lt;/strong&gt; within the CI/CD pipeline.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Risk of Running Synthesis Multiple Times
&lt;/h3&gt;

&lt;p&gt;In the previous section ("Why Static Stack Creation Is Recommended"), I mentioned that the &lt;code&gt;cdk deploy&lt;/code&gt; command internally executes a synthesis process equivalent to the &lt;code&gt;cdk synth&lt;/code&gt; command.&lt;/p&gt;

&lt;p&gt;When you run the &lt;code&gt;cdk deploy&lt;/code&gt; command separately for each environment such as dev / stg / prod, the &lt;code&gt;cdk synth&lt;/code&gt; command is also executed for each environment. This means that the synthesis process runs for each environment.&lt;/p&gt;

&lt;p&gt;When synthesis is executed multiple times, depending on the code, there is a &lt;strong&gt;risk that the first and second synthesis runs could produce different outputs&lt;/strong&gt;. Even if you believe you have written correct code, unexpected mistakes can occur due to implementation errors or inconsistent versions.&lt;/p&gt;

&lt;p&gt;Since synthesis for the development environment and synthesis for the production environment are executed at different times, unintended environmental differences may be introduced. Therefore, it is desirable to run synthesis only once.&lt;/p&gt;

&lt;p&gt;Beyond the risk, running the synthesis process twice is also redundant and inefficient. Especially in large-scale applications, synthesis can take a considerable amount of time, potentially leading to a significant increase in deployment time.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to Achieve This in a CI/CD Pipeline
&lt;/h3&gt;

&lt;p&gt;So how do you achieve "Synthesize once, deploy many" within a CI/CD pipeline?&lt;/p&gt;

&lt;p&gt;When you run the &lt;code&gt;cdk synth&lt;/code&gt; command, the CDK app code is synthesized, and a set of files called the cloud assembly, including CloudFormation templates, is output to a directory called &lt;code&gt;cdk.out&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;On the other hand, the &lt;code&gt;cdk deploy&lt;/code&gt; command has an &lt;code&gt;--app&lt;/code&gt; (&lt;code&gt;-a&lt;/code&gt;) option where you can specify the location of the synthesized cloud assembly. When specified, the &lt;strong&gt;synthesis process during the deploy command is skipped&lt;/strong&gt;, and the deployment is executed based on the specified cloud assembly.&lt;/p&gt;

&lt;p&gt;In other words, by &lt;strong&gt;synthesizing the CDK app only once in advance&lt;/strong&gt;, saving the cloud assembly for all environments as an artifact, and then &lt;strong&gt;referencing that cloud assembly&lt;/strong&gt; in subsequent deployment jobs for each environment, you can achieve "Synthesize once, deploy many."&lt;/p&gt;

&lt;p&gt;This is also possible precisely because of the "static stack creation" approach mentioned earlier. With the static stack creation approach, cloud assemblies for all environments such as dev / stg / prod are &lt;strong&gt;generated in a single synthesis&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Specifically, using GitHub Actions as an example, the configuration would look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;jobs&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;synth&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;runs-on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ubuntu-latest&lt;/span&gt;
    &lt;span class="na"&gt;steps&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="c1"&gt;# ... checkout, setup-node, npm ci ...&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;npx cdk synth&lt;/span&gt; &lt;span class="c1"&gt;# Without specifying a stack name, same as --all&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/upload-artifact@v4&lt;/span&gt;
        &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
          &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;cdk-out&lt;/span&gt;
          &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;cdk.out&lt;/span&gt;

  &lt;span class="na"&gt;deploy-dev&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;needs&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;synth&lt;/span&gt;
    &lt;span class="na"&gt;steps&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="c1"&gt;# ... checkout, setup-node, npm ci, download-artifact, configure-aws-credentials ...&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;npx cdk deploy --app cdk.out --require-approval never Dev/*&lt;/span&gt;

  &lt;span class="na"&gt;deploy-stg&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;needs&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;deploy-dev&lt;/span&gt;
    &lt;span class="na"&gt;steps&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="c1"&gt;# ...&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;npx cdk deploy --app cdk.out --require-approval never Stg/*&lt;/span&gt;

  &lt;span class="na"&gt;deploy-prod&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;needs&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;deploy-stg&lt;/span&gt;
    &lt;span class="na"&gt;steps&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="c1"&gt;# ...&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;npx cdk deploy --app cdk.out --require-approval never Prod/*&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For more details about cdk.out, please refer to the article "&lt;a href="https://dev.to/aws-heroes/why-is-cdkout-cloud-assembly-necessary-in-aws-cdk-n5f"&gt;Why is cdk.out (Cloud Assembly) Necessary in AWS CDK?&lt;/a&gt;."&lt;/p&gt;




&lt;h2&gt;
  
  
  3. Separate the Asset Build/Publish and Deploy Phases
&lt;/h2&gt;

&lt;p&gt;Next is about separating the asset build/publish phase from the deploy phase in AWS CDK.&lt;/p&gt;

&lt;p&gt;When you run the &lt;code&gt;cdk deploy&lt;/code&gt; command, a series of processes are executed: CDK app synthesis, asset build/publish, and CloudFormation deployment.&lt;/p&gt;

&lt;p&gt;Assets refer to things like Lambda function code defined in the CDK app and Docker images used by ECS. During &lt;code&gt;cdk deploy&lt;/code&gt;, after CDK app synthesis is complete, Docker images are built, and then the built images and Lambda function code files are uploaded (published) to S3 or ECR.&lt;/p&gt;

&lt;p&gt;As introduced in the previous section, using the &lt;code&gt;--app&lt;/code&gt; (&lt;code&gt;-a&lt;/code&gt;) option allows you to skip the synthesis process, but asset build/publish is still executed during deployment.&lt;/p&gt;

&lt;p&gt;Having asset build/publish and deployment executed in the same command is convenient. On the other hand, from the perspective of separating build and deploy (release) as described in the Twelve-Factor App's "&lt;a href="https://12factor.net/build-release-run" rel="noopener noreferrer"&gt;Build, release, run&lt;/a&gt;" principle, it is desirable to have the asset build/publish and deploy phases separated.&lt;/p&gt;

&lt;h3&gt;
  
  
  Benefits of Phase Separation
&lt;/h3&gt;

&lt;p&gt;When the asset build/publish and deploy phases are separated in AWS CDK, the deployment job's responsibility becomes simpler, allowing it to focus solely on executing CloudFormation. This provides the following benefits:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Deployments are not affected by temporary build failures, enabling safer execution&lt;/li&gt;
&lt;li&gt;When a deployment fails, you can skip the asset build and retry only the deployment&lt;/li&gt;
&lt;li&gt;Heavy processes like Docker builds are performed in a prior phase, reducing the time spent in the deploy phase itself&lt;/li&gt;
&lt;li&gt;It becomes possible to deploy using the AWS CLI with CloudFormation directly, without using the CDK CLI in the deploy phase (in this case, the deploy job no longer needs permissions to assume the asset publishing roles)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  The cdk publish-assets Command
&lt;/h3&gt;

&lt;p&gt;Previously in the CDK CLI, asset build/publish and deployment were tightly coupled.&lt;/p&gt;

&lt;p&gt;While it was possible to perform asset build/publish using the &lt;a href="https://github.com/aws/aws-cdk-cli/tree/main/packages/cdk-assets" rel="noopener noreferrer"&gt;cdk-assets&lt;/a&gt; library, this method was not widely adopted because it required a separate dependency from the CDK CLI and was not well documented in the CDK documentation.&lt;/p&gt;

&lt;p&gt;To address this, the &lt;code&gt;cdk publish-assets&lt;/code&gt; command was added to the CDK CLI, which &lt;strong&gt;performs synthesis through asset build/publish without deploying&lt;/strong&gt;. By using this command, you can separate the asset build/publish and deploy phases.&lt;/p&gt;

&lt;p&gt;NOTE: As of April 2026, the &lt;code&gt;cdk publish-assets&lt;/code&gt; command is still in an unstable version, so you need to run it with the &lt;code&gt;--unstable=publish-assets&lt;/code&gt; option.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Without specifying a stack name, same as --all&lt;/span&gt;
npx cdk publish-assets &lt;span class="nt"&gt;--unstable&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;publish-assets

&lt;span class="c"&gt;# You can also skip synthesis with the --app option&lt;/span&gt;
npx cdk publish-assets &lt;span class="nt"&gt;--unstable&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;publish-assets &lt;span class="nt"&gt;--app&lt;/span&gt; cdk.out
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Example of Phase Separation in a CI/CD Pipeline
&lt;/h3&gt;

&lt;p&gt;By inserting a job that runs the &lt;code&gt;cdk publish-assets&lt;/code&gt; command between the synth job and the deploy job as shown below, you can separate the asset build/publish and deploy phases.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;cdk publish-assets&lt;/code&gt; command can also skip synthesis by specifying the cloud assembly synthesized in the synth job via the &lt;code&gt;--app&lt;/code&gt; option. This allows you to achieve "Synthesize once, deploy many" while also separating the asset build/publish and deploy phases.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;  &lt;span class="na"&gt;synth&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;runs-on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ubuntu-latest&lt;/span&gt;
    &lt;span class="na"&gt;steps&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="c1"&gt;# ... checkout, setup-node, npm ci ...&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;npx cdk synth&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/upload-artifact@v4&lt;/span&gt;
        &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
          &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;cdk-out&lt;/span&gt;
          &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;cdk.out&lt;/span&gt;

  &lt;span class="na"&gt;publish-assets&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;needs&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;synth&lt;/span&gt;
    &lt;span class="na"&gt;runs-on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ubuntu-latest&lt;/span&gt;
    &lt;span class="na"&gt;steps&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="c1"&gt;# ... checkout, setup-node, npm ci, configure-aws-credentials ...&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/download-artifact@v4&lt;/span&gt;
        &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
          &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;cdk-out&lt;/span&gt;
          &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;cdk.out&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;npx cdk publish-assets --unstable=publish-assets --app cdk.out --all&lt;/span&gt;
      &lt;span class="c1"&gt;# You can also publish for specific stages (stacks)&lt;/span&gt;
      &lt;span class="c1"&gt;# Since these run within the same job, Docker layer caching takes effect and builds after the first one are essentially skipped&lt;/span&gt;
      &lt;span class="c1"&gt;# If AWS environments are separated, switch to a role with access to each account&lt;/span&gt;
      &lt;span class="c1"&gt;# npx cdk publish-assets --unstable=publish-assets --app cdk.out Dev/*&lt;/span&gt;
      &lt;span class="c1"&gt;# npx cdk publish-assets --unstable=publish-assets --app cdk.out Stg/*&lt;/span&gt;
      &lt;span class="c1"&gt;# npx cdk publish-assets --unstable=publish-assets --app cdk.out Prod/*&lt;/span&gt;

  &lt;span class="na"&gt;deploy-dev&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;needs&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;publish-assets&lt;/span&gt;
    &lt;span class="na"&gt;steps&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="c1"&gt;# ... checkout, setup-node, npm ci, download-artifact, configure-aws-credentials ...&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;npx cdk deploy --app cdk.out --require-approval never Dev/*&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Reference
&lt;/h3&gt;

&lt;p&gt;I actually contributed the &lt;code&gt;cdk publish-assets&lt;/code&gt; command to AWS CDK! (See the &lt;a href="https://github.com/aws/aws-cdk-cli/pull/1020" rel="noopener noreferrer"&gt;Pull Request&lt;/a&gt;.)&lt;/p&gt;

&lt;p&gt;The separation of the asset build/publish phase is also discussed in a blog post &lt;a href="https://www.endoflineblog.com/cdk-tips-07-cicd-for-cdk-applications" rel="noopener noreferrer"&gt;CDK tips, part 7 – CI/CD for CDK applications&lt;/a&gt; by a former CDK maintainer. Please check it out as well.&lt;/p&gt;

&lt;p&gt;As described in that blog post, CDK Pipelines also creates a &lt;code&gt;publish-assets&lt;/code&gt; stage before the deploy stage.&lt;/p&gt;




&lt;h2&gt;
  
  
  4. Commit cdk.context.json
&lt;/h2&gt;

&lt;p&gt;The last topic is about committing cdk.context.json.&lt;/p&gt;

&lt;h3&gt;
  
  
  What Is cdk.context.json?
&lt;/h3&gt;

&lt;p&gt;cdk.context.json is a file that caches values retrieved from an AWS account during synthesis.&lt;/p&gt;

&lt;p&gt;For example, it dynamically retrieves and stores information such as Availability Zone details and currently available Amazon Machine Image (AMI) IDs for EC2 instances from the AWS account.&lt;/p&gt;

&lt;p&gt;Specifically, when you execute methods called &lt;a href="https://docs.aws.amazon.com/cdk/v2/guide/context.html#context_methods" rel="noopener noreferrer"&gt;context methods&lt;/a&gt;, such as &lt;code&gt;fromLookup()&lt;/code&gt;, provided by CDK, the method &lt;strong&gt;internally retrieves information from the AWS account via the AWS SDK&lt;/strong&gt; and stores the result in the cdk.context.json file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;vpc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Vpc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fromLookup&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Vpc&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;vpcId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;parameter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;StringParameter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;valueFromLookup&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;parameterName&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, during synthesis or deployment, if the information already exists in the cache (cdk.context.json), the &lt;strong&gt;process to retrieve information from the AWS account via the SDK does not run&lt;/strong&gt;, and the information from the file is used instead.&lt;/p&gt;

&lt;h3&gt;
  
  
  Benefits of Committing cdk.context.json
&lt;/h3&gt;

&lt;p&gt;There are two main benefits of committing cdk.context.json:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Avoiding non-deterministic behavior&lt;/li&gt;
&lt;li&gt;Improving deployment speed&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Avoiding Non-Deterministic Behavior
&lt;/h3&gt;

&lt;p&gt;For example, suppose you are deploying with a context method that retrieves the latest AMI for EC2.&lt;/p&gt;

&lt;p&gt;If a new AMI version is released at some point, since the CDK implementation retrieves the latest image, the retrieved AMI value may differ from the one used by the already deployed EC2 instance without you realizing it, triggering an EC2 replacement (rebuild).&lt;/p&gt;

&lt;p&gt;To avoid such "non-deterministic" behavior where the configuration changes depending on when the deployment is executed, you can cache the AMI information from the time of deployment in the cdk.context.json file. On subsequent deployments, &lt;strong&gt;the cached information is referenced to use the same value every time&lt;/strong&gt;, ensuring "deterministic" behavior.&lt;/p&gt;

&lt;h3&gt;
  
  
  Improving Deployment Speed
&lt;/h3&gt;

&lt;p&gt;In CDK apps that use context methods, if the relevant information is not found in cdk.context.json during synthesis, a dummy value is stored first and the synthesis completes.&lt;/p&gt;

&lt;p&gt;After that, the CDK CLI retrieves the information from the AWS account via the AWS SDK and updates cdk.context.json.&lt;/p&gt;

&lt;p&gt;Then the synthesis process runs again, and this time the final cloud assembly is generated based on the values stored in cdk.context.json.&lt;/p&gt;

&lt;p&gt;In other words, if cdk.context.json is not committed and the file does not exist, &lt;strong&gt;synthesis runs twice&lt;/strong&gt;, which wastes time.&lt;/p&gt;

&lt;p&gt;Additionally, since &lt;strong&gt;AWS SDK communication runs&lt;/strong&gt; every time synthesis or deployment is executed, even more time is wasted.&lt;/p&gt;

&lt;p&gt;By committing cdk.context.json, &lt;strong&gt;synthesis only needs to run once&lt;/strong&gt; and &lt;strong&gt;no AWS SDK communication is triggered&lt;/strong&gt;, resulting in improved deployment speed.&lt;/p&gt;

&lt;h3&gt;
  
  
  More Details on cdk.context.json
&lt;/h3&gt;

&lt;p&gt;For more details about cdk.context.json, please refer to the article "&lt;a href="https://dev.to/aws-heroes/the-necessity-of-cdkcontextjson-in-aws-cdk-3jp1"&gt;The Necessity of 'cdk.context.json' in AWS CDK&lt;/a&gt;."&lt;/p&gt;




&lt;h2&gt;
  
  
  Official Documentation
&lt;/h2&gt;

&lt;p&gt;The &lt;a href="https://docs.aws.amazon.com/cdk/v2/guide/best-practices.html" rel="noopener noreferrer"&gt;AWS CDK official documentation&lt;/a&gt; also covers best practices. Please check it out as well.&lt;/p&gt;




&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;In this article, I introduced the following four approaches as best practices for AWS CDK deployment:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Use Static Stack Creation + Stage&lt;/li&gt;
&lt;li&gt;Synthesize once, deploy many&lt;/li&gt;
&lt;li&gt;Separate the asset build/publish and deploy phases&lt;/li&gt;
&lt;li&gt;Commit cdk.context.json&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;These approaches may not apply to every project. However, I hope they serve as a reference to help you choose the approach that best fits your project.&lt;/p&gt;

</description>
      <category>aws</category>
      <category>cdk</category>
      <category>awscdk</category>
    </item>
    <item>
      <title>AWS CDK Tips: How to Centrally Apply Configurations to Multiple Resources</title>
      <dc:creator>Kenta Goto</dc:creator>
      <pubDate>Mon, 23 Mar 2026 09:36:01 +0000</pubDate>
      <link>https://dev.to/aws-heroes/aws-cdk-tips-how-to-centrally-apply-configurations-to-multiple-resources-i3g</link>
      <guid>https://dev.to/aws-heroes/aws-cdk-tips-how-to-centrally-apply-configurations-to-multiple-resources-i3g</guid>
      <description>&lt;p&gt;In this article, I will introduce a collection of tips for &lt;strong&gt;centrally and collectively applying configurations&lt;/strong&gt; to the properties of multiple resources in AWS CDK.&lt;/p&gt;

&lt;p&gt;The tips for bulk property application covered in this article use the following four features:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Aspects&lt;/li&gt;
&lt;li&gt;PropertyInjectors&lt;/li&gt;
&lt;li&gt;Mixins&lt;/li&gt;
&lt;li&gt;RemovalPolicies&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  1. Aspects
&lt;/h2&gt;

&lt;p&gt;The first feature I will introduce is &lt;a href="https://docs.aws.amazon.com/cdk/v2/guide/aspects.html" rel="noopener noreferrer"&gt;Aspects&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Aspects is a feature that allows you to apply the same operation to all resources within any given scope, such as a stack or construct.&lt;/p&gt;

&lt;h3&gt;
  
  
  1-1. How to Use Aspects
&lt;/h3&gt;

&lt;p&gt;You can apply properties to multiple resources at once by defining a class that implements the &lt;code&gt;IAspect&lt;/code&gt; interface and writing property override logic for specific resource types in the &lt;code&gt;visit&lt;/code&gt; method, as shown below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Aspects are implemented by implementing the IAspect interface&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BucketVersioningUpdater&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;cdk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;IAspect&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;visit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;IConstruct&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// The visit method is called for every resource within the construct passed to Aspects,&lt;/span&gt;
    &lt;span class="c1"&gt;// so we add a condition to only execute for CfnBucket resources&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nx"&gt;CfnBucket&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;// If bucket versioning is disabled, override it to enabled&lt;/span&gt;
      &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;versioningConfiguration&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt;
        &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;cdk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Tokenization&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isResolvable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;versioningConfiguration&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;
          &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;versioningConfiguration&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;status&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Enabled&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addPropertyOverride&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;VersioningConfiguration&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="na"&gt;Status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Enabled&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;});&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;cdk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;App&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;stack&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;CdkSampleStack&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;CdkSampleStack&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Apply BucketVersioningUpdater to all resources in CdkSampleStack&lt;/span&gt;
&lt;span class="nx"&gt;cdk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Aspects&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;of&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;BucketVersioningUpdater&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  1-2. Reference Article for Aspects
&lt;/h3&gt;

&lt;p&gt;Aspects can be used not only for overriding properties but also for property validation.&lt;/p&gt;

&lt;p&gt;For details on how to validate properties with Aspects, please refer to my article &lt;a href="https://dev.to/aws-heroes/how-to-choose-validation-approaches-in-aws-cdk-2ji3"&gt;How to Choose Validation Approaches in AWS CDK&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  1-3. Adjusting the Execution Order of Multiple Aspects
&lt;/h3&gt;

&lt;p&gt;When applying multiple Aspects to the same scope, you can adjust the execution order of each Aspect by specifying a &lt;code&gt;priority&lt;/code&gt;. This property was introduced in AWS CDK &lt;a href="https://github.com/aws/aws-cdk/releases/tag/v2.172.0" rel="noopener noreferrer"&gt;v2.172.0&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;You specify a numeric value for &lt;code&gt;priority&lt;/code&gt;, where a smaller number means higher priority and earlier execution. There is also a class called &lt;code&gt;AspectPriority&lt;/code&gt; that can be used like an enum, offering static variables such as &lt;code&gt;MUTATING&lt;/code&gt; (=200), &lt;code&gt;DEFAULT&lt;/code&gt; (=500), and &lt;code&gt;READONLY&lt;/code&gt; (=1000). However, if you assign the same &lt;code&gt;priority&lt;/code&gt; value to multiple Aspects, their execution order is not guaranteed, so it is recommended to assign different values to each Aspect.&lt;/p&gt;

&lt;p&gt;For example, suppose you apply multiple Aspects to a single stack as shown below. This is an example where property overrides are performed first and property validation is performed last. In this case, the Aspects are executed in the order: &lt;code&gt;AspectForOverride&lt;/code&gt; -&amp;gt; &lt;code&gt;AspectForOther1&lt;/code&gt; -&amp;gt; &lt;code&gt;AspectForOther2&lt;/code&gt; -&amp;gt; &lt;code&gt;AspectForValidation&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;cdk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;App&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;stack&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;CdkSampleStack&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;CdkSampleStack&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// priority = 1000&lt;/span&gt;
&lt;span class="nx"&gt;cdk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Aspects&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;of&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;AspectForValidation&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;priority&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;cdk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;AspectPriority&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;READONLY&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="c1"&gt;// priority = 200&lt;/span&gt;
&lt;span class="nx"&gt;cdk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Aspects&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;of&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;AspectForOverride&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;priority&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;cdk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;AspectPriority&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;MUTATING&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="c1"&gt;// priority = 500&lt;/span&gt;
&lt;span class="nx"&gt;cdk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Aspects&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;of&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;AspectForOther1&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;priority&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;cdk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;AspectPriority&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;DEFAULT&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="c1"&gt;// priority = 600&lt;/span&gt;
&lt;span class="nx"&gt;cdk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Aspects&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;of&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;AspectForOther2&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;priority&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;600&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When using a library like &lt;a href="https://github.com/cdklabs/cdk-nag" rel="noopener noreferrer"&gt;cdk-nag&lt;/a&gt; that validates CDK stacks through Aspects, you can also set its priority to be lower (i.e., a larger &lt;code&gt;priority&lt;/code&gt; number) so that validation runs after the other Aspects have been executed.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. PropertyInjectors
&lt;/h2&gt;

&lt;p&gt;The next feature I will introduce is &lt;a href="https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.PropertyInjectors.html" rel="noopener noreferrer"&gt;PropertyInjectors&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This feature was introduced in May 2025 with AWS CDK &lt;a href="https://github.com/aws/aws-cdk/releases/tag/v2.196.0" rel="noopener noreferrer"&gt;v2.196.0&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;PropertyInjectors is a feature that &lt;strong&gt;centrally rewrites the props&lt;/strong&gt; of all resources of any given type of L2 Construct (including some L3 Constructs) within a given scope.&lt;/p&gt;

&lt;p&gt;For more details, please refer to &lt;a href="https://docs.aws.amazon.com/cdk/v2/guide/blueprints.html" rel="noopener noreferrer"&gt;Configure constructs with CDK Blueprints&lt;/a&gt; in the AWS CDK Developer Guide.&lt;/p&gt;

&lt;p&gt;NOTE: The Developer Guide mentioned above introduces this as an approach called "CDK Blueprints," but in this article, I will consistently use the name &lt;code&gt;PropertyInjectors&lt;/code&gt;, which is the actual CDK class name, just as I do with the other features (&lt;code&gt;Aspects&lt;/code&gt;, &lt;code&gt;RemovalPolicies&lt;/code&gt;).&lt;/p&gt;

&lt;h3&gt;
  
  
  2-1. How to Use PropertyInjectors
&lt;/h3&gt;

&lt;p&gt;For example, suppose you have a requirement to set &lt;code&gt;blockPublicAccess&lt;/code&gt; to &lt;code&gt;BLOCK_ALL&lt;/code&gt; for all Amazon S3 buckets in a stack. Here is an example of using PropertyInjectors to meet this requirement.&lt;/p&gt;

&lt;p&gt;First, create a &lt;code&gt;MyBucketPropsInjector&lt;/code&gt; class that implements &lt;code&gt;IPropertyInjector&lt;/code&gt;. Specify the &lt;code&gt;PROPERTY_INJECTION_ID&lt;/code&gt; exposed by the target L2 Construct in &lt;code&gt;this.constructUniqueId&lt;/code&gt; within the &lt;code&gt;constructor&lt;/code&gt;, and have the &lt;code&gt;inject&lt;/code&gt; method return a new props object with the desired properties overridden.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyBucketPropsInjector&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;IPropertyInjector&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;constructUniqueId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Specify the PROPERTY_INJECTION_ID exposed by the Bucket construct&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;constructUniqueId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Bucket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;PROPERTY_INJECTION_ID&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// Return new props with the desired properties overridden&lt;/span&gt;
  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;inject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;originalProps&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;BucketProps&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;_context&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;InjectionContext&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;BucketProps&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;blockPublicAccess&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;BlockPublicAccess&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;BLOCK_ALL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;originalProps&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, add &lt;code&gt;MyBucketPropsInjector&lt;/code&gt; to the desired scope as shown below. This allows you to collectively override &lt;code&gt;blockPublicAccess&lt;/code&gt; to &lt;code&gt;BLOCK_ALL&lt;/code&gt; for all buckets within the scope.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;App&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;// When applying to the App&lt;/span&gt;
&lt;span class="nx"&gt;PropertyInjectors&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;of&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;MyBucketPropsInjector&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;

&lt;span class="c1"&gt;// When applying to a specific stack&lt;/span&gt;
&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Stack&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;MyStack&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;propertyInjectors&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;MyBucketPropsInjector&lt;/span&gt;&lt;span class="p"&gt;()],&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2-2. Avoiding Infinite Loops
&lt;/h3&gt;

&lt;p&gt;Suppose you want to set &lt;code&gt;serverAccessLogsBucket&lt;/code&gt;, which is also of type &lt;code&gt;Bucket&lt;/code&gt;, in the props of a &lt;code&gt;Bucket&lt;/code&gt; using PropertyInjectors.&lt;/p&gt;

&lt;p&gt;PropertyInjectors are executed &lt;strong&gt;at the time the target Construct is created&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Therefore, if you implement this requirement using the approach introduced above, when a new &lt;code&gt;Bucket&lt;/code&gt; is created for &lt;code&gt;serverAccessLogsBucket&lt;/code&gt;, PropertyInjectors will be triggered again. Then, the &lt;code&gt;serverAccessLogsBucket&lt;/code&gt; creation process will also be executed within that &lt;code&gt;Bucket&lt;/code&gt;, &lt;strong&gt;causing an infinite loop that results in an error.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To avoid this, you can implement a skip mechanism like the one shown below to prevent the infinite loop.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SpecialBucketInjector&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;IPropertyInjector&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;constructUniqueId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="c1"&gt;// Flag to determine whether to skip&lt;/span&gt;
  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;_skip&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_skip&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;constructUniqueId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Bucket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;PROPERTY_INJECTION_ID&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;inject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;originalProps&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;BucketProps&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;InjectionContext&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;BucketProps&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// If the skip flag is true, return the original props as-is&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_skip&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;originalProps&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;accessLogBucket&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;originalProps&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;serverAccessLogsBucket&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;accessLogBucket&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;// Since PropertyInjectors are executed when a new Construct is created,&lt;/span&gt;
      &lt;span class="c1"&gt;// set the skip flag to true here&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_skip&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

      &lt;span class="c1"&gt;// Because the skip flag is true, property overriding will not occur for this instance,&lt;/span&gt;
      &lt;span class="c1"&gt;// meaning `serverAccessLogsBucket` will not be overridden for this Bucket&lt;/span&gt;
      &lt;span class="nx"&gt;accessLogBucket&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Bucket&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;scope&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;my-access-log&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;blockPublicAccess&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;BlockPublicAccess&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;BLOCK_ALL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;});&lt;/span&gt;

      &lt;span class="c1"&gt;// Reset the skip flag to false&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_skip&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;serverAccessLogsBucket&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;accessLogBucket&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;originalProps&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2-3. Differences Between Aspects and PropertyInjectors
&lt;/h3&gt;

&lt;p&gt;Both Aspects and PropertyInjectors can apply properties collectively to multiple resources within any given scope, but there are several differences:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Difference in what they target&lt;/li&gt;
&lt;li&gt;Difference in when they are applied&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;First, regarding the difference in what they target: while Aspects overrides the configuration properties of L1 Constructs, PropertyInjectors &lt;strong&gt;rewrites the props of L2 or L3 Constructs themselves&lt;/strong&gt;. This is the key distinction.&lt;/p&gt;

&lt;p&gt;Next, regarding the difference in when they are applied: Aspects are executed during the second phase of the CDK application lifecycle, the "Prepare phase."&lt;/p&gt;

&lt;p&gt;Most Constructs are primarily created during the first phase, the "Construct phase." In other words, Aspects are executed by traversing the construct tree &lt;strong&gt;after most of the CDK application's Constructs have been created&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;On the other hand, since PropertyInjectors directly rewrite the props of L2 or L3 Constructs, the value overrides are applied &lt;strong&gt;at the time the Construct is created (during the first "Construct phase" of the lifecycle)&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwmgmmpy0iexj3henrcki.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwmgmmpy0iexj3henrcki.png" alt=" " width="800" height="80"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  2-4. When to Use Aspects vs PropertyInjectors
&lt;/h3&gt;

&lt;p&gt;Here are some use cases to consider when choosing between Aspects and PropertyInjectors:&lt;/p&gt;

&lt;p&gt;Cases where Aspects are more suitable:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When the target resources use L1 Constructs&lt;/li&gt;
&lt;li&gt;When you want to make overrides at the granularity of AWS CloudFormation properties&lt;/li&gt;
&lt;li&gt;When you want to centrally inspect resource properties (without overriding them)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Cases where PropertyInjectors are more suitable:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When you want to make more abstract overrides at the granularity of L2 or L3 Construct props, rather than at the L1 Construct (CloudFormation) property level&lt;/li&gt;
&lt;li&gt;When you want to override properties to prevent errors caused by built-in validation within L2 Constructs (since Aspects are executed after Construct creation is complete, only PropertyInjectors can prevent errors within L2 Constructs)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  2-5. Notes on PropertyInjectors Application Timing
&lt;/h3&gt;

&lt;p&gt;As mentioned earlier, PropertyInjectors are executed at the time the target Construct is created.&lt;/p&gt;

&lt;p&gt;If you apply PropertyInjectors using &lt;code&gt;PropertyInjectors.of().add&lt;/code&gt; after the target Stack has already been created, as shown below, the override process will not be executed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;App&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;CdkSampleStack&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;CdkSampleStack&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Applying PropertyInjectors here will not work because CdkSampleStack has already been created&lt;/span&gt;
&lt;span class="nx"&gt;PropertyInjectors&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;of&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;MyBucketPropsInjector&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead, call it before the Stack is created, or specify it in the &lt;code&gt;propertyInjectors&lt;/code&gt; option of the App or Stack props, as shown below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;App&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;// Apply PropertyInjectors before the stack is created&lt;/span&gt;
&lt;span class="nx"&gt;PropertyInjectors&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;of&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;MyBucketPropsInjector&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;CdkSampleStack&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;CdkSampleStack&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Or specify it in the stack props&lt;/span&gt;
&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;CdkSampleStack&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;CdkSampleStack&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;propertyInjectors&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;MyBucketPropsInjector&lt;/span&gt;&lt;span class="p"&gt;()],&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  3. Mixins
&lt;/h2&gt;

&lt;p&gt;The third feature I will introduce is &lt;a href="https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib-readme.html#mixins" rel="noopener noreferrer"&gt;Mixins&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Mixins was introduced as a developer preview feature in AWS CDK v2.229.0. In March 2026, with CDK v2.241.0, the core Mixins functionality was moved to the stable package (aws-cdk-lib) and became Generally Available (GA).&lt;/p&gt;

&lt;p&gt;Mixins is a mechanism for adding functionality to constructs through composable abstractions. It enables you to selectively apply capabilities that were traditionally bundled into L2 Constructs to L1 Constructs, or conversely, to partially apply L1 properties to L2 Constructs.&lt;/p&gt;

&lt;p&gt;For more details, please check out my article &lt;a href="https://dev.to/aws-heroes/aws-cdk-introduces-mixins-a-major-feature-for-flexible-construct-composition-developer-preview-583d"&gt;AWS CDK Introduces Mixins: A Major Feature for Flexible Construct Composition (Developer Preview)&lt;/a&gt; (NOTE: This article was written when Mixins was still in Developer Preview).&lt;/p&gt;

&lt;h3&gt;
  
  
  3-1. How to Use Mixins
&lt;/h3&gt;

&lt;p&gt;Mixins are applied using the &lt;code&gt;Mixins.of()&lt;/code&gt; method or the &lt;code&gt;with()&lt;/code&gt; method of each construct.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Stack&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Mixins&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;aws-cdk-lib&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;CfnBucket&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;aws-cdk-lib/aws-s3&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;BucketVersioning&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;BucketAutoDeleteObjects&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;aws-cdk-lib/aws-s3/mixins&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;App&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;stack&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Stack&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;CdkSampleStack&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;bucket&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;CfnBucket&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;MyBucketApply&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;Mixins&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;of&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bucket&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Mixins&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;BucketVersioning&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="c1"&gt;// Apply BucketVersioning to bucket&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;BucketAutoDeleteObjects&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// Apply BucketAutoDeleteObjects to bucket&lt;/span&gt;

&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;CfnBucket&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;MyBucketWith&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;with&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;BucketVersioning&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="c1"&gt;// Apply BucketVersioning with Construct's with method&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;with&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;BucketAutoDeleteObjects&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// Apply BucketAutoDeleteObjects with Construct's with method&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also apply Mixins to all resources under a specific scope, similar to &lt;code&gt;Aspects&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Stack&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Mixins&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;ConstructSelector&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;aws-cdk-lib&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;CfnBucket&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;aws-cdk-lib/aws-s3&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;BucketVersioning&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;aws-cdk-lib/aws-s3/mixins&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Construct&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;constructs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;App&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;stack&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Stack&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;CdkSampleStack&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;construct&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Construct&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;MyConstruct&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Apply to all resources under construct&lt;/span&gt;
&lt;span class="nx"&gt;Mixins&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;of&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;construct&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;BucketVersioning&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;

&lt;span class="c1"&gt;// Apply only to specific resource types under construct&lt;/span&gt;
&lt;span class="nx"&gt;Mixins&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;of&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;construct&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;ConstructSelector&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resourcesOfType&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;CfnBucket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;CFN_RESOURCE_TYPE_NAME&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;// Limited to CfnBucket&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;BucketVersioning&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3-2. Creating Custom Mixins
&lt;/h3&gt;

&lt;p&gt;You can easily create custom Mixins by extending the &lt;code&gt;Mixin&lt;/code&gt; abstract class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CustomVersioningMixin&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Mixin&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;supports&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;construct&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;construct&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nx"&gt;s3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;CfnBucket&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;applyTo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bucket&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;bucket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;versioningConfiguration&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Enabled&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;bucket&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;s3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;CfnBucket&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;MyBucket&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;Mixins&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;of&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bucket&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;CustomVersioningMixin&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3-3. Differences Between Mixins and Aspects
&lt;/h3&gt;

&lt;p&gt;Mixins are similar to Aspects in that they modify properties of resources in a specific scope, but they differ in &lt;strong&gt;timing of application&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;As explained in the Aspects vs PropertyInjectors comparison (section 2-3), Aspects are executed later in the CDK application lifecycle (in the Prepare phase), after all Constructs have been created. On the other hand, like PropertyInjectors, Mixins are executed immediately &lt;strong&gt;when they are called&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Aspects&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;construct&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Construct&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;scope&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;MyConstruct&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Bucket&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;construct&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;MyBucket1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Executed after Constructs below this are created&lt;/span&gt;
&lt;span class="nx"&gt;Aspects&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;of&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;construct&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;CustomVersioningAspect&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;

&lt;span class="c1"&gt;// Aspects are also applied to MyBucket2&lt;/span&gt;
&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Bucket&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;construct&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;MyBucket2&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Mixins&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;construct&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Construct&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;scope&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;MyConstruct&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Bucket&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;construct&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;MyBucket1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Executed at this point&lt;/span&gt;
&lt;span class="nx"&gt;Mixins&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;of&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;construct&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;CustomVersioningMixin&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;

&lt;span class="c1"&gt;// Mixins are not applied to MyBucket2&lt;/span&gt;
&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Bucket&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;construct&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;MyBucket2&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;According to the Mixins &lt;a href="https://github.com/aws/aws-cdk-rfcs/pull/824" rel="noopener noreferrer"&gt;RFC&lt;/a&gt;, &lt;strong&gt;it is recommended to use Mixins to make changes and to use Aspects to validate behaviors&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Additionally, a mechanism to convert between Aspects and Mixins is also provided. You can use &lt;code&gt;Shims.asMixin&lt;/code&gt; to apply an existing Aspect immediately as a Mixin, or use &lt;code&gt;Shims.asAspect&lt;/code&gt; to delay the application of a Mixin to a later phase during synthesis like an Aspect.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Mixins&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Shims&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Aspects&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;aws-cdk-lib&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Applies the aspect immediately&lt;/span&gt;
&lt;span class="nx"&gt;Mixins&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;of&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;scope&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Shims&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;asMixin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;SomeAspect&lt;/span&gt;&lt;span class="p"&gt;()));&lt;/span&gt;

&lt;span class="c1"&gt;// Delays application of the Mixin to a later phase during synthesis&lt;/span&gt;
&lt;span class="nx"&gt;Aspects&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;of&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;scope&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Shims&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;asAspect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;SomeMixin&lt;/span&gt;&lt;span class="p"&gt;()));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3-4. Differences Between Mixins and PropertyInjectors
&lt;/h3&gt;

&lt;p&gt;While PropertyInjectors is also similar to Mixins, &lt;strong&gt;it is recommended to use Mixins when you want to apply abstractions, and PropertyInjectors when you want to change default behaviors (i.e., when you want to use it as a blueprint)&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;There are also the following differences:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Mixins

&lt;ul&gt;
&lt;li&gt;Target: L1 Constructs + L2 Constructs&lt;/li&gt;
&lt;li&gt;Often used with narrow scope (specific Constructs, etc.)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;PropertyInjectors

&lt;ul&gt;
&lt;li&gt;Target: L2 Constructs (including some L3 Constructs)&lt;/li&gt;
&lt;li&gt;Often used with broad scope (App level, etc.)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;




&lt;h2&gt;
  
  
  4. RemovalPolicies
&lt;/h2&gt;

&lt;p&gt;The fourth feature I will introduce is &lt;a href="https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.RemovalPolicies.html" rel="noopener noreferrer"&gt;RemovalPolicies&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This feature was introduced in AWS CDK &lt;a href="https://github.com/aws/aws-cdk/releases/tag/v2.183.0" rel="noopener noreferrer"&gt;v2.183.0&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;With RemovalPolicies, you can &lt;strong&gt;apply a RemovalPolicy to all resources within any given scope at once.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  4-1. How to Use RemovalPolicies
&lt;/h3&gt;

&lt;p&gt;For example, if you want to centrally set the RemovalPolicy for all resources, you can write it as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;App&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;// Set the RemovalPolicy to DESTROY for all resources&lt;/span&gt;
&lt;span class="nx"&gt;RemovalPolicies&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;of&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;destroy&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;// Set the RemovalPolicy to RETAIN for all resources&lt;/span&gt;
&lt;span class="nx"&gt;RemovalPolicies&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;of&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;retain&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  4-2. MissingRemovalPolicies
&lt;/h3&gt;

&lt;p&gt;Some resources may already have a RemovalPolicy set internally by L2 Constructs, or you may have explicitly set a RemovalPolicy yourself. If you want to apply a RemovalPolicy collectively to &lt;strong&gt;all resources except those that already have one&lt;/strong&gt;, you can use &lt;code&gt;MissingRemovalPolicies&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;App&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;// Set the RemovalPolicy to DESTROY for resources that do not yet have one&lt;/span&gt;
&lt;span class="nx"&gt;MissingRemovalPolicies&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;of&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;destroy&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this article, I introduced a collection of tips for &lt;strong&gt;centrally and collectively applying configurations&lt;/strong&gt; to the properties of multiple resources in AWS CDK using the following features:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Aspects&lt;/li&gt;
&lt;li&gt;PropertyInjectors&lt;/li&gt;
&lt;li&gt;Mixins&lt;/li&gt;
&lt;li&gt;RemovalPolicies&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;By leveraging these features, you can efficiently manage the properties of multiple resources and improve the readability and maintainability of your code.&lt;/p&gt;

</description>
      <category>aws</category>
      <category>cdk</category>
      <category>awscdk</category>
    </item>
    <item>
      <title>Zero Orphaned Resources: Force Deleting Any CloudFormation Stack</title>
      <dc:creator>Kenta Goto</dc:creator>
      <pubDate>Mon, 16 Mar 2026 08:59:30 +0000</pubDate>
      <link>https://dev.to/aws-heroes/zero-orphaned-resources-force-deleting-any-cloudformation-stack-3b1h</link>
      <guid>https://dev.to/aws-heroes/zero-orphaned-resources-force-deleting-any-cloudformation-stack-3b1h</guid>
      <description>&lt;h2&gt;
  
  
  DELETE_FAILED and FORCE_DELETE_STACK
&lt;/h2&gt;

&lt;p&gt;If you've ever worked with AWS CloudFormation, chances are you've encountered &lt;code&gt;DELETE_FAILED&lt;/code&gt;. A non-empty S3 bucket, an ECR repository with images still in it, a Custom Resource that doesn't return SUCCESS... You end up manually emptying resources and retrying the deletion over and over. It's painful.&lt;/p&gt;

&lt;p&gt;"What about &lt;code&gt;FORCE_DELETE_STACK&lt;/code&gt;?" you might think. CloudFormation does provide this feature. However, it merely &lt;strong&gt;detaches failed resources from the stack&lt;/strong&gt;. The resources themselves remain in your AWS account. In other words, it creates orphaned resources, and you still have to delete them manually.&lt;/p&gt;




&lt;h2&gt;
  
  
  When Stack Deletion Gets Complicated
&lt;/h2&gt;

&lt;p&gt;CloudFormation stack deletion can get complicated beyond just DELETE_FAILED. To begin with, when you want to delete multiple stacks at once, CloudFormation offers no built-in way to do so. On top of that, the following problems come into play.&lt;/p&gt;

&lt;h3&gt;
  
  
  Inter-Stack Dependencies
&lt;/h3&gt;

&lt;p&gt;When deleting multiple stacks, if there are dependencies via Exports/Imports, getting the order wrong causes an error. The more stacks you have, the less realistic it becomes to manually figure out the correct deletion order.&lt;/p&gt;

&lt;h3&gt;
  
  
  Slow Deletion Due to VPC Lambda
&lt;/h3&gt;

&lt;p&gt;Stacks containing Lambda functions connected to a VPC have to wait for ENI (Elastic Network Interface) cleanup during deletion. This is extremely slow, sometimes taking tens of minutes for a single stack.&lt;/p&gt;

&lt;h3&gt;
  
  
  Resource Orphaning from Retain Policies
&lt;/h3&gt;

&lt;p&gt;DeletionPolicy settings like &lt;code&gt;Retain&lt;/code&gt; and &lt;code&gt;RetainExceptOnCreate&lt;/code&gt; exist to protect important data. But there are times when you want to delete everything cleanly, such as tearing down a dev environment or recreating a stack. In those cases, the stack disappears but the resources remain, becoming orphans.&lt;/p&gt;

&lt;h3&gt;
  
  
  Deletion Protection
&lt;/h3&gt;

&lt;p&gt;Resource-level deletion protection on EC2 instances, RDS clusters, Cognito user pools, and stack-level TerminationProtection are important safeguards against accidental deletion in production. However, when you actually want to delete an unneeded stack, you have to figure out which resources have protection enabled and disable them one by one.&lt;/p&gt;




&lt;h2&gt;
  
  
  delstack Solves All of These
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/go-to-k/delstack" rel="noopener noreferrer"&gt;delstack&lt;/a&gt; is a CLI tool I built to solve all of the problems above. It works with stacks from any IaC tool that uses CloudFormation: AWS CDK, SAM, Amplify, Serverless Framework, and more.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgithub.com%2Fuser-attachments%2Fassets%2F4f02526d-536c-4a23-81fd-10484902133f" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgithub.com%2Fuser-attachments%2Fassets%2F4f02526d-536c-4a23-81fd-10484902133f" alt="delstack" width="954" height="394"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Force Deletion of DELETE_FAILED Resources: Zero Orphaned Resources
&lt;/h3&gt;

&lt;p&gt;It automatically cleans up resources that cause normal deletion to fail, then deletes them. Unlike FORCE_DELETE_STACK, it doesn't leave resources behind. Nested child stacks are processed recursively as well.&lt;/p&gt;

&lt;p&gt;It supports over 10 resource types including S3 buckets, S3 Directory/Table/Vector buckets, ECR repositories, Backup Vaults, Athena WorkGroups, IAM groups, nested stacks, and Custom Resources. See the &lt;a href="https://github.com/go-to-k/delstack#resource-types-that-can-be-forced-to-delete" rel="noopener noreferrer"&gt;README&lt;/a&gt; for the full list of supported resource types.&lt;/p&gt;

&lt;p&gt;Resources not in this list are deleted normally without any issues, so delstack isn't just for DELETE_FAILED stacks. You can use it for everyday stack deletion as well.&lt;/p&gt;

&lt;h3&gt;
  
  
  Automatic Dependency Resolution with Parallel Deletion
&lt;/h3&gt;

&lt;p&gt;When multiple stacks are specified, it automatically analyzes dependencies through CloudFormation Exports/Imports and determines the correct order using reverse topological sort. Independent stacks are deleted in parallel for maximum throughput, and rather than simple step-based batching, it uses dynamic scheduling: as soon as a stack's deletion completes, any stacks that depended on it are immediately started, keeping parallelism as high as possible at all times.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Example: Stacks A, B, C, D, E, F (C-&amp;gt;A, D-&amp;gt;A, E-&amp;gt;B, F-&amp;gt;C,D,E)

Step 1: Delete F (no stacks depend on it)
Step 2: Delete C, D, E in parallel (after F completes)
Step 3: Delete B (after E completes)
Step 4: Delete A (after both C and D complete)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  VPC Lambda Pre-Optimization
&lt;/h3&gt;

&lt;p&gt;Before deletion begins, it automatically detaches VPC configurations from Lambda functions and deletes their ENIs in parallel. This eliminates the ENI cleanup wait time entirely.&lt;/p&gt;

&lt;h3&gt;
  
  
  Retain Policy Override
&lt;/h3&gt;

&lt;p&gt;With the &lt;code&gt;-f&lt;/code&gt; option, resources with Retain/RetainExceptOnCreate policies are force deleted. Resources are reliably removed along with the stack.&lt;/p&gt;

&lt;h3&gt;
  
  
  Automatic Deletion Protection Removal
&lt;/h3&gt;

&lt;p&gt;With the &lt;code&gt;-f&lt;/code&gt; option, resource-level deletion protection on EC2, RDS, Cognito, CloudWatch Logs, ALB, and more, as well as stack TerminationProtection, are automatically detected and disabled before deletion proceeds. Without &lt;code&gt;-f&lt;/code&gt;, protected resources are reported and the process is aborted before stack deletion begins, so it's safe by default.&lt;/p&gt;

&lt;h3&gt;
  
  
  CDK Integration
&lt;/h3&gt;

&lt;p&gt;With the &lt;code&gt;delstack cdk&lt;/code&gt; subcommand, you can run it in a CDK app directory to synthesize, discover all stacks (including cross-region), and delete them with dependency resolution.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgithub.com%2Fuser-attachments%2Fassets%2F0222969c-8c19-4700-80ce-096654ffba74" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgithub.com%2Fuser-attachments%2Fassets%2F0222969c-8c19-4700-80ce-096654ffba74" alt="delstack for CDK" width="644" height="346"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  How to Use
&lt;/h2&gt;

&lt;p&gt;Install with Homebrew in one line.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;brew &lt;span class="nb"&gt;install &lt;/span&gt;go-to-k/tap/delstack
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For Linux/Windows, you can use the install script.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl &lt;span class="nt"&gt;-fsSL&lt;/span&gt; https://raw.githubusercontent.com/go-to-k/delstack/main/install.sh | sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For other installation methods, see the &lt;a href="https://github.com/go-to-k/delstack#install" rel="noopener noreferrer"&gt;README&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;With interactive mode (&lt;code&gt;-i&lt;/code&gt;), just search and select your stacks. Add &lt;code&gt;-f&lt;/code&gt; to enable force deletion of resources with Retain policies or deletion protection, and stacks with TerminationProtection.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;delstack &lt;span class="nt"&gt;-i&lt;/span&gt; &lt;span class="nt"&gt;-f&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Filter a keyword of stack names(case-insensitive): goto

? Select StackNames.
Nested child stacks and XXX_IN_PROGRESS(e.g. ROLLBACK_IN_PROGRESS) status stacks are not displayed.
(* = TerminationProtection)

 [Use arrows to move, space to select, &amp;lt;right&amp;gt; to all, &amp;lt;left&amp;gt; to none, type to filter]
  [ ]  dev-GOTO-03-TestStack
&amp;gt; [x]  dev-Goto-02-TestStack
  [ ] * dev-goto-01-TestStack
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also specify stack names directly instead of using interactive mode.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;delstack &lt;span class="nt"&gt;-s&lt;/span&gt; stack1 &lt;span class="nt"&gt;-s&lt;/span&gt; stack2 &lt;span class="nt"&gt;-s&lt;/span&gt; stack3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;GitHub Actions is supported too, making it easy to integrate stack cleanup into your CI/CD pipeline.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Delete stack&lt;/span&gt;
  &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;go-to-k/delstack@main&lt;/span&gt;
  &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;stack-name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;YourStack1, YourStack2&lt;/span&gt;
    &lt;span class="na"&gt;force&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
    &lt;span class="na"&gt;region&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;us-east-1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For CDK apps, set &lt;code&gt;cdk: true&lt;/code&gt; to run &lt;code&gt;cdk synth&lt;/code&gt; and delete all discovered stacks. You can also pass CDK context values and specify an existing &lt;code&gt;cdk.out&lt;/code&gt; directory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Delete CDK stacks&lt;/span&gt;
  &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;go-to-k/delstack@main&lt;/span&gt;
  &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;cdk&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
    &lt;span class="c1"&gt;# cdk-app: ./cdk.out           # Use existing cdk.out (skip synthesis)&lt;/span&gt;
    &lt;span class="c1"&gt;# cdk-context: env=dev,foo=bar  # CDK context values (comma separated)&lt;/span&gt;
    &lt;span class="na"&gt;force&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;delstack solves every problem around CloudFormation stack deletion with a single command.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Force deletion with zero orphaned resources&lt;/li&gt;
&lt;li&gt;Automatic parallel deletion with dependency resolution&lt;/li&gt;
&lt;li&gt;Faster deletion through VPC Lambda optimization&lt;/li&gt;
&lt;li&gt;Automatic handling of deletion protection and Retain policies&lt;/li&gt;
&lt;li&gt;CDK integration with cross-region support&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For detailed options and the full list of supported resources, see the &lt;a href="https://github.com/go-to-k/delstack" rel="noopener noreferrer"&gt;README&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Issues and Stars are welcome: &lt;a href="https://github.com/go-to-k/delstack" rel="noopener noreferrer"&gt;github.com/go-to-k/delstack&lt;/a&gt;&lt;/p&gt;

</description>
      <category>aws</category>
      <category>cloudformation</category>
      <category>cdk</category>
      <category>awscdk</category>
    </item>
    <item>
      <title>Why is cdk.out (Cloud Assembly) Necessary in AWS CDK?</title>
      <dc:creator>Kenta Goto</dc:creator>
      <pubDate>Thu, 26 Feb 2026 11:45:47 +0000</pubDate>
      <link>https://dev.to/aws-heroes/why-is-cdkout-cloud-assembly-necessary-in-aws-cdk-n5f</link>
      <guid>https://dev.to/aws-heroes/why-is-cdkout-cloud-assembly-necessary-in-aws-cdk-n5f</guid>
      <description>&lt;h2&gt;
  
  
  What is cdk.out
&lt;/h2&gt;

&lt;p&gt;cdk.out is a directory that stores a collection of files called the cloud assembly.&lt;/p&gt;

&lt;p&gt;When you run the cdk synth command or the cdk deploy command, the cloud assembly is generated from your CDK code and stored in the cdk.out directory.&lt;/p&gt;




&lt;h2&gt;
  
  
  What is the Cloud Assembly
&lt;/h2&gt;

&lt;p&gt;The cloud assembly is a collection of files generated by the synthesis of a CDK application (CDK code). The CDK CLI references this information to deploy resources to the AWS environment.&lt;/p&gt;

&lt;p&gt;In other words, the cloud assembly can be considered an intermediate artifact that CDK uses to deploy infrastructure definitions written in CDK code to the AWS environment.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhboec4t5zpjsc8b8ysqc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhboec4t5zpjsc8b8ysqc.png" alt="CDK deployment flow" width="520" height="306"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Components of the Cloud Assembly
&lt;/h2&gt;

&lt;p&gt;The cloud assembly primarily consists of the following files and directories:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;(stack name).template.json

&lt;ul&gt;
&lt;li&gt;CloudFormation template generated by CDK code&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;(stack name).assets.json

&lt;ul&gt;
&lt;li&gt;File describing information about assets used in CDK code&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;(stack name).metadata.json

&lt;ul&gt;
&lt;li&gt;File describing metadata used in CDK code&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;manifest.json

&lt;ul&gt;
&lt;li&gt;File describing metadata of the cloud assembly&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;tree.json

&lt;ul&gt;
&lt;li&gt;File representing the Construct tree structure of resources defined in CDK code&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;cdk.out

&lt;ul&gt;
&lt;li&gt;File storing Cloud Assembly schema version information&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;asset.(hash value)/

&lt;ul&gt;
&lt;li&gt;Directory storing asset files used in CDK code&lt;/li&gt;
&lt;li&gt;Uses hash values calculated for each &lt;a href="https://docs.aws.amazon.com/cdk/v2/guide/assets.html#assets-types" rel="noopener noreferrer"&gt;asset&lt;/a&gt; as directory names&lt;/li&gt;
&lt;li&gt;Each asset is classified into two types: S3 assets and Docker image assets, which are uploaded to S3 buckets or ECR repositories created by the &lt;code&gt;cdk bootstrap&lt;/code&gt; command when the &lt;code&gt;cdk deploy&lt;/code&gt; command is executed&lt;/li&gt;
&lt;li&gt;Docker image assets are built when the &lt;code&gt;cdk deploy&lt;/code&gt; command is executed (not when the &lt;code&gt;cdk synth&lt;/code&gt; command is executed)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;assembly-(stage name)/

&lt;ul&gt;
&lt;li&gt;Directory storing cloud assemblies generated for each stage when using &lt;a href="https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.Stage.html" rel="noopener noreferrer"&gt;Stage Construct&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;




&lt;h2&gt;
  
  
  Why cdk.out is Necessary
&lt;/h2&gt;

&lt;p&gt;As explained above, cdk.out is a directory for storing the cloud assembly, which is an intermediate artifact generated from CDK code.&lt;/p&gt;

&lt;p&gt;So why is this directory necessary? You might wonder whether it's really necessary to generate and store information needed for deployment as files.&lt;/p&gt;

&lt;p&gt;Before explaining this, let me first explain that synthesis can be executed multiple times depending on how CDK commands are used, and the risks associated with this.&lt;/p&gt;

&lt;h3&gt;
  
  
  Risks of Running Synthesis Multiple Times
&lt;/h3&gt;

&lt;p&gt;The cdk deploy command internally executes synthesis processing equivalent to the synth command.&lt;/p&gt;

&lt;p&gt;For example, if you execute cdk deploy after executing cdk synth, synthesis is executed in both synth and deploy, meaning the CDK application is synthesized twice.&lt;/p&gt;

&lt;p&gt;In this case, there is a risk that the second synthesis execution will create output different from the first synthesis result. This could cause behavior different from what the developer intended, as a different result might be generated in the next synthesis process even though the developer confirmed and approved the first synthesis result.&lt;/p&gt;

&lt;p&gt;On the other hand, even when executing cdk deploy without executing cdk synth, if you manage multiple environments such as development environment (dev) and production environment (prod) with one CDK application, synthesis will be executed every time you deploy to each environment. There is also a risk that unintended environment differences may be introduced because synthesis in the development environment and synthesis in the production environment are executed at different times.&lt;/p&gt;

&lt;p&gt;Apart from risks, running the synthesis process twice is redundant and inefficient. Especially for large applications, synthesis can take considerable time, which can lead to a significant increase in deployment time.&lt;/p&gt;

&lt;h3&gt;
  
  
  Reusing cdk.out with the &lt;code&gt;--app&lt;/code&gt; Option
&lt;/h3&gt;

&lt;p&gt;I explained the risks of running synthesis multiple times, so how can we avoid these risks?&lt;/p&gt;

&lt;p&gt;The cdk deploy command has the &lt;code&gt;--app&lt;/code&gt; (&lt;code&gt;-a&lt;/code&gt;) option to avoid this.&lt;/p&gt;

&lt;p&gt;This option allows you to specify the path to the cdk.out directory. By specifying this, you can deploy using an already synthesized cloud assembly and skip the synthesis that is normally executed during the deploy command. If you know that no code has been changed since the last cdk synth, you can avoid redundant synthesis processing by reusing the cloud assembly with this option.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx cdk deploy &lt;span class="nt"&gt;--app&lt;/span&gt; ./cdk.out
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also explicitly separate synthesis and deployment execution by specifying a command to directly synthesize CDK code. This command is typically the same as the command defined in the &lt;code&gt;app&lt;/code&gt; property of the &lt;code&gt;cdk.json&lt;/code&gt; file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx cdk deploy &lt;span class="nt"&gt;--app&lt;/span&gt; &lt;span class="s2"&gt;"npx ts-node --prefer-ts-exts bin/cdk-sample.ts"&lt;/span&gt;

&lt;span class="c"&gt;# For environments where TypeScript can be executed with Node.js&lt;/span&gt;
npx cdk deploy &lt;span class="nt"&gt;--app&lt;/span&gt; &lt;span class="s2"&gt;"node bin/cdk-sample.ts"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this option, you can specify cdk.out as the synthesis result and deploy using the same cloud assembly in multiple environments such as development and production. In other words, you can reuse cdk.out with the &lt;code&gt;--app&lt;/code&gt; option. This avoids the risks of running synthesis multiple times and also avoids redundant synthesis processing.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx cdk synth

npx cdk deploy &lt;span class="nt"&gt;-a&lt;/span&gt; ./cdk.out DevStack
npx cdk deploy &lt;span class="nt"&gt;-a&lt;/span&gt; ./cdk.out ProdStack
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Separating Synthesis and Deployment
&lt;/h3&gt;

&lt;p&gt;I explained that reusing cdk.out with the &lt;code&gt;--app&lt;/code&gt; option can avoid the risks of running synthesis multiple times. This is precisely because cdk.out enables the separation of synthesis and deployment.&lt;/p&gt;

&lt;p&gt;In this way, being able to separate synthesis and deployment can be said to be the necessity of cdk.out in AWS CDK.&lt;/p&gt;

&lt;p&gt;Additionally, if a mechanism supports the cloud assembly, you can deploy using the cloud assembly from applications other than the CDK CLI. For example, tools like &lt;a href="https://github.com/aws/aws-cdk-cli/tree/main/packages/cdk-assets" rel="noopener noreferrer"&gt;cdk-assets&lt;/a&gt; also reference the cloud assembly to manage assets. In this way, separating synthesis and deployment, such as performing synthesis with CDK and deployment with tools other than the CDK CLI, enables flexible workflows in CDK application development.&lt;/p&gt;

&lt;h3&gt;
  
  
  synthesize once, deploy many
&lt;/h3&gt;

&lt;p&gt;In application development such as containers, there is an approach called "build once, deploy many." This is the idea of building an application only once and deploying that build artifact to multiple environments.&lt;/p&gt;

&lt;p&gt;The approach of reusing cdk.out with the &lt;code&gt;--app&lt;/code&gt; option in CDK is exactly the same idea. It can be called "synthesize once, deploy many," where synthesis is performed only once and deployment can be done multiple times afterward.&lt;/p&gt;




&lt;h2&gt;
  
  
  Other Benefits and Use Cases of cdk.out
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Asset Caching
&lt;/h3&gt;

&lt;p&gt;cdk.out includes asset directories that store asset files used in CDK code. This includes code files used by Lambda and ECS, Dockerfiles, and other files.&lt;/p&gt;

&lt;p&gt;As a CDK mechanism, if an asset with the same hash value as the asset used in the CDK code to be synthesized already exists during synthesis, the bundling process for that asset is skipped and the existing asset is reused. In other words, cdk.out also functions as a cache for assets.&lt;/p&gt;

&lt;h3&gt;
  
  
  Utilizing cdk.out in CI/CD Pipelines
&lt;/h3&gt;

&lt;p&gt;I mentioned that cdk.out allows you to deploy using the same cloud assembly in multiple environments. Specifically, you can utilize cdk.out in CI/CD pipelines such as GitHub Actions as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;jobs&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;synthesize&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;runs-on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ubuntu-latest&lt;/span&gt;
    &lt;span class="na"&gt;steps&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;npm ci&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;npx cdk synth&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/upload-artifact@v6&lt;/span&gt;
        &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
          &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;cdk-out&lt;/span&gt;
          &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;cdk.out&lt;/span&gt;

  &lt;span class="na"&gt;deploy-dev&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;needs&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;synthesize&lt;/span&gt;
    &lt;span class="na"&gt;steps&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/download-artifact@v6&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;npx cdk deploy --app cdk.out DevStack&lt;/span&gt;

  &lt;span class="na"&gt;deploy-prod&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;needs&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;deploy-dev&lt;/span&gt;
    &lt;span class="na"&gt;steps&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/download-artifact@v6&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;npx cdk deploy --app cdk.out ProdStack&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Reference Materials
&lt;/h3&gt;

&lt;p&gt;In the explanations so far, I mentioned that multiple deployments are possible based on a single synthesis, but did you know that there are actually two types of CDK stack creation methods: static stack creation and dynamic stack creation?&lt;/p&gt;

&lt;p&gt;The approach of performing synthesis only once introduced this time is possible with the static stack creation approach. On the other hand, dynamic stack creation also has other benefits, so if you are interested in the differences between them, please check out the article &lt;a href="https://dev.to/aws-heroes/cdk-environment-management-static-vs-dynamic-stack-creation-383l"&gt;"CDK Environment Management: Static vs Dynamic Stack Creation"&lt;/a&gt;. This is an article I co-authored with Thorsten Höger, an AWS Hero from Germany.&lt;/p&gt;

&lt;p&gt;Also, &lt;a href="https://www.endoflineblog.com/cdk-tips-07-cicd-for-cdk-applications" rel="noopener noreferrer"&gt;an article by a former CDK maintainer&lt;/a&gt; is helpful, so please take a look.&lt;/p&gt;




&lt;h2&gt;
  
  
  cdk.out Bloat and How to Deal with It
&lt;/h2&gt;

&lt;h3&gt;
  
  
  cdk.out Bloat
&lt;/h3&gt;

&lt;p&gt;We learned that cdk.out plays an important role in CDK, but by repeating synthesis, new files and directories accumulate, and it can reach several GB before you know it.&lt;/p&gt;

&lt;p&gt;This is because a new asset directory is created when application files are updated. Especially for container applications using Docker, the size tends to be particularly large.&lt;/p&gt;

&lt;p&gt;Additionally, since the cdk deploy command also builds Docker images, not only cdk.out but also built images stored in local Docker will continue to increase.&lt;/p&gt;

&lt;h3&gt;
  
  
  cdk-agc
&lt;/h3&gt;

&lt;p&gt;To optimize such bloated cdk.out, I created a tool called &lt;code&gt;cdk-agc&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;(It's very useful, so please star it on &lt;a href="https://github.com/go-to-k/cdk-agc" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt; if you like!)&lt;/p&gt;

&lt;p&gt;Specifically, it does the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Deletes &lt;strong&gt;old asset files and directories in cdk.out&lt;/strong&gt; that are not used by the current stack&lt;/li&gt;
&lt;li&gt;Automatically deletes &lt;strong&gt;locally built Docker images&lt;/strong&gt; if the deleted assets are Docker assets&lt;/li&gt;
&lt;li&gt;Deletes &lt;strong&gt;temporary CDK directories&lt;/strong&gt; accumulated in &lt;code&gt;$TMPDIR&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No installation required&lt;/strong&gt; - just run the command with &lt;code&gt;npx&lt;/code&gt; and it executes immediately&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This optimizes cdk.out, removes accumulated Docker images, and saves local disk space. Furthermore, if you cache &lt;code&gt;cdk.out&lt;/code&gt; in CI/CD, it can &lt;strong&gt;significantly reduce cache size&lt;/strong&gt;, improving the efficiency of your CI/CD pipeline.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;No installation required&lt;/strong&gt; - you can run it immediately by adding &lt;code&gt;npx&lt;/code&gt; to the command, so please feel free to try it.&lt;/p&gt;

&lt;p&gt;You can use &lt;code&gt;-d&lt;/code&gt; for dry run, &lt;code&gt;-k&lt;/code&gt; to specify retention period, and &lt;code&gt;-t&lt;/code&gt; to clean up temporary directories in &lt;code&gt;$TMPDIR&lt;/code&gt;, so please use them according to your needs.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Navigate to the CDK project with cdk.out you want to optimize&lt;/span&gt;
&lt;span class="nb"&gt;cd &lt;/span&gt;my-cdk-project

&lt;span class="c"&gt;# Clean up cdk.out (also deletes built Docker asset images)&lt;/span&gt;
npx cdk-agc

&lt;span class="c"&gt;# Check files to be deleted with dry run feature&lt;/span&gt;
npx cdk-agc &lt;span class="nt"&gt;-d&lt;/span&gt;

&lt;span class="c"&gt;# Keep files modified within 24 hours&lt;/span&gt;
npx cdk-agc &lt;span class="nt"&gt;-k&lt;/span&gt; 24

&lt;span class="c"&gt;# Clean up temporary directories in $TMPDIR&lt;/span&gt;
npx cdk-agc &lt;span class="nt"&gt;-t&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;cdk.out is often overlooked, but it plays an important role in CDK application development. By understanding and properly utilizing cdk.out, you can develop CDK applications more efficiently.&lt;/p&gt;

</description>
      <category>aws</category>
      <category>cdk</category>
      <category>awscdk</category>
    </item>
    <item>
      <title>AWS CDK Hotswap Deployments Now Support Bedrock AgentCore Runtime</title>
      <dc:creator>Kenta Goto</dc:creator>
      <pubDate>Thu, 29 Jan 2026 12:56:45 +0000</pubDate>
      <link>https://dev.to/aws-heroes/aws-cdk-hotswap-deployments-now-support-bedrock-agentcore-runtime-42c7</link>
      <guid>https://dev.to/aws-heroes/aws-cdk-hotswap-deployments-now-support-bedrock-agentcore-runtime-42c7</guid>
      <description>&lt;p&gt;AWS CDK's hotswap deployments now support Bedrock AgentCore Runtime.&lt;/p&gt;




&lt;h2&gt;
  
  
  What are hotswap deployments?
&lt;/h2&gt;

&lt;p&gt;Hotswap deployments refer to deployments using the &lt;code&gt;--hotswap&lt;/code&gt; option with the &lt;code&gt;cdk deploy&lt;/code&gt; command. Instead of updating the CloudFormation stack, this feature &lt;strong&gt;directly updates the changed resources using the AWS SDK&lt;/strong&gt; internally in CDK, &lt;strong&gt;significantly reducing deployment time&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx cdk deploy &lt;span class="nt"&gt;--hotswap&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;AWS CDK generates CloudFormation templates from CDK code by executing an internal process called "synthesis". Then, it deploys CloudFormation stacks based on those templates.&lt;/p&gt;

&lt;p&gt;However, even when only application-side files such as Lambda function code or container images are changed, CDK still requires CloudFormation stack deployment, which takes time.&lt;/p&gt;

&lt;p&gt;Since application code changes more frequently than infrastructure definitions, the wait time caused by CloudFormation deployment for each code change can degrade the development experience.&lt;/p&gt;

&lt;p&gt;To address such cases, CDK provides the hotswap deployment feature. When using hotswap deployments, changes to application-side files such as Lambda function code or container images can be directly updated to resources without performing CloudFormation deployment processing. This significantly reduces deployment time and accelerates the development cycle.&lt;/p&gt;

&lt;p&gt;However, this hotswap feature &lt;strong&gt;only supports specific changes to certain resources&lt;/strong&gt;, such as Lambda function code assets and ECS image assets. For details on supported resources, please refer to the official documentation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://docs.aws.amazon.com/cdk/v2/guide/ref-cli-cmd-deploy.html" rel="noopener noreferrer"&gt;Developer Guide: cdk deploy&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  How hotswap deployments work
&lt;/h2&gt;

&lt;p&gt;In hotswap deployment, synthesis processing is first executed to generate a CloudFormation template based on the implemented CDK code.&lt;/p&gt;

&lt;p&gt;Next, it obtains the differences between that template and the template of the actually deployed stack.&lt;/p&gt;

&lt;p&gt;If the properties of the resources with differences are hotswap-compatible, it retrieves the current configuration values using AWS SDK Get APIs. Then, combining the information from the newly generated template with the information obtained from the AWS SDK, it finally updates the differences using AWS SDK Update APIs.&lt;/p&gt;




&lt;h2&gt;
  
  
  Hotswap support for Bedrock AgentCore Runtime
&lt;/h2&gt;

&lt;p&gt;AWS CDK CLI v2.1102.0 added hotswap deployment support for Bedrock AgentCore Runtime.&lt;/p&gt;

&lt;p&gt;Actually, I contributed this feature to the AWS CDK.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/aws/aws-cdk-cli/pull/991" rel="noopener noreferrer"&gt;feat(cli): support hotswap for AWS::BedrockAgentCore::Runtime&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This was one of the most challenging CDK contributions I've ever done. I particularly struggled with aspects unrelated to CDK's features and implementation, such as dependencies and integration tests.&lt;/p&gt;

&lt;p&gt;By the way, CDK is divided into the Construct library repository and the CLI repository, and this feature is implemented on the CLI repository side.&lt;/p&gt;




&lt;h2&gt;
  
  
  How to use hotswap deployments with Bedrock AgentCore Runtime
&lt;/h2&gt;

&lt;p&gt;Bedrock AgentCore Runtime supports container images (ECR) or code files (S3) as runtime sources. As of January 2026, S3 only supports Python.&lt;/p&gt;

&lt;p&gt;The hotswap deployment introduced this time &lt;strong&gt;supports both ECR and S3 updates.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Version
&lt;/h3&gt;

&lt;p&gt;Please update the &lt;code&gt;aws-cdk&lt;/code&gt; version to v2.1102.0 or later.&lt;/p&gt;

&lt;p&gt;NOTE: &lt;code&gt;aws-cdk&lt;/code&gt; is the CDK CLI, and &lt;code&gt;aws-cdk-lib&lt;/code&gt; is the Construct library. The hotswap feature this time is a CLI-side feature.&lt;/p&gt;

&lt;p&gt;NOTE: Make sure &lt;code&gt;aws-cdk-lib&lt;/code&gt; and &lt;code&gt;@aws-cdk/aws-bedrock-agentcore-alpha&lt;/code&gt; are also installed.&lt;/p&gt;

&lt;p&gt;Example package.json:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"devDependencies"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="err"&gt;//&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;...&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="err"&gt;//&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;...&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"aws-cdk"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"2.1102.0"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="nl"&gt;"dependencies"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"@aws-cdk/aws-bedrock-agentcore-alpha"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^2.235.0-alpha.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"aws-cdk-lib"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"2.235.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  ECR source implementation example
&lt;/h3&gt;

&lt;p&gt;First, here's an implementation example using container images.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;AgentRuntimeArtifact.fromAsset&lt;/code&gt; method builds the local Dockerfile, automatically pushes it to a CDK-managed ECR, and allows you to specify it as the Runtime source.&lt;/p&gt;

&lt;p&gt;NOTE: You need to prepare application code and &lt;code&gt;Dockerfile&lt;/code&gt; in the ArtifactDirectory directory in advance, but this is omitted in this article.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;runtimeArtifact&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;AgentRuntimeArtifact&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fromAsset&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;__dirname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ArtifactDirectory&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;runtimeECR&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Runtime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;RuntimeECR&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;runtimeName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;runtime_ecr&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;description&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Runtime&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;agentRuntimeArtifact&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;runtimeArtifact&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;environmentVariables&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;LOG_LEVEL&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;INFO&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  S3 source implementation example
&lt;/h3&gt;

&lt;p&gt;Next, here's an implementation example using code files.&lt;/p&gt;

&lt;p&gt;First, upload local code files to S3 using the &lt;code&gt;Asset&lt;/code&gt; construct from the &lt;code&gt;aws-cdk-lib/aws-s3-assets&lt;/code&gt; module.&lt;/p&gt;

&lt;p&gt;Then, you can specify that S3 object as the Runtime source using the &lt;code&gt;AgentRuntimeArtifact.fromS3&lt;/code&gt; method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;asset&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Asset&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;CodeAsset&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;__dirname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ArtifactDirectory&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;runtimeArtifact&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;AgentRuntimeArtifact&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fromS3&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;bucketName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;asset&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;s3BucketName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;objectKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;asset&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;s3ObjectKey&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="nx"&gt;AgentCoreRuntime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;PYTHON_3_13&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;app.py&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;runtimeS3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Runtime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;RuntimeS3&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;runtimeName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;runtime_s3&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;agentRuntimeArtifact&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;runtimeArtifact&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;description&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Runtime&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;environmentVariables&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;LOG_LEVEL&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;INFO&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Executing hotswap deployment
&lt;/h3&gt;

&lt;p&gt;After completing the CDK implementation as described above, first deploy without the &lt;code&gt;--hotswap&lt;/code&gt; option.&lt;/p&gt;

&lt;p&gt;NOTE: This example shows the case using S3 source CDK code, but the same applies to ECR source.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx cdk deploy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, make changes only to the application code, not the CDK code.&lt;/p&gt;

&lt;p&gt;Then, before performing hotswap deployment, try running the &lt;code&gt;cdk diff&lt;/code&gt; command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx cdk diff
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You should see differences in the &lt;code&gt;AgentRuntimeArtifact&lt;/code&gt; of the Runtime resource, as shown below. Since &lt;code&gt;AgentRuntimeArtifact&lt;/code&gt; is a hotswap-supported property, you can execute hotswap deployment.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[~] AWS::BedrockAgentCore::Runtime RuntimeS3 RuntimeS39E2E9695
 └─ [~] AgentRuntimeArtifact
     └─ [~] .CodeConfiguration:
         └─ [~] .Code:
             └─ [~] .S3:
                 └─ [~] .Prefix:
                     ├─ [-] a18dede8bbfbfc4db1d3a434052cab2fe8ea82c01a945a1d66127d63b5523299.zip
                     └─ [+] f2793197e0247f4f89ebcf16787e78399b4de6c255ff8e57d7d1766ac38d4113.zip
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, execute deployment with the &lt;code&gt;--hotswap&lt;/code&gt; option.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx cdk deploy &lt;span class="nt"&gt;--hotswap&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If a message like &lt;code&gt;hotswapped!&lt;/code&gt; is displayed as shown below, the hotswap deployment has succeeded. The deployment time should be significantly reduced compared to when not using hotswap deployment.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(  Synthesis time: 3.45s

� The --hotswap and --hotswap-fallback flags deliberately introduce CloudFormation drift to speed up deployments
� They should only be used for development - never use them for your production Stacks!

CdkSampleStack: start: Building CdkSampleStack Template
CdkSampleStack: success: Built CdkSampleStack Template
CdkSampleStack: start: Publishing CdkSampleStack Template (current_account-us-east-1-2ad80d58)
CdkSampleStack: success: Published CdkSampleStack Template (current_account-us-east-1-2ad80d58)
CdkSampleStack: deploying... [1/1]

( hotswapping resources:
   ( AWS::BedrockAgentCore::Runtime 'runtime_s3-iImLoZB224'
   ( AWS::BedrockAgentCore::Runtime 'runtime_s3-iImLoZB224' hotswapped!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Bedrock AgentCore Runtime properties that support hotswap
&lt;/h2&gt;

&lt;p&gt;The following properties of Bedrock AgentCore Runtime support hotswap deployment:&lt;/p&gt;

&lt;p&gt;In other words, hotswap deployment is only executed when these properties are changed.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;agentRuntimeArtifact&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;description&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;environmentVariables&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If changes include other properties, only changes to the above properties are applied with hotswap deployment, and changes to other properties are ignored.&lt;/p&gt;

&lt;p&gt;Using the &lt;code&gt;--hotswap-fallback&lt;/code&gt; option allows you to fall back to a full CloudFormation deployment.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx cdk deploy &lt;span class="nt"&gt;--hotswap-fallback&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By the way, I aligned these three properties with Lambda's hotswap-supported properties.&lt;/p&gt;

&lt;p&gt;Internally, the target properties can be increased as much as desired, but targeting properties that don't change frequently doesn't provide much benefit, so I simply narrowed it down to the minimum necessary properties, just like Lambda.&lt;/p&gt;

&lt;p&gt;If there are properties you'd like to be targeted, please let me know and I'll consider changes.&lt;/p&gt;




&lt;h2&gt;
  
  
  Precautions for hotswap deployments
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Never use in production environments
&lt;/h3&gt;

&lt;p&gt;While this seemingly convenient hotswap deployment feature is useful, &lt;strong&gt;it must not be used in production environments.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is because hotswap deployment intentionally introduces drift (inconsistency between the template and actual resource state) to the CloudFormation stack, which can cause unexpected behavior when trying to update the CloudFormation stack later.&lt;/p&gt;

&lt;p&gt;Therefore, use hotswap deployments &lt;strong&gt;to improve development speed in development environments&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Be aware of tokens
&lt;/h3&gt;

&lt;p&gt;For example, when defining an S3 source using &lt;code&gt;BucketDeployment&lt;/code&gt; and &lt;code&gt;Source.asset()&lt;/code&gt; as shown below, hotswap doesn't work.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;bucket&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Bucket&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;CodeBucket&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Not recommended (hotswap doesn't work)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;deployment&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;aws_s3_deployment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;BucketDeployment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Deploy&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;sources&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;aws_s3_deployment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Source&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;asset&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;__dirname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;agent-code&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;))],&lt;/span&gt;
  &lt;span class="na"&gt;destinationBucket&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;bucket&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;extract&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;agentRuntimeArtifact&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;AgentRuntimeArtifact&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fromS3&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;bucketName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;bucket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;bucketName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;objectKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;cdk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Fn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;select&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;deployment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;objectKeys&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="c1"&gt;// Token, resolved at deployment time&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="nx"&gt;AgentCoreRuntime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;PYTHON_3_13&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;app.py&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;objectKey&lt;/code&gt; generated here is a token, which is information that is not resolved during synthesis but is resolved at deployment time. On the CloudFormation template, it is represented using functions like &lt;code&gt;Fn::GetAtt&lt;/code&gt; or &lt;code&gt;Ref&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In that case, even if you upload the changed code files, the property value specifying the S3 information of Runtime on the CloudFormation template remains a value using functions like &lt;code&gt;Fn::GetAtt&lt;/code&gt;, and no difference occurs, so hotswap doesn't work. Therefore, please use the &lt;code&gt;Asset&lt;/code&gt; construct as shown above.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"AgentRuntimeArtifact"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"CodeConfiguration"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"Code"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"S3"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"Bucket"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"Ref"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"CodeBucketFF4C7AD6"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"Prefix"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"Fn::Select"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"Fn::GetAtt"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="s2"&gt;"DeployCustomResource218AF6A4"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="s2"&gt;"SourceObjectKeys"&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If hotswap is not being executed, check whether it's a token.&lt;/p&gt;

&lt;p&gt;Specifically, look at the CloudFormation template generated by CDK and check if it's something like &lt;code&gt;Fn::GetAtt&lt;/code&gt; whose value doesn't change even when modified.&lt;/p&gt;




&lt;h2&gt;
  
  
  Making it more convenient
&lt;/h2&gt;

&lt;p&gt;When using S3 as a source, it's tedious to manually call the &lt;code&gt;Asset&lt;/code&gt; construct every time, right?&lt;/p&gt;

&lt;p&gt;Actually, I've submitted a pull request to AWS CDK to address this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/aws/aws-cdk/pull/36472" rel="noopener noreferrer"&gt;feat(bedrock-agentcore-alpha): add fromCodeAsset method to create runtime artifact with local code assets&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once this is merged, you'll be able to simply specify the directory path of code files using the &lt;code&gt;AgentRuntimeArtifact.fromCodeAsset&lt;/code&gt; method, and it will upload to S3 for you, as shown below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;runtimeArtifact&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;agentcore&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;AgentRuntimeArtifact&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fromCodeAsset&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;__dirname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ArtifactDirectory&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="na"&gt;runtime&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;agentcore&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;AgentCoreRuntime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;PYTHON_3_13&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;entrypoint&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;app.py&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;If there are any bugs, I'd appreciate it if you could let me know. I'll fix them immediately.&lt;/p&gt;

</description>
      <category>aws</category>
      <category>cdk</category>
      <category>agentcore</category>
      <category>bedrock</category>
    </item>
    <item>
      <title>AWS CDK IAM Role Management: Preventing Automatic Policy Updates</title>
      <dc:creator>Kenta Goto</dc:creator>
      <pubDate>Thu, 11 Dec 2025 11:14:15 +0000</pubDate>
      <link>https://dev.to/aws-heroes/aws-cdk-iam-role-management-preventing-automatic-policy-updates-ibk</link>
      <guid>https://dev.to/aws-heroes/aws-cdk-iam-role-management-preventing-automatic-policy-updates-ibk</guid>
      <description>&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Table of Contents&lt;/li&gt;
&lt;li&gt;Automatic IAM Role Generation in L2 Constructs&lt;/li&gt;
&lt;li&gt;withoutPolicyUpdates()&lt;/li&gt;
&lt;li&gt;mutable in fromRoleArn()&lt;/li&gt;
&lt;li&gt;Role.customizeRoles()&lt;/li&gt;
&lt;li&gt;Conclusion&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Automatic IAM Role Generation in L2 Constructs
&lt;/h2&gt;

&lt;p&gt;One of the major features of AWS CDK is that most L2 Constructs automatically generate IAM roles with the necessary permissions for the resources.&lt;/p&gt;

&lt;p&gt;On the other hand, most Constructs also allow you to pass a custom role you created yourself, so you can avoid automatic generation.&lt;/p&gt;

&lt;p&gt;However, even in that case, the L2 Construct often automatically grants the necessary permissions to the custom role you created and passed.&lt;/p&gt;




&lt;h2&gt;
  
  
  withoutPolicyUpdates()
&lt;/h2&gt;

&lt;p&gt;However, if you want to manage IAM roles yourself, you may also want to manage the policies yourself, so there may be cases where you want to avoid having permissions automatically granted to your custom role within the L2 Construct.&lt;/p&gt;

&lt;p&gt;In such cases, you can use the &lt;code&gt;withoutPolicyUpdates()&lt;/code&gt; method of the &lt;code&gt;Role&lt;/code&gt; Construct to avoid automatic permission grants to your custom role.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;role&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;iam&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Role&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ExecutionRole&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;assumedBy&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;iam&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ServicePrincipal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;bedrock-agentcore.amazonaws.com&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="c1"&gt;// Add the necessary policies yourself&lt;/span&gt;
&lt;span class="nx"&gt;role&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addToPrincipalPolicy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;statement&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;agentcore&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Runtime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;TestRuntime&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;runtimeName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;integ_test_runtime&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;agentRuntimeArtifact&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;runtimeArtifact&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;executionRole&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;role&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;withoutPolicyUpdates&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="c1"&gt;// Pass the return value of withoutPolicyUpdates(), not role&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  mutable in fromRoleArn()
&lt;/h2&gt;

&lt;p&gt;In CDK, you can pseudo-import a role created outside the stack into the CDK stack using static methods such as &lt;code&gt;fromRoleArn()&lt;/code&gt; of the &lt;code&gt;Role&lt;/code&gt; Construct.&lt;/p&gt;

&lt;p&gt;This allows you to use the convenient methods provided by the Construct in your CDK code even for manually created resources, making it a very useful feature.&lt;/p&gt;

&lt;p&gt;For IAM roles imported with the &lt;code&gt;fromRoleArn()&lt;/code&gt; method, you can also set &lt;code&gt;mutable: false&lt;/code&gt; to prevent policies from being automatically attached.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;importedRole&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;iam&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Role&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fromRoleArn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ImportedRole&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;my-role-arn&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;mutable&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;agentcore&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Runtime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;TestRuntime&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;runtimeName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;integ_test_runtime&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;agentRuntimeArtifact&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;runtimeArtifact&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;executionRole&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;importedRole&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Role.customizeRoles()
&lt;/h2&gt;

&lt;p&gt;Using the &lt;code&gt;Role.customizeRoles()&lt;/code&gt; method, you can output a report of what IAM roles and policies would be generated by L2 Constructs without actually creating them.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;cdk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;App&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;stack&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;cdk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Stack&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;LambdaStack&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;iam&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Role&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;customizeRoles&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;kms&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Key&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;BucketKey&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;bucket&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;s3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Bucket&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Bucket&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;encryptionKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;lambda&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Handler&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;runtime&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;lambda&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Runtime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;NODEJS_22_X&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;index.handler&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;code&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;lambda&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Code&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fromInline&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`
    exports.handler = async function(event, context) {
      console.log("Event: ", event);
      return;
    };
  `&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;bucket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;grantRead&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This generates two files: &lt;code&gt;iam-policy-report.txt&lt;/code&gt; and &lt;code&gt;iam-policy-report.json&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;missing role&amp;gt; (LambdaStack/Handler/ServiceRole)

AssumeRole Policy:
[
  {
    "Action": "sts:AssumeRole",
    "Effect": "Allow",
    "Principal": {
      "Service": "lambda.amazonaws.com"
    }
  }
]

Managed Policy ARNs:
[
  "arn:(PARTITION):iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
]

Managed Policies Statements:
NONE

Identity Policy Statements:
[
  {
    "Action": [
      "s3:GetObject*",
      "s3:GetBucket*",
      "s3:List*"
    ],
    "Effect": "Allow",
    "Resource": [
      "(LambdaStack/Bucket/Resource.Arn)",
      "(LambdaStack/Bucket/Resource.Arn)/*"
    ]
  },
  {
    "Action": [
      "kms:Decrypt",
      "kms:DescribeKey"
    ],
    "Effect": "Allow",
    "Resource": "(LambdaStack/BucketKey/Resource.Arn)"
  }
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"roles"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"roleConstructPath"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"LambdaStack/Handler/ServiceRole"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"roleName"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"missing role"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"missing"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"assumeRolePolicy"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
          &lt;/span&gt;&lt;span class="nl"&gt;"Action"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"sts:AssumeRole"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
          &lt;/span&gt;&lt;span class="nl"&gt;"Effect"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Allow"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
          &lt;/span&gt;&lt;span class="nl"&gt;"Principal"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"Service"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"lambda.amazonaws.com"&lt;/span&gt;&lt;span class="w"&gt;
          &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"managedPolicyArns"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"arn:(PARTITION):iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"managedPolicyStatements"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[],&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"identityPolicyStatements"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
          &lt;/span&gt;&lt;span class="nl"&gt;"Action"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"s3:GetObject*"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"s3:GetBucket*"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"s3:List*"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
          &lt;/span&gt;&lt;span class="nl"&gt;"Effect"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Allow"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
          &lt;/span&gt;&lt;span class="nl"&gt;"Resource"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"(LambdaStack/Bucket/Resource.Arn)"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"(LambdaStack/Bucket/Resource.Arn)/*"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
          &lt;/span&gt;&lt;span class="nl"&gt;"Action"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"kms:Decrypt"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"kms:DescribeKey"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
          &lt;/span&gt;&lt;span class="nl"&gt;"Effect"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Allow"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
          &lt;/span&gt;&lt;span class="nl"&gt;"Resource"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"(LambdaStack/BucketKey/Resource.Arn)"&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Based on this report, you can create IAM roles/policies yourself and specify them in the &lt;code&gt;usePrecreatedRoles&lt;/code&gt; option of &lt;code&gt;customizeRoles&lt;/code&gt; to safely switch to self-managing IAM roles.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;stack&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Stack&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;LambdaStack&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;iam&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Role&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;customizeRoles&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;usePrecreatedRoles&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;LambdaStack/Handler/ServiceRole&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;lambda-service-role&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;NOTE: The above examples are quoted from the &lt;a href="https://github.com/aws/aws-cdk/wiki/Security-And-Safety-Dev-Guide#using-the-customize-roles-feature-to-generate-a-report-and-supply-role-names" rel="noopener noreferrer"&gt;Security And Safety Dev Guide&lt;/a&gt; in the CDK repository's Wiki and the official documentation &lt;a href="https://docs.aws.amazon.com/cdk/v2/guide/define-iam-l2.html" rel="noopener noreferrer"&gt;Define permissions for L2 constructs with the AWS CDK&lt;/a&gt;, so please refer to them for more details.&lt;/p&gt;




&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;If you don't want to leave the generation/granting of IAM roles and permissions (policies) to automatic generation by L2 Constructs in CDK, it would be good to remember these methods.&lt;/p&gt;

</description>
      <category>aws</category>
      <category>awscdk</category>
      <category>cdk</category>
    </item>
  </channel>
</rss>
