Manage AWS infrastructure in your favorite programming language. Can you?
A modern approach to simplifying your AWS infrastructure
If you're here, then you are someone who already has knowledge of the AWS cloud, some of its tools and services, or you've got some idea about managing its (AWS) services using the CLI (Command Line Interface), or maybe just exploring this blog. Before you read this, you should have understood the title and might have wondered if it's possible to manage a cloud infrastructure effectively using different programming languages. Yes, you can. Let's see how!
Introducing AWS Cloud Development Kit (CDK)
Well, before we get started, let us just understand CDK in basic terms. CDK is an abbreviated form of Cloud Development Kit. In simple words, it can be explained or understood as a powerful framework that simplifies the management, scalability, and deployment of cloud infrastructure. Managing cloud infrastructure is a difficult task, especially when various AWS services and resources are involved. Manually provisioning the resources using the AWS management console involves multiple repetitive steps, which can sometimes lead to human errors while doing so. To avoid these, we will switch to IaC (Infrastructure as a Code) to simplify the definitions and management of resources with just a piece of code. Let us now understand how AWS CDK works and how we can achieve this. Before jumping in, let's get some more basics covered. CDK, as mentioned above, is a cloud development kit that is used for defining cloud infrastructure in code and provisioning it through an AWS service called "CloudFormation". CloudFormation, if you don't know, is explained below. AWS CDK now supports the following programming languages: TypeScript, JavaScript, Python, Java, C#/.Net, and Go. Therefore, developers can use any of the above programming languages of their choice to define the resources.
What is CloudFormation?
CloudFormation is an AWS service that helps set up your AWS resources so that one can spend less time on the management aspect and focus more on the innovation side and on the real-time applications that run on AWS. CloudFormation allows the use of code to define and manage AWS infrastructure resources. It enables us to construct or build templates in JSON or YAML formats that define the AWS resources, configurations, and their relationships. Now that we have understood what exactly AWS CDK is and the reason it's being used by most organizations lately, let us now understand some basic keywords or terms around it.
Let's talk, CDK!
(Important to understand)
CDK is usually comprised of constructs, stacks, and apps.
CONSTRUCTS: Constructs are usually the fundamental blocks of the CDK applications (Apps in particular). It contains all of the information that is required by AWS Cloud Formation to produce a component. If you would like to learn and understand more about this, there's a video that provides an in-depth overview of CDK constructs and how to utilize them in your Infrastructure as a Code (IaC).
Check out the CDK constructs tutorial
STACKS: The unit of resource management and deployment in CDK is understood as a stack. A stack represents a collection of AWS resources and configurations that are defined within a specific scope. They are created using CDK apps. They allow you to group and manage the relevant resources. Any number of stacks can be defined inside your AWS CDK app. I know, that's quite some information to process. But it will make more sense when you look at the architecture of a CDK app below.
CDK App: An app is something inside which one or more stacks are present that contain various AWS services and resources that are to be defined and deployed. In short, a CDK app acts as a container for stacks and is usually referred to within the scope of those stacks. With just a single command
"cdk deploy", we can deploy all the stacks that we have defined inside our CDK app. A few of the commands required for installation and configuration are listed below.
To simplify the entire architecture, inside a CDK app are the stacks, and inside the stacks are various constructs that hold the resources that are further converted to CloudFormation templates and deployed as infrastructures on AWS.
Time to install and get going.
Prerequisites:
Before we set up the environment on our desktop, make sure you have some of these prerequisites in place. If you don't, you can still do it now. Firstly,
AWS account: You will primarily need an AWS account to use CDK.
AWS CLI: You might need to install the AWS Command Line Interface (CLI) and configure it with your AWS credentials. Download and install " AWS CLI"
Node.js and npm packages: AWS CDK uses Node.js for its Command Line Interface (CLI). So, make sure you have these installed as well. Download and install " Node.js"
Almost there! Once you have all these listed, you are good to go.
Deploy your first stack using CDK:
Now, open your terminal or command prompt, and we will run the following command to install CDK globally using npm:
npm install -g aws-cdk
After doing this, verify that CDK has been successfully installed by checking its version:
cdk --version
As mentioned above, AWS CDK uses the AWS CLI's configuration. To access your AWS account, make sure you have already configured the AWS CLI. If not, run:
aws configure
At this stage, you will be prompted to enter your AWS Access Key ID, Secret Access Key, Default Region, and Output Format. For more help on using CLI, refer to "AWS Command Line Interface (CLI)" By now, you should have successfully installed and set up your CDK environment.
Now, let's create a CDK app in our preferred programming language. I'm a huge Python nerd, so there's no choice in not choosing Python. You can choose yours. Replace Python with your preferred programming language if you prefer a different one (e.g.,
typescript
,javascript
, etc.). Here, we run the following command to initialize our app:cdk init app --language=python
After this, navigate to your CDK app directory by changing your current working directory to the newly created CDK app folder by running:
cd <your-app-name>
Now, we define the resources required to deploy the infrastructure on AWS inside the app we have just created, using constructs in one of the programming languages. After defining the stack that we want to deploy, the last step is the deployment. Here's an example of how a basic stack looks like:
from aws_cdk import core class MyCdkStack(core.Stack): def __init__(self, scope: core.Construct, id: str, **kwargs) -> None: super().__init__(scope, id, **kwargs) # Define AWS resources here # For example, create an S3 bucket: from aws_cdk import aws_s3 as s3 bucket = s3.Bucket( self, "MyCdkBucket", versioned=True, )
Time for deployment. Now, we deploy the infrastructure or resources that we defined earlier using the command:
cdk deploy
Boom, there you go! You have just deployed some infrastructure on AWS using CDK in your favorite programming language. After this, from now on, you can manage the infrastructure using the Cloud Development Kit (CDK), make changes to your code as needed, and simultaneously deploy the updates.
More on how to write and define different kinds of stacks and resources using various programming languages will be discussed in the upcoming blogs of this series on AWS CDK. Stay tuned; let us also build some cool projects around CDK. This blog just gives a little sneak peek into the world of the AWS Cloud Development Kit; there's still much more to learn and understand further. Meanwhile, I've attached a few links and resources that you can refer to for documentation, as well as a link to one of the basic projects that I've tried around CDK. Please check them all out here. That's all for now. See you soon in the next one :)
Ps: If you would want to learn or build some projects around CDK or AWS, please feel free to reach out to me on " Amogha Kancharla "
Useful links: