First Steps to AWS CDK

A useful tool to create an AWS architecture as Infrastructure as Code

Lucasretamoso
White Prompt Blog

--

Introduction

Whenever there is a challenge that arises demanding the need of technology, a systemic process is sought after to provide a solution. Sometimes, this solution can be complex in the world of software development. At White Prompt, we develop solutions within the development life cycle that go beyond code.

We apply our deep bench of experience and knowledge of the infinite possibilities of tech to solve these challenges. We set the strategy, stick to the process and dive into the dev. If you are interested in working with a team who strives to make the end product exceptional, contact us today at White Prompt.

When you want to create a system to solve a challenge, it will more than likely involve a cloud service. If a client communicates certain specifications for an architecture diagram, for example, and wants to deploy it upon completion, then you need a way to deploy using a cloud service.

So, our first question is, how is this possible?

Well, there are a few different ways to implement some services on the cloud, which include:

  • Deploy directly from console,
  • Deploy using a CLI library,
  • Deploy using a template file.

The one we will be discussing is known as Infrastructure as Code.

Infrastructure as code is the process of provisioning and managing your cloud resources by writing a template file that is both human readable, and machine consumable.

In AWS there is a service which helps you with the implementation called CloudFormation.

You have some technologies and tools that help you to generate the template file, for example the CloudFormation service has one integrated in AWS console. This is a great option. However, having the knowledge of deploying using the cloud still presents a challenge when a template needs to be created. This can be a complex task as features can be added or removed within the lifetime of the application.

Therefore, using tools that exist in the industry can lend a helping hand when you need help creating and maintaining a template — Pulumi, Terraform and CDK exist to name a few. Let’s continue discussing the framework CDK, or Cloud Development Kit.

The AWS Cloud Development Kit (AWS CDK) is an open-source software development framework to define your cloud application resources using familiar programming languages.

Provisioning cloud applications can be a challenging process that requires you to perform manual actions, write custom scripts, maintain templates, or learn domain-specific languages. AWS CDK uses the familiarity and expressive power of programming languages for modeling your applications. It provides high-level components called constructs that preconfigure cloud resources with proven defaults, so you can build cloud applications with ease. AWS CDK provisions your resources in a safe, repeatable manner through AWS CloudFormation. It also allows you to compose and share your own custom constructs incorporating your organization’s requirements, helping you expedite new projects.

Source from AWS Docs (https://docs.aws.amazon.com/cdk/v2/guide/home.html)

So, when the new architecture and process is ready to be deployed, it is best to create a test project to observe how it functions. Let’s say you chose CDK to deploy the new process because it is a native tool from AWS. With that in mind, let’s continue with the example of a challenge below.

Problem to Solve

Here we have the following example: As an Accountant, I can sum n numbers.

This example is not a large problem, but it is useful as an example because we need to create an endpoint and a lambda. Below, we have the following architecture diagram:

Now that we have the problem and objective, we can begin to code.

Requirements

Before we start, we need a few resources to help test the application and run the CDK, while still using Node and Typescript .

The requirements are:

  1. AWS Command Line Interface: This tool helps us to configure the AWS account and provides us the credential to deploy the solution.
  2. Docker: We will use @aws-cdk/aws-lambda-nodejs which compiles the code using a docker image.
    A. WSL: Docker needs this program to work. (Windows Only)
  3. NodeJS
  4. Typescript
  5. SAM CLI (optional)

Journey

  1. Create the project

First, we need to create the project, so we must install CDK by running the following command:

npm install -g aws-cdk

After that, we can create the folder where we store our project:

mkdir first-step-cdk
cd first-step-cdk
cdk init app --language typescript

Now, we should have the following structure:

.
├── README.md
├── bin
│ └── first-step-cdk.ts
├── cdk.json
├── jest.config.js
├── lib
│ └── first-step-cdk-stack.ts
├── package-lock.json
├── package.json
├── test
│ └── first-step-cdk.test.ts
└── tsconfig.json

With that we have 2 important files:

  • bin/first-step-cdk.ts: This is the core of the project, here we must find the stacks defined in our project,
  • lib/first-step-cdk-stack.ts: This file represents only one stack.

Let’s go ahead and define Stack for a clear understanding. Stack is a collection of AWS resources that you can manage as a single unit. In other words, you can create, update, or delete a collection of resources by creating, updating, or deleting stacks.

2. Create the Lambda

We will follow the CDK documentation to create the resources, which can be found here in this document.

The following code define a lambda with these parameters:

  • functionName: Define the name of the lambda function,
  • description: The description of the lambda function,
  • memorySize: The memory ram used in the lambda (in Mb),
  • runtime: It can be the different versions and runtime from node, java, python, etc.,
  • entry: The place where the lambda code is hosted,
  • handler: The handle name which is the function defined in the entry file.

Once our resources are created and set up properly, we need to create the lambda code. Go ahead and create the file index.ts in the code/sum_numbers folder (the entry place).

Note: We need to install aws-lambda to get the event and return type.

Once you have created the lambda code, move forward with testing it by running cdk synth and go to cdk.out to see the template file created for CloudFormation.

2.1. Test lambda code (SAM required)

In order to test the code, run the following commands:

  • Run the command cdk synth --no-staging > template.yml,
  • Run the command sam local generate-event apigateway http-api-proxy > event.json,
  • In template.yml , you need to find the lambda id, usually it is before than Type: AWS::Lambda::Function . In my case, the id is lamdbaIdA52E7964,
  • In event.json, change the body key with something like that: "{ \"numbers\" : [1,2,3] }",
  • Now, run the command sam local invoke lamdbaIdA52E7964 -e event.json.

3. Create the API Gateway

As we continue to follow the CDK documentation to create the resources, (found here in this document) we see that the following code defines a Rest API:

To check if it is correct, run the command cdk synth.

4. Integrate the lambda with the API

Now, we are ready to integrate. Integration is easy, and we only need to create a LambdaIntegration, which is done by creating a resource with sum_numbers and a POST method with the lambda integrated.

5. Deploy

To deploy, we will need to configure AWS cli. You can accomplish this by following the tutorial here.

First, we need to create a helper Stack which works with CDK to upload the Lambda codes and CloudFormation template to S3. To do so, we need to run the command cdk bootstrap.

To finish and deploy the specified stack, run the command cdk deploy. Now you have an API with an endpoint connected to a Lambda. The API can be tested using Postman or in the console.

6. Clean

After testing the API, it is necessary to clean the deployed stack. To do this, run the command cdk destroy.

Conclusion

CDK is a powerful tool with the capability to deploy AWS architecture in a programmable way. This method is only the start, as you can create users, groups, roles, VPC, etc. Additionally, you can connect with other resources that were created before on your AWS console.

White Prompt has a number of case studies relevant to this blog topic, which can offer a few third-party examples. That way, you can gain a better sense of the type of clients we work with and how we work together. Take a minute to read our case study on Ashiwea, who presented us with a challenge to create a highly-customizable PAAS (Platofrm As A Service) to enhance the user experience and simplify the internal operations.

To conclude, I have a task, optional of course, to create a PR on this repository.

  • Add a plan to API,
  • Create a API Key to protect the endpoint,
  • Create test to the lambda,
  • Create a new endpoint which defines if a number is prime.

Further reading and links

We think and we do!

Do you have a business that requires an efficient and powerful data architecture to succeed? Get in touch with us at White Prompt, and we will make it happen!

--

--