Managing dependencies - AWS Cloud Development Kit (AWS CDK) v1

This is the AWS CDK v1 Developer Guide. The older CDK v1 entered maintenance on June 1, 2022 and will now only receive critical bug fixes and security patches. New features will be developed for CDK v2 exclusively. Support for CDK v1 will end entirely on June 1, 2023. Migrate to CDK v2 to have access to the latest features and fixes.

Managing dependencies

Dependencies for your AWS CDK app or library are managed using package management tools commonly used with the programming language in which you develop your app. Typically, the CDK supports the language's standard or official package management tool, if there is one, or its most popular or widely-supported one if not. You may also be able to use other tools, especially if they interoperate with the supported tools, although our ability to support alternatives is limited.

The CDK supports the following package managers.

Language Supported package management tool
TypeScript/JavaScript NPM (Node Package Manager) or Yarn
Python PIP (Package Installer for Python)
Java Maven
C# NuGet
Go Go modules
Note

The projects generated by cdk init specify dependencies for the CDK core libraries and stable constructs.

The remainder of this topic provides details on using AWS CDK dependencies in each language.

TypeScript and JavaScript

In TypeScript and JavaScript CDK projects, dependencies are specified in package.json in the project's main directory. Every AWS Construct Library module is a separate NPM package. All versions of AWS CDK packages match and specify a single version, and the same version. If the versions are different, NPM is likely to install multiple copies of CDK libraries, and you will see unpredictable compilation or runtime errors.

Tip

When you install a package using npm install, NPM records it in package.json for you.

If you prefer, you may use Yarn in place of NPM. However, the CDK does not support Yarn's plug-and-play mode, which is default mode in Yarn 2. Add the following to your project's .yarnrc.yml file to disable this feature.

nodeLinker: node-modules

Applications

The following is an example package.json generated by cdk init --language typescript. The file generated for JavaScript is similar, just without the TypeScript-related entries.

{ "name": "my-package", "version": "0.1.0", "bin": { "my-package": "bin/my-package.js" }, "scripts": { "build": "tsc", "watch": "tsc -w", "test": "jest", "cdk": "cdk" }, "devDependencies": { "@aws-cdk/assert": "1.121.0", "@types/jest": "^26.0.10", "@types/node": "10.17.27", "jest": "^26.4.2", "ts-jest": "^26.2.0", "aws-cdk": "1.121.0", "ts-node": "^9.0.0", "typescript": "~3.9.7" }, "dependencies": { "@aws-cdk/core": "1.121.0", "source-map-support": "^0.5.16" } }

All CDK apps use @aws-cdk/core, which must be specified in the dependencies section of package.json along with all other AWS Construct Library module your app uses.

Note

All AWS CDK dependencies must have the same version.

Specify versions of libraries and tools needed to test your app (for example, the jest testing framework) in the devDependencies section of package.json. Optionally, use ^ to specify that later compatible versions are acceptable.

Construct libraries

If you're developing a construct library, specify its dependencies via a combination of the peerDependencies and devDependencies sections, as shown in the following example package.json file.

{ "name": "my-package", "version": "0.0.1", "peerDependencies": { "@aws-cdk/core": "1.140.0", "@aws-cdk/aws-s3": "1.140.0", "@aws-cdk/aws-iam": "1.140.0" }, "devDependencies": { "jsii": "^1.50.0", "aws-cdk": "^1.140.0" } }

In peerDependencies, use a caret (^) to specify the lowest version of the AWS CDK packages that your library works with, to maximize the compatibility of your library with a range of CDK versions. Using peerDependencies makes sure there is only one copy of all CDK libraries in the node_modules tree.

In devDependencies, specify the tools and libraries you need for testing, optionally with ^ to indicate that later compatible versions are acceptable. Specify exactly (without ^ or ~) the lowest version of the CDK packages that you advertise your library be compatible with. This practice ensures that your tests run against those versions, so that if you inadvertently use a feature found only in newer versions, your tests can catch it.

Warning

peerDependencies are installed automatically only by NPM 7 and later. If you are using NPM 6 or earlier, or if you are using Yarn, you must include the dependencies of your dependencies in devDependencies, or they will not be installed, and you will receive a warning about unresolved peer dependencies

Installing and updating dependencies

Run the following command to install your project's dependencies.

NPM
# Install the latest version of everything that matches the ranges in 'package.json' npm install # Install the same exact dependency versions as recorded in 'package-lock.json' npm ci
Yarn
# Install the latest version of everything that matches the ranges in 'package.json' yarn upgrade # Install the same exact dependency versions as recorded in 'yarn.lock' yarn install --frozen-lockfile

To update the installed modules, the npm install and yarn upgrade commands given above can be used. Either command updates the packages in node_modules to the latest versions that satisfy the rules in package.json, but they do not update package.json itself, which you might want to do to set a new minimum version. If you host your package on GitHub, you can configure Dependabot version updates to automatically update package.json. Alternatively, use npm-check-updates.

Important

By design, when you install or update dependencies, NPM and Yarn choose the latest version of every package that satisfies the requirements specified in package.json. There is always a risk that these versions may be broken (either accidentally or intentionally). Test thoroughly after updating your project's dependencies.

Python

In Python, you specify dependencies by putting them in requirements.txt (for applications) or setup.py (for construct libraries). Dependencies are then managed with the PIP tool. PIP is invoked in one of the following ways:

pip command options python -m pip command options

The python -m pip invocation works on most systems; pip requires that PIP's executable be on the system path. If pip doesn't work, try replacing it with python -m pip.

cdk init --language python creates a virtual environment for your new project, which allows each project to have its own versions of dependencies, as well as a basic requirements.txt file. You must activate this virtual environment (source .venv/bin/activate) each time you begin working with the project.

Applications

An example requirements.txt follows. Because PIP does not have a dependency-locking feature, we recommend that you use the == operator to specify exact versions for all dependencies, as shown here.

aws-cdk.core==1.140.0 aws-cdk.aws-s3==1.140.0 aws-cdk.aws-iam==1.140.0
Note

All AWS CDK dependencies must have the same version.

Installing a module with pip install does not add it to requirements.txt; you should do that yourself. If you want to upgrade to a later version of a dependency, edit its version number in requirements.txt.

To install or update your project's dependencies after creating or editing requirements.txt, issue:

python -m pip install -r requirements.txt
Tip

The pip freeze command outputs the versions of all installed dependencies in a format that can be written to a text file and used as a requirements file with pip install -r. This file is convenient for pinning all dependencies (indluding transitive ones) to the exact versions you tested with. To avoid problems when upgrading packages later, use a separate file for this, e.g. freeze.txt not requirements.txt, and regenerate it when you upgrade your project's dependencies.

Construct libraries

In libraries, dependencies are specified in setup.py, so that transitive dependencies are automatically downloaded when the package is consumed by an application. Otherwise, every application that wants to use your package needs to copy your dependencies into their requirements.txt. An example setup.py is shown here.

from setuptools import setup setup( name='my-package', version='0.0.1', install_requires=[ 'aws-cdk.core==1.140.0', 'aws-cdk.aws-s3==1.140.0', 'aws-cdk.aws-iam==1.140.0', ], ... )
Note

All AWS CDK dependencies must have the same version.

To work on the package for development, create or activate a virtual environment, then run the following command.

python -m pip install -e .

Although PIP automatically installs transitive dependencies, there can only be one installed copy of any one package. The version that is specified highest in the dependency tree is selected; applications always have the last word in what version of packages get installed.

Java

In Java, dependencies are specified in pom.xml and installed using Maven. The <dependencies> container includes a <dependency> element for each package. Following is a section of pom.xml for a typical CDK Java app.

Tip

Many Java IDEs have integrated Maven support and visual pom.xml editors, which you may find convenient for managing dependencies.

<properties> <cdk.version>1.140.0</cdk.version> </properties> <dependencies> <dependency> <groupId>software.amazon.awscdk</groupId> <artifactId>core</artifactId> <version>${cdk.version}</version> </dependency> <dependency> <groupId>software.amazon.awscdk</groupId> <artifactId>s3</artifactId> <version>${cdk.version}</version> </dependency> <dependency> <groupId>software.amazon.awscdk</groupId> <artifactId>iam</artifactId> <version>${cdk.version}</version> </dependency> </dependencies>
Note

All AWS CDK dependencies must have the same version. To make it easier to update the versions of these packages while keeping them in sync, we define a propertywith the desired CDK version.

Maven does not support dependency locking, so while it is possible to specify version ranges in pom.xml, we recommend you always use exact versions to keep your builds repeatable.

Maven automatically installs transitive dependencies, but there can only be one installed copy of each package. The version that is specified highest in the POM tree is selected; applications always have the last word in what version of packages get installed.

Maven automatically installs or updates your dependencies whenever you build (mvn compile) or package (mvn package) your project. The CDK Toolkit does this automatically every time you run it, so generally there is no need to manually invoke Maven.

C#

In C# AWS CDK apps, you manage dependencies using NuGet. NuGet has four standard, mostly-equivalent interfaces; you can use the one that suits your needs and working style. You can also use compatible tools, such as Paket or MyGet or even edit the .csproj file directly.

NuGet does not allow you to specify version ranges for dependencies. Every dependency is pinned to a specific version.

Note

All AWS CDK dependencies must have the same version.

After updating your dependencies, Visual Studio will use NuGet to retrieve the specified versions of each package the next time you build. If you are not using Visual Studio, use the dotnet restore command to update your dependencies.

Editing the project file directly

Your project's .csproj file contains an <ItemGroup> container that lists your dependencies as <PackageReference elements.

<ItemGroup> <PackageReference Include="Amazon.CDK" Version="1.140.0" /> <PackageReference Include="Amazon.CDK.AWS.S3" Version="1.140.0" /> <PackageReference Include="Amazon.CDK.AWS.IAM" Version="1.140.0" /> </ItemGroup>

The Visual Studio NuGet GUI

Visual Studio's NuGet tools are accessible from Tools > NuGet Package Manager > Manage NuGet Packages for Solution. Use the Browse tab to find the AWS Construct Library packages you want to install. You can choose the desired version, including pre-release versions of your modules and add them to any of the open projects.

NuGet package manager showing Amazon CDKAWS packages available for installation.

Look on the Updates page to install new versions of your packages.

The NuGet console

The NuGet console is a PowerShell-based interface to NuGet that works in the context of a Visual Studio project. You can open it in Visual Studio by choosing Tools > NuGet Package Manager > Package Manager Console. For more information about using this tool, see Install and Manage Packages with the Package Manager Console in Visual Studio.

The dotnet command

The dotnet command is the primary command-line tool for working with Visual Studio C# projects. You can invoke it from any Windows command prompt. Among its many capabilities, dotnet can add NuGet dependencies to a Visual Studio project.

Assuming you're in the same directory as the Visual Studio project (.csproj) file, issue a command like the following to install a package. Note that since the main CDK library is included when you create a project, you should ever only need to explictly install experimental modules. Experimental modules require you to specify an explicit version number.

dotnet add package Amazon.CDK.AWS.IoT -v VERSION-NUMBER

You may issue the command from another directory by including the path to the project file, or to the directory that contains it, after the add keyword. The following example assumes that you are in your AWS CDK project's main directory.

To install a specific version of a package, include the -v flag and the desired version.

To update a package, issue the same dotnet add command you used to install it. For experimental modules, again, you must specify an explicit version number.

For more information about managing packages using the dotnet command, see Install and Manage Packages Using the dotnet CLI.

The nuget command

The nuget command line tool can install and update NuGet packages. However, it requires your Visual Studio project to be set up differently from the way cdk init sets up projects. (Technical details: nuget works with Packages.config projects, while cdk init creates a newer-style PackageReference project.)

We do not recommend the use of the nuget tool with AWS CDK projects created by cdk init. If you are using another type of project, and want to use nuget, see the NuGet CLI Reference.

Go

In Go, dependencies versions are defined in go.mod. The default go.mod is similar to the one shown here.

module my-package go 1.16 require ( github.com/aws/aws-cdk-go/awscdk/v2 v2.16.0 github.com/aws/constructs-go/constructs/v10 v10.0.5 github.com/aws/jsii-runtime-go v1.29.0 )

Package names (modules, in Go parlance) are specified by URL with the version number appended. Go's module system does not support version ranges, so all dependencies must be pinned to a specfic version.

Go automatically downloads dependencies whenever you build. The CDK does this for you automatically whenever you run your app, so there is no need to do it manually.

You may use the go get command to install a module and update go.mod. To see a list of available updates for your dependencies, issue go list -m -u all.