DEV Community

Mikaeel Khalid for AWS Community Builders

Posted on • Originally published at blog.mikaeels.com on

How to List Stacks in AWS CDK

Image description

AWS Cloud Development Kit (CDK) is a powerful tool that allows developers to define and deploy their infrastructure as code. One of the key features of CDK is the ability to create and manage stacks, which are collections of AWS resources that are deployed together. In this article, we will explore how to list stacks in AWS CDK.

To list stacks in AWS CDK, you can use the cdk list command. This command will display a list of all stacks that have been defined in your CDK application. The output will include the stack name, the stack environment, and the stack status.

To list all stacks in your CDK application, you can use the following command:

cdk list
Enter fullscreen mode Exit fullscreen mode

Note, we could use the alias - cdk ls

This command will display a list of all stacks that have been defined in your CDK application, including the stack name, the stack environment, and the stack status.

If you want to list stacks in a specific environment, you can use the -e or --environment option. For example, to list stacks in the prod environment, you can use the following command:

cdk list -e prod
Enter fullscreen mode Exit fullscreen mode

This command will display a list of all stacks that have been defined in the prod environment, including the stack name, the stack environment, and the stack status.

If you want to list stacks in a specific region, you can use the -r or --region option. For example, to list stacks in the us-east-1 the region, you can use the following command:

cdk list -r us-east-1
Enter fullscreen mode Exit fullscreen mode

This command will display a list of all stacks that have been defined in the us-east-1 region, including the stack name, the stack environment, and the stack status.

In addition to listing stacks, the cdk list command also allows you to filter the output by stack status. You can use the -s or --status option to filter the output based on the stack status. For example, to list only stacks that are in the CREATE_COMPLETE status, you can use the following command:

cdk list -s CREATE_COMPLETE
Enter fullscreen mode Exit fullscreen mode

This command will display a list of all stacks that are in the CREATE_COMPLETE status, including the stack name, the stack environment, and the stack status.

Another useful option when using the cdk list command is the --long option. This option will display additional information about each stack, such as the stack's id, name, AWS account, region, environment, and where the stack is deployed.

For example, if you want to view all the stacks in your CDK application, along with additional information, you can use the following command:

cdk list --long
Enter fullscreen mode Exit fullscreen mode

The --long flag outputs information about the environment (accountand region) our stacks belong to:

- id: my-stack-prod
  name: my-stack-prod
  environment:
    account: '123456789'
    region: us-east-1
    name: aws://123456789/us-east-1

- id: my-stack-dev
  name: my-stack-dev
  environment:
    account: '123456789'
    region: us-east-1
    name: aws://123456789/us-east-1
Enter fullscreen mode Exit fullscreen mode

In conclusion, the cdk list command is a powerful tool that allows you to list stacks in AWS CDK. You can use this command to list all stacks in your CDK application, list stacks in a specific environment, list stacks in a specific region, and filter the output by stack status. This makes it easy to manage your stacks and ensure that your infrastructure is deployed correctly.

Top comments (0)