How to Deploy AWS CDK to AWS Emulator
If you run AWS Emulator locally and point the endpoint settings at it, cdk bootstrap and cdk deploy from AWS CDK work without an AWS account. This is how to test deployment and deletion of infrastructure code (IaC) written in CDK locally or in CI before deploying it to real AWS. The article uses a TypeScript stack with an S3 bucket, an SQS queue, and a Lambda function. It shows the commands and output from running bootstrap, deploy, diff, and destroy.
The test environment was Ubuntu 24.04 (x86_64), Node.js 22.23.5, AWS Emulator 1.0.531 (released November 19, 2026), AWS CDK CLI 2.1149.0, aws-cdk-lib 2.277.0, and AWS CLI 2.41.2. I did not use an AWS account or credentials. I ran the CDK CLI in a shell where the HTTPS_PROXY environment variable was not set (according to the CDK CLI reference, if you don't specify --proxy, the proxy in this environment variable is used). I did not check how the same stack behaves when deployed to real AWS, including cost, IAM permission evaluation, or how long creation takes. The official limitations documentation also says that the emulator does not evaluate IAM policies the same way AWS does. I did not test Docker image assets (pushing to ECR), cdk watch and hotswap, or deploying to another account or region. As the official documentation describes, the account ID used by the emulator is twelve zeros. In the output, I wrote it as <12桁の0>.
- 1. Start the emulator and point the CDK CLI at it
- 2. The stack to deploy
- 3. Running deploy right after startup asks for bootstrap
- 4. Prepare for deployment with cdk bootstrap
- 5. Create the stack with cdk deploy
- 6. Check changes with cdk diff before applying them
- 7. Delete a non-empty bucket with cdk destroy
- 8. After restarting the emulator, bootstrap again
Start the emulator and point the CDK CLI at it
AWS Emulator accepts AWS API calls locally. According to the official documentation, it does not need an AWS account, and requests are not sent to AWS. The page on fidelity, which describes how closely the emulator matches AWS, says that support for IaC tools such as CloudFormation, Terraform, and AWS CDK varies by service.
I cover installation in a separate article, "How to Install AWS Emulator on Ubuntu and Get Started." Here I followed the Linux instructions and installed the Debian package after verifying its gpg signature. The -t off flag at startup turns off telemetry, which otherwise sends usage data to AWS.
aws-emulator -t off
I installed the CDK CLI with npm.
npm install -g aws-cdk@2.1149.0
The emulator keeps running in the terminal where you started it, so run the CDK commands in a different terminal. In that terminal, set the environment variables from the official documentation.
export AWS_ENDPOINT_URL=https://127.0.0.1:9297
export AWS_SHARED_CREDENTIALS_FILE=~/.aws/emulator/9297/emulator_credentials
export AWS_CONFIG_FILE=~/.aws/emulator/9297/config
export AWS_CA_BUNDLE=~/.aws/emulator/9297/ca.pem
export AWS_EC2_METADATA_DISABLED=true
export AWS_DISABLE_HOST_PREFIX_INJECTION=true
export AWS_REGION=us-east-1
export AWS_DEFAULT_REGION=us-east-1
unset AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN AWS_PROFILE
The CDK CLI runs on Node.js. The official documentation's table says that for JavaScript SDKs to trust the emulator's certificate, you need NODE_EXTRA_CA_CERTS. However, the CDK CLI reference says that if you don't specify --ca-bundle-path, it reads AWS_CA_BUNDLE. In practice, all the commands below succeeded with only the eight environment variables above, without setting NODE_EXTRA_CA_CERTS.
The official documentation warns that if AWS_ENDPOINT_URL is not set in the shell where you run a command, the command will use any credentials it finds and send requests to real AWS. On a machine with real credentials, run cdk bootstrap and cdk deploy only in a terminal where all of the environment variables above are set.
You can confirm that requests are reaching the emulator with the AWS CLI. As the official verification steps describe, if the ARN ends with user/emulator, the emulator is responding. Right after startup, there are no CloudFormation stacks.
$ aws sts get-caller-identity
{
"UserId": "<ユーザーID>",
"Account": "<12桁の0>",
"Arn": "arn:aws:iam::<12桁の0>:user/emulator"
}
$ aws cloudformation list-stacks
{
"StackSummaries": []
}
The stack to deploy
I created the app files with cdk init app --language typescript --generate-only, ran npm install, and then wrote the stack in lib/ with an S3 bucket, an SQS queue, and a Lambda function. I left the bin/ side as cdk init generated it (I did not specify env).
import * as cdk from 'aws-cdk-lib/core';
import * as s3 from 'aws-cdk-lib/aws-s3';
import * as sqs from 'aws-cdk-lib/aws-sqs';
import * as lambda from 'aws-cdk-lib/aws-lambda';
import { Construct } from 'constructs';
export class CdkappStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const bucket = new s3.Bucket(this, 'Uploads', {
removalPolicy: cdk.RemovalPolicy.DESTROY,
autoDeleteObjects: true,
});
const queue = new sqs.Queue(this, 'Jobs', {
visibilityTimeout: cdk.Duration.seconds(60),
});
const fn = new lambda.Function(this, 'Hello', {
runtime: lambda.Runtime.NODEJS_22_X,
handler: 'index.handler',
code: lambda.Code.fromAsset('lambda'),
environment: { QUEUE_URL: queue.queueUrl },
});
queue.grantSendMessages(fn);
new cdk.CfnOutput(this, 'BucketName', { value: bucket.bucketName });
new cdk.CfnOutput(this, 'QueueUrl', { value: queue.queueUrl });
new cdk.CfnOutput(this, 'FunctionName', { value: fn.functionName });
}
}
The Lambda code is in lambda/index.mjs. It returns the name it receives and the queue URL from the environment variable. When you pass a directory to Code.fromAsset, the CDK zips it and uploads it during deployment.
export const handler = async (event) => {
return { message: `hello, ${event.name ?? "world"}`, queueUrl: process.env.QUEUE_URL };
};
Running deploy right after startup asks for bootstrap
I ran cdk deploy on the freshly started emulator without any setup. The deployment stopped before it started.
$ cdk deploy --require-approval never
✨ Synthesis time: 1.97s
current credentials could not be used to assume 'arn:aws:iam::<12桁の0>:role/cdk-hnb659fds-file-publishing-role-<12桁の0>-us-east-1', but are for the right account. Proceeding anyway.
(同じ行が続くので略)
current credentials could not be used to assume 'arn:aws:iam::<12桁の0>:role/cdk-hnb659fds-deploy-role-<12桁の0>-us-east-1', but are for the right account. Proceeding anyway.
CdkappStack: SSM parameter /cdk-bootstrap/hnb659fds/version not found in aws://<12桁の0>/us-east-1. Has the environment been bootstrapped? Please run 'cdk bootstrap' (see https://docs.aws.amazon.com/cdk/latest/guide/bootstrapping.html)
(略)
According to the CDK bootstrapping guide, each environment (a combination of account and region) must be bootstrapped before you deploy to it. Deploying to an environment that hasn't been bootstrapped produces an error that the SSM parameter is missing. The emulator showed nearly the same message. The could not be used to assume lines before it mean that the CDK CLI could not assume the IAM roles (such as cdk-hnb659fds-deploy-role-…). According to the description of the roles that bootstrap creates, bootstrap creates these roles, and the CDK CLI assumes them during deployment. After bootstrap, these lines did not appear during deploy.
Prepare for deployment with cdk bootstrap
Running cdk bootstrap created a CloudFormation stack named CDKToolkit. The whole process took about 13 seconds. (Part of the output is omitted.)
$ cdk bootstrap
⏳ Bootstrapping environment aws://<12-digit 0>/us-east-1...
Trusted accounts for deployment: (none)
Trusted accounts for lookup: (none)
Using default execution policy of 'arn:aws:iam::aws:policy/AdministratorAccess'. Pass '--cloudformation-execution-policies' to customize.
CDKToolkit: creating CloudFormation changeset...
(略)
CDKToolkit | 1/15 | 2:01:09 AM | CREATE_COMPLETE | AWS::S3::Bucket | StagingBucket
CDKToolkit | 2/15 | 2:01:09 AM | CREATE_COMPLETE | AWS::SSM::Parameter | CdkBootstrapVersion
(略)
CDKToolkit | 12/15 | 2:01:09 AM | CREATE_COMPLETE | AWS::CloudFormation::Stack | CDKToolkit
✅ Environment aws://<12-digit 0>/us-east-1 bootstrapped.
I checked what was created with the AWS CLI. There is an S3 bucket for staging (where the zipped Lambda code and templates are stored), an ECR repository for Docker images, five IAM roles, and an SSM parameter that holds the bootstrap version. According to the official documentation, hnb659fds is a fixed string used by the default bootstrap.
$ aws ssm get-parameter --name /cdk-bootstrap/hnb659fds/version --query Parameter.Value --output text
34
$ aws s3 ls
2026-11-20 02:01:09 cdk-hnb659fds-assets-<12桁の0>-us-east-1
$ aws ecr describe-repositories --query 'repositories[].repositoryName'
[
"cdk-hnb659fds-container-assets-<12桁の0>-us-east-1"
]
$ aws iam list-roles --query 'Roles[].RoleName'
[
"cdk-hnb659fds-cfn-exec-role-<12桁の0>-us-east-1",
"cdk-hnb659fds-deploy-role-<12桁の0>-us-east-1",
"cdk-hnb659fds-file-publishing-role-<12桁の0>-us-east-1",
"cdk-hnb659fds-image-publishing-role-<12桁の0>-us-east-1",
"cdk-hnb659fds-lookup-role-<12桁の0>-us-east-1"
]

