In this video, you’ll see how to create infrastructure patterns for your organization
using the AWS Cloud Development Kit (AWS CDK).
With patterns, you can define reusable components that represent multiple AWS resources,
configure AWS infrastructure using familiar programming languages,
and share complex configurations like code across your organization.
The AWS CDK allows developers to define reusable cloud components known as constructs,
the most complex type of which are known as patterns.
An organization can then re-use these patterns in its cloud applications without having to
manually configure and implement resources or resource connections.
To get started, let’s navigate to the Cloud9 integrated development environment (IDE).
First, let’s ensure the AWS command-line interface (CLI) and CDK are installed on this Cloud9 instance.
As you can see, the AWS CLI is already installed.
The CDK is also installed.
To ensure you have the latest version of the CDK, you can use the Node Package Manager
(NPM) to check for and install updates.
You can use the “cdk doctor” command to ensure the current version of AWS CDK is properly
installed and configured to display relevant environment variables.
The AWS CDK lets you define your AWS cloud infrastructure
in a general-purpose programming language.
Currently, the AWS CDK supports TypeScript, JavaScript, Python, Java, and C#.
The specific language you work in has its own prerequisites.
For our purposes, we’ll install what we need to use Python, Java, and JavaScript.
To build the Python demonstration application, you must install twine from the Python package index.
Next, to use Java, we’ll install Open JDK version 8.
Next, we’ll configure the Java Runtime Environment and Java Compiler to use the correct binary for execution.
We’ll also download and install Apache Maven, a Java package manager and build automation tool.
Next, let’s configure our shell environment variables so Maven can execute commands successfully,
and our Path variable to include the Maven binaries.
Let’s confirm that Maven is now installed.
Now that Maven has been installed, let’s return to the project directory.
Next, let’s create a main project folder to house our infrastructure pattern.
We will export artifacts in Python, Java, and JavaScript.
This pattern will serve as a code library that can be used in other AWS CDK applications
that are built in our organization, so we want to name it clearly.
We’ll use the default construct that gets scaffolded by the AWS CDK.
The construct is a dead-letter queue for an Amazon Simple Notification Service
(Amazon SNS) topic subscription.
Navigate into the main project folder.
Let’s initialize a library in TypeScript, using the command “cdk init lib”
to specify that we are creating a library to be used by other applications.
As you can see, the library initialization command created several necessary files and
folders for the project.
Let's take a look at the package.json file to see the default project parameters and
dependencies included in the library scaffolding.
Notice that the AWS CDK and relevant constructs for the sample application
are included as dependencies in the project.
Now let’s look at the lib directory to see the default boilerplate code provided after the initialization.
As you can see, the AWS CDK construct libraries for the default dead-letter-queue application were imported.
A library defines codes for a construct rather than for an application.
For instance, the DeadLetterQueue construct extends the base Construct class provided
by the AWS CDK.
The DeadLetterQueue object creates a queue with a 300-second visibility timeout,
creates an SNS topic, and then adds a subscription from the queue to the topic.
Let’s return to Bash and run the build script on this project.
As you can see, the build script has successfully compiled our TypeScript code to JavaScript.
Next, let’s use the NPM to install “jsii” and “jsii-pacmak,” which are open source
projects that allow other programming languages to interface to our TypeScript project construct.
Now that the projects are installed, let’s update the build and package scripts in the package.json files.
The project is now configured to execute the appropriate jsii command to package the construct
so that it is available to projects in several other languages, such as Python and Java.
You can also add further details about the project to the package.json file
so it can be properly indexed and accessed by artifact repositories in Python and Java.
Let’s add "author", "repository', and "license" sections.
Next, let’s configure jsii so that it will properly compile the construct to importable Python and Java modules.
The output directory of our jsii-compiled package is the dist directory in our dead-letter-queue project folder.
For Python, you only need to specify the distributed package name and its module identifier.
For Java, you must specify the package name, and since we are using Maven, also the groupId and artifactId.
Under peerDependencies, let’s specify the constructs library as a dependency as well.
The AWS CDK libraries will be automatically added under the peerDependencies section
when the command “npm run build” is executed.
You can see tests associated with the project in the test directory.
Since we’re not using SynthUtils to test the construct, let’s remove it from the import in the main test code.
Let’s return to the index file of our construct.
Let’s make all parameters associated with the DeadLetterQueueProps interface “read only,”
since when we move from one language to another through the jsii compilation,
we’ll be passing parameters by value.
Return to Bash.
Let’s move the TypeScript configuration to a file that will not be read,
since jsii will generate its own configuration when it is used to package our construct.
Next, let’s build the construct using the “npm run build” command.
That command called the "build" script in our package.json file, which has been aliased to jsii.
Now run the package script, which likewise has been aliased to jsii-pacmak.
As you can see, the dist directory we specified for output has been created.
Let’s take a look at its contents.
The Java package has been created with all the data contained within a package skeleton,
as specified by our jsii Java build parameters.
Similarly, the compiled JavaScript has been compiled in a folder.
Since our original code was configured in TypeScript, the resulting JavaScript file
was directly compiled and placed in a zipped folder.
Finally, the output Python package has been generated in the Python folder.
Now that you’ve seen how you can write an AWS CDK construct in one language (TypeScript)
and compile it for use in several different languages,
let’s see how we can use the package compiled to Python in a separate application.
Create and enter a directory for the application.
Let’s initialize a Python project in our home directory.
Let’s take a look at the code that was just generated.
Here’s the boilerplate AWS CDK code generated by the initialization.
Let’s initialize the virtual environment automatically generated by the “cdk init” command,
and then install all the necessary dependencies through the Python package manager.
Now, let’s take a look at the CloudFormation template that is generated from
the Python AWS CDK application code.
Note that there are no resources in the template yet.
Next, let’s install the Python module that was generated by Jsii from our TypeScript construct code.
To do so, run the command "pip install" along with the path of the wheel file that was generated from Jsii.
Now that the module has been successfully installed, you can add it to the application
using an import statement at the top of the CloudFormation stack .py file.
Let’s add our construct to the stack.
Here, we have defined the Dead Letter Queue object, as defined in our dead-letter-queue project,
in our Python AWS CDK application, passing it a name of "MyDLQ" and a visibility timeout of 600 seconds.
Return to Bash.
Now that we’ve instantiated the dead letter queue object in our Python AWS CDK application and saved it,
let’s see the generated CloudFormation template.
As you can see, the dead letter queue object has been successfully defined in the generated
CloudFormation stack template, which indicates that the construct has been read successfully.
You’ve just seen how to create infrastructure patterns for your organization using the AWS CDK.
Thanks for watching.
Now it’s your turn to try.