AWS Cloud Development Kit (AWS CDK)
This tutorial provides information about the AWS CDK, a framework for defining cloud infrastructure in code and provisioning it through AWS CloudFormation.
In this tutorial, you’ll learn about the following:
- What is the AWS CDK ?
- Working with the AWS CDK in TypeScript.
- AWS CDK prerequisites.
- Creating a AWS CDK Typescript project.
- Building, synthesizing, and deploying.
What is the AWS CDK?
The AWS CDK lets you build reliable, scalable, cost-effective applications in the cloud with the considerable expressive power of a programming language. This approach yields many benefits, including:
- Build with high-level constructs that automatically provide sensible, secure defaults for your AWS resources, defining more infrastructure with less code.
- Use programming idioms like parameters, conditionals, loops, composition, and inheritance to model your system design from building blocks provided by AWS and others.
- Put your infrastructure, application code, and configuration all in one place, ensuring that at every milestone you have a complete, cloud-deployable system.
- Employ software engineering practices such as code reviews, unit tests, and source control to make your infrastructure more robust.
- Connect your AWS resources together (even across stacks) and grant permissions using simple, intent-oriented APIs.
- Import existing AWS CloudFormation templates to give your resources a CDK API.
- Use the power of AWS CloudFormation to perform infrastructure deployments predictably and repeatedly, with rollback on error.
- Easily share infrastructure design patterns among teams within your organization or even with the public.
The AWS CDK supports TypeScript, JavaScript, Python, Java, C#/.Net, and Go. Developers can use one of these supported programming languages to define reusable cloud components known as Constructs. You compose these together into Stacks and Apps.
Working with the AWS CDK in TypeScript
TypeScript is a fully-supported client language for the AWS CDK and is considered stable. Working with the AWS CDK in TypeScript uses familiar tools, including Microsoft’s TypeScript compiler (tsc
), Node.js and the Node Package Manager (npm
). You may also use Yarn if you prefer, though the examples in this Guide use NPM. The modules comprising the AWS Construct Library are distributed via the NPM repository, npmjs.org.
AWS CDK prerequisites
- AWS Account : To work with the AWS CDK, you must have an AWS account and credentials and a corresponding access key. If you don’t have an AWS account yet, see Create and Activate an AWS Account.
- NodeJS: All AWS CDK applications require Node.js 10.13 or later, even if you work in Python, Java, C#, or Go. You may download a compatible version at nodejs.org.
- TypeScript: You also need TypeScript itself (version 3.8 or later). If you don’t already have it, you can install it using
npm
.
npm install -g typescript
Test the installation by issuing
tsc -v
.
Keep TypeScript up to date with a regularnpm update -g typescript
.
4. AWS CDK Toolkit: After installing Node.js, install the AWS CDK Toolkit (the cdk
command)
npm install -g aws-cdk
Test the installation by issuing cdk --version
.
After installation, set up your workstation with your AWS credentials
write following command in terminal
aws configure
Enter your Access Key Id and press enter
Now, Enter your Secret Access Key and press enter
Now, define default region name and output format and press enter
this will configure your AWS credentials.
Creating a AWS CDK Typescript project
Each AWS CDK app should be in its own directory, with its own local module dependencies. Create a new directory for your app
mkdir test-project
cd test-project
Now initialize the app by using the cdk init command. Specify the desired template (“app”) and programming language as shown in the following examples:
cdk init app --language typescript
The cdk init command creates a number of files and folders inside the test-project
directory to help you organize the source code for your AWS CDK app.
Creating a project also installs the aws-cdk-lib
module and its dependencies.
Building, synthesizing, and deploying
Generally, you should be in the project’s root directory when building and running your application.
- Build the app: Node.js cannot run TypeScript directly; instead, your application is converted to JavaScript using the TypeScript compiler,
tsc
. The resulting JavaScript code is then executed.
It can be useful to compile manually to check for errors and to run tests. To compile your TypeScript app manually, issuenpm run build
. You may also issuenpm run watch
to enter watch mode, in which the TypeScript compiler automatically rebuilds your app whenever you save changes to a source file. - Synthesize an AWS CloudFormation template: Synthesizes a AWS CloudFormation template from one or more of the stacks in your AWS CDK app. To Synthesizes issue
cdk synth
.
Thecdk synth
command executes your app, which causes the resources defined in it to be translated into an AWS CloudFormation template. The displayed output ofcdk synth
is a YAML-format template.
The template is also saved in thecdk.out
directory in JSON format. - Deploying the stack: The
cdk synth
generates a perfectly valid AWS CloudFormation template. You could take it and deploy it using the AWS CloudFormation console or another tool. But the AWS CDK Toolkit can also do that.
To deploy the stack using AWS CloudFormation, issue:cdk deploy
.
It is optional (though good practice) to synthesize before deploying. The AWS CDK synthesizes your stack before each deployment.cdk deploy
displays progress information as your stack is deployed. When it's done, the command prompt reappears. You can go to the AWS CloudFormation console and see that it now listsTestProjetcStack
You’ve deployed your first stack using the AWS CDK — congratulations! But that’s not all there is to the AWS CDK.