Create the stack with cdk deploy
After bootstrap, I ran cdk deploy again, and this time it succeeded. --require-approval never proceeds without asking for confirmation. With the default broadening setting, the CLI asks before continuing if a change widens permissions or security group rules. The could not be used to assume lines from earlier did not appear.
$ cdk deploy --require-approval never
✨ Synthesis time: 2.05s
CdkappStack: start: Building CdkappStack/Custom::S3AutoDeleteObjectsCustomResourceProvider Code
CdkappStack: success: Built CdkappStack/Custom::S3AutoDeleteObjectsCustomResourceProvider Code
CdkappStack: start: Building Hello/Code
CdkappStack: success: Built Hello/Code
(略)
CdkappStack: success: Published Hello/Code (current_account-current_region-63668ddd)
CdkappStack: creating CloudFormation changeset...
CdkappStack: deploying... [1/1]
(略)
CdkappStack | 11/12 | 2:01:26 AM | CREATE_COMPLETE | AWS::CloudFormation::Stack | CdkappStack
CdkappStack | 12/12 | 2:01:26 AM | CREATE_COMPLETE | Custom::S3AutoDeleteObjects | Uploads/AutoDeleteObjectsCustomResource/Default (UploadsAutoDeleteObjectsCustomResource9FEBEEBB)
✅ CdkappStack
✨ Deployment time: 6.13s
Outputs:
CdkappStack.BucketName = cdkappstack-uploads4f6eb0fd-ba006595367a
CdkappStack.FunctionName = CdkappStack-Hello4A628BD4-D51B84266CC2
CdkappStack.QueueUrl = https://sqs.us-east-1.amazonaws.com/<12-digit 0>/CdkappStack-JobsDF1CC2D4-A2F472DEBD4F
Stack ARN:
arn:aws:cloudformation:us-east-1:<12桁の0>:stack/CdkappStack/793159eb-31d1-a732-e24a-92c209c938af
✨ Total time: 13.7s
Before passing the templates to CloudFormation, the CDK CLI uploaded the zipped Lambda code and the template to the staging bucket. The bucket contained two zip files (the code for the Hello function and the code for the custom resource used by autoDeleteObjects) and the template JSON.
$ aws s3 ls s3://cdk-hnb659fds-assets-<12-digit 0>-us-east-1/
2026-11-20 02:01:20 238 69c9cd74120f78ed47a54dccb156fb508c0f53289e4fd83fd103cd4af61141f2.zip
2026-11-20 02:01:20 2569 6dba3da2af96b69a187d689b326e5272b9abc8671c0427845a8bed243e02877d.zip
2026-11-20 02:01:20 12386 ef1fcbe707ddd5e4037194a274efcd29841d410fab14b4e4fbddcd95a8b4db92.json
I invoked the deployed Lambda function using the FunctionName from the output. The uploaded code ran, and the environment variable contained the queue URL. The queue's visibility timeout was 60 seconds, as specified in the code.
$ aws lambda invoke --function-name CdkappStack-Hello4A628BD4-D51B84266CC2 --cli-binary-format raw-in-base64-out --payload '{"name":"cdk"}' response.json && cat response.json; echo
{
"StatusCode": 200,
"ExecutedVersion": "$LATEST"
}
{"message":"hello, cdk","queueUrl":"https://sqs.us-east-1.amazonaws.com/<12桁の0>/CdkappStack-JobsDF1CC2D4-A2F472DEBD4F"}
$ aws sqs get-queue-attributes --queue-url https://sqs.us-east-1.amazonaws.com/<12-digit 0>/CdkappStack-JobsDF1CC2D4-A2F472DEBD4F --attribute-names VisibilityTimeout
{
"Attributes": {
"VisibilityTimeout": "60"
}
}
Check changes with cdk diff before applying them
I changed the queue's visibilityTimeout from 60 seconds to 120 seconds and ran cdk diff. cdk diff creates a CloudFormation changeset and compares it. It showed that only the queue's VisibilityTimeout would change.
$ cdk diff
(略)
Stack CdkappStack (aws://<12-digit 0>/us-east-1)
Resources
[~] AWS::SQS::Queue Jobs JobsDF1CC2D4
└─ [~] VisibilityTimeout
├─ [-] 60
└─ [+] 120
✨ Number of stacks with differences: 1
When I ran cdk deploy, only the queue was updated.
$ cdk deploy --require-approval never
(略)
CdkappStack | 0/3 | 2:01:51 AM | UPDATE_IN_PROGRESS | AWS::SQS::Queue | Jobs (JobsDF1CC2D4)
CdkappStack | 0/3 | 2:01:51 AM | UPDATE_IN_PROGRESS | AWS::CloudFormation::Stack | CdkappStack User Initiated
CdkappStack | 1/3 | 2:01:51 AM | UPDATE_COMPLETE | AWS::SQS::Queue | Jobs (JobsDF1CC2D4)
CdkappStack | 2/3 | 2:01:52 AM | UPDATE_COMPLETE | AWS::CloudFormation::Stack | CdkappStack
CdkappStack | 3/3 | 2:01:52 AM | UPDATE_COMPLETE_CLEANUP | AWS::CloudFormation::Stack | CdkappStack
✅ CdkappStack
(略)
$ aws sqs get-queue-attributes --queue-url https://sqs.us-east-1.amazonaws.com/<12-digit 0>/CdkappStack-JobsDF1CC2D4-A2F472DEBD4F --attribute-names VisibilityTimeout
{
"Attributes": {
"VisibilityTimeout": "120"
}
}
Delete a non-empty bucket with cdk destroy
I put one file in the bucket and then ran cdk destroy --force. --force proceeds without asking for confirmation to delete. Even with the file still in the bucket, the whole stack, including the bucket, was deleted.
$ aws s3 cp --no-progress hello.txt s3://cdkappstack-uploads4f6eb0fd-ba006595367a/hello.txt
upload: ./hello.txt to s3://cdkappstack-uploads4f6eb0fd-ba006595367a/hello.txt
$ cdk destroy --force
CdkappStack: destroying... [1/1]
(略)
CdkappStack | 9 | 2:02:00 AM | DELETE_COMPLETE | AWS::S3::Bucket | Uploads (Uploads4F6EB0FD)
CdkappStack | 10 | 2:02:00 AM | DELETE_COMPLETE | AWS::IAM::Role | Custom::S3AutoDeleteObjectsCustomResourceProvider/Role (CustomS3AutoDeleteObjectsCustomResourceProviderRole3B1BD092)
CdkappStack | 11 | 2:02:01 AM | DELETE_COMPLETE | AWS::CloudFormation::Stack | CdkappStack
✅ CdkappStack: destroyed
$ aws s3 ls
2026-11-20 02:01:09 cdk-hnb659fds-assets-<12桁の0>-us-east-1
Only the staging bucket created by bootstrap remained. cdk destroy did not delete the CDKToolkit stack.
I removed autoDeleteObjects: true, leaving only removalPolicy: cdk.RemovalPolicy.DESTROY, and ran the same steps. This time, the deletion failed.
$ cdk destroy --force
(略)
CdkappStack | 0 | 2:02:19 AM | DELETE_FAILED | AWS::S3::Bucket | Uploads (Uploads4F6EB0FD) Resource handler returned message: "The bucket you tried to delete is not empty (Service: S3, Status Code: 409, (略))
(略)
❌ CdkappStack: destroy failed DeploymentError: The stack named CdkappStack is in a failed state. You may need to delete it from the AWS console : DELETE_FAILED (The following resource(s) failed to delete: [Uploads4F6EB0FD]. )
$ aws cloudformation list-stacks --query 'StackSummaries[].[StackName,StackStatus]' --output text
CDKToolkit CREATE_COMPLETE
CdkappStack DELETE_COMPLETE
CdkappStack DELETE_FAILED
The aws-cdk-lib S3 documentation (Bucket deletion) says that a bucket with RemovalPolicy.DESTROY is deleted only when it is empty, and that you need autoDeleteObjects to delete its contents along with it. autoDeleteObjects empties the bucket using a Lambda function for a custom resource. That is why Custom::S3AutoDeleteObjects appeared in the deploy output. The emulator behaved as the documentation describes. After the failure, I deleted the file with aws s3 rm and ran cdk destroy --force again, and the stack was deleted.
After restarting the emulator, bootstrap again
As the official limitations documentation describes, AWS Emulator keeps resources only in memory, and stopping it removes everything. After I stopped and restarted the emulator, the CDKToolkit stack and the staging bucket were both gone.
$ aws cloudformation list-stacks --query 'StackSummaries[].[StackName,StackStatus]' --output text
$ aws s3 ls
This is the same state as when the first cdk deploy stopped. If you deploy every time you start the emulator in CI or a similar setup, run cdk bootstrap followed by cdk deploy as a single sequence. That way, the stack is created from an empty state in the same way each time.





