Before you begin, make sure you already created your AWS access key ID and your AWS secret key. Or if you’re learning through a company’s infrastructure, make sure your local machine can load AWS credentials via your company’s SSO login. Please read https://gefyra.co/getting-started-with-aws-cdk/ before you begin.
Creating A Default Framework
I have here an empty Github folder, and I want to begin creating my AWS CDK framework to deploy infrastructure on AWS cloud.
As mentioned in my previous post, I will be installing my Typescript setup with the following command:
cdk init app --language typescript
You don’t really have to initialize a template from a default configuration, but it will make things easier if you would start to learn from here. AWS CDK is highly modular, and if you’re already proficient with Typescript and NPM, you should have no problem creating your custom framework on your own.
But for learning purposes, we’re going to use AWS CDK’s default settings and explain things here.
After installing your CDK app, you’re greeted with the following folder structure:
I will not be going through every little detail here, and you can read it up on the official AWS documentation on its core concepts, but here are some things you must know:
binfolder runs the actual code that deploys infrastructure. A default file will be created which follows the root folder’s name.
libfolder contains the parameters and settings that you want to run in Typescript files. A default file will be created which follows the root folder’s name.
testfolder runs tests to see if your Typescript code is valid. A default file will be created which follows the root folder’s name.
cdk.jsoncontains code and context to run the app. The
cdkcommand will look for this configuration json to run your app. For now, we’ll not modify the
cdk.jsonfile and use its default settings.
At this stage, nothing is done. AWS CDK only created a framework for you to deploy infrastructure, but we have not linked this app to our AWS account yet. Let’s get this done before we move anywhere further.
Linking Or Bootstrapping Your AWS CDK App To Your Account
Go to your AWS credentials file and look into it by pressing:
The above code might defer slightly based on where you store your AWS configuration files.
If you have followed the previous post on Getting Started With AWS CDK, you should have a configuration that looks like this:
So the string within the square brackets is the profile identifier, and under the profile, we have the
aws_access_key_id and the
aws_secret_access_key. Look for the profile where you want to deploy infrastructure, and remember the profile identifier.
Then, head back to the root directory of your AWS CDK app, and type:
cdk bootstrap --profile <profile_identifier>
You should see something like this in your terminal:
What is happening here?
For AWS CDK to work, it has to store its configurations in an S3 bucket in case anything goes wrong with your local machine. Without bootstrapping, there is no way to link your CDK app with your AWS account.
For more information, check out the official documentation here: https://docs.aws.amazon.com/cdk/latest/guide/bootstrapping.html
Finally, we want to test if the bootstrap works.
If this is your first CDK bootstrap, then a new bucket will be created for you. However, this isn’t my first time, and so CDK will use the bootstrap bucket that has existed instead.
Another litmus test we can apply is to create an S3 bucket via AWS CDK, but I’m going to stop here before we move on to the next tutorial.