Introduction
This tutorial will show you how to install the AWS CDK on Ubuntu and deploy your first JavaScript Infrastructure as Code (IAS) to AWS.
Warning: I do not assume any responsibility if you incur any charges. You are responsible for managing your resources in your AWS account.
If you find yourself stuck, I recommend reviewing the AWS documentation to find detailed concepts, definitions, and critical terminology to help you along the way.
AWS CDK Intro
"AWS CDK is a software development framework for defining cloud infrastructure in code and provisioning it through AWS CloudFormation.
You use the CDK to define your cloud resources in a familiar programming language. The CDK supports TypeScript, JavaScript, Python, Java, C#/.Net, and (in developer preview) Go." - AWS Docs.
Today we're going to leverage JavaScript, which you can port to TypeScript at a later time.
Prerequisites
This tutorial assumes you're comfortable, familiar, and have the necessary software installed on your system to follow along.
- AWS IAM Credentials
- JavaScript
- Homebrew
- Node JS 10.13.0 or later
- Ubuntu OS
- macOS
- Terminal
- Command Line Interface
Note: I will be installing AWS CDK on Ubuntu. Some OS-specific troubleshooting may be required if you run into issues.
Ready!? Let's begin.
Install The AWS CDK
Go ahead and type the following command to begin installing the CDK:
$ npm install -g aws-cdk
Verify that you have installed the CDK:
$ cdk --version
Create A CDK Project
Create a folder where your CDK project will live:
$ mkdir MyTestService
Change directory
$ cd MyTestService
In an NPM fashion, you can create a CDK project using the following command:
$ cdk init app --language javascript
Depending on your host system, it will take a moment to create all the resources necessary to prepare your IaC project. So, sit tight and have some delicious warm coffee.
A successful install will result in the following example output:
✅ All done!
****************************************************
*** Newer version of CDK is available [1.106.1] ***
*** Upgrade recommended (npm install -g aws-cdk) ***
****************************************************
You can view a list of your stacks by using the following command:
$ cdk ls
example output:
MyTestServiceStack
But we're not done yet!
Add an Amazon S3 Resources
Make sure you're in the root directory of your project. If not, the CDK commands will not function properly.
For this following command, we will add an AWS resource, or within the context Node JS, a package, like so:
$ npm install @aws-cdk/aws-s3
Next, define an Amazon S3 bucket in the stack using the Bucket construct. To do this, locate line 13 or the commented out text and append the following code:
const cdk = require('@aws-cdk/core');
const s3 = require('@aws-cdk/aws-s3');
class HelloCdkStack extends cdk.Stack {
constructor(scope, id, props) {
super(scope, id, props);
// The code that defines your stack goes here
new s3.Bucket(this, 'MyFirstBucket', {
versioned: true,
removalPolicy: cdk.RemovalPolicy.DESTROY,
autoDeleteObjects: true
});
}
}
module.exports = { HelloCdkStack }
Synthesize an AWS CloudFormation template
$ cdk synth
The "synth" command generates a CloudFormation file. Also, the actual output will extend for several lines. Makes sure to scroll down and explore the generated CloudFormation file.
Resources:
CDKMetadata:
Type: AWS::CDK::Metadata
Properties:
....
If you received an error like --app is required..., it's probably because you are running the command from a subdirectory. Navigate to the main app directory and try again.
Deploy Your Stack
First, bootstrap your project before deployment, ensuring that AWS is aware of the permissions between resources.
$ cdk bootstrap
AND then you should see a similar output:
⏳ Bootstrapping environment aws://965805005461/us-east-2...
CDKToolkit: creating CloudFormation changeset...
Finally, deploy your stack to the AWS Cloud. To do that, type the following command:
$ cdk deploy
As your code is preparing to deploy, it will prompt you to for a yes or no. Type Yes and then press Enter to continue with the deployment process.
While that's doing its thing, you can navigate to the AWS CloudFormation console and see the magic in real-time.
Delete Your Resources
To delete your resources, you just deployed, type the following command:
$ cdk destroy
When prompted to delete all the resources you have created, Type "Y" for yes — otherwise, type "N" for no.
Example prompt:
Are you sure you want to delete: MyTestServiceStack (y/n)? y
Great Job! You just created your first AWS CDK JS APP!
These are industry sought after skills, and I hope it helps you upskill and get you where you need to go in your daily hacking or career!
Until next time!
Top comments (0)