An Example of Simplifying Local Resource Deployment with AWS CDK and LocalStack

An Example of Simplifying Local Resource Deployment with AWS CDK and LocalStack

Hello everyone, this is Edelweis from the Systems Department.
It has been more than six months since I moved from the Philippines to Japan to join GMO Research. The past six months have been very productive as it helped me improve my technical and even a little bit of Japanese skills.

In the past, I have worked with infrastructure creation and maintenance. It has always been my top priority to make deployment and maintenance as easy as possible. I also want other people to get to know the importance of understanding the infrastructure of a project.

From this, I came across AWS Cloud Development Kit (CDK) and Localstack. AWS CDK works like AWS CloudFormation template, but it does not necessarily need for user to remember complex functions or templates of CloudFormation. This makes management easier. Moreover, if the user has a clear knowledge of a specific programming language it would be easy to deploy resources.

You may refer to AWS CDK and Localstack official page for details of these tools. In this article, I will show how I installed and used the tools.

Using Localstack and AWS CDK I was able to deploy an API which I can use for study or investigation. Below are the steps I took to successfully do this.

Basic Design/Architecture

The design for this report is very simple and straightforward. We will only use API Gateway and Lambda to create a sample REST API.

Environment Construction (Windows)

Localstack

1.     Install localstack using python

2.     Start localstack

3.     Check localstack status

Successful installation and start of localstack will result to the following:

AWS CDK

To install and use AWS CDK all we need is to create a new and empty directory and execute the cdk commands.

1.     Create new and empty directory

2.     Install cdk

3.     Initialize cdklocak

For this article, I used python as my programming language. AWS CDK also supports programming languages like Typescript, Javascript, Java, C#, and Go. This also makes this tool very easy to use for everyone.
The command cdklocal init will initialize the directory where we can start creating our resources.
We will edit one file and create a new file in order to create resources.

Creating AWS Resources (Lambda and API Gateway)

Create the lambda code

Create a new directory and new file for the lambda handler.
For this project, we will deploy a very simple lambda which returns “Hello World!” every time it is executed.

File: src/lamba_handler.py

Update the stack to create the lambda function and integrate the lambda function with the API Gateway.

File: cdk_techblog/cdk_techblog_stack.py

In the above code, we want to create a lambda function in Python named 'HelloWorldLambda' and an API Gateway RestAPI named  HelloWorldApi. The API Gateway also has a resource named test.

Once the code for the resources is completed, we are ready to deploy.

Deploying Resources to Localstack

 1.     Run cdk bootstrap

This command will create an AWS S3 Bucket which AWS CDK will use to deploy the stack. This command is done only once or first time deployment.
To verify that the bucket is successfully created, run the following localstack to check.

This should output the S3 Bucket created like

2.     Deploy Command

Successful deployment will show the stacks created as well as confirmation that deployment was successful.

That’s it. Using just a few commands, we were able to deploy our AWS Resources in Localstack. However, we still want to check whether the resources work like how we want them to be. So let’s test it.

Testing of Deployed Resources

In order to verify that the resources are deployed. We will use the curl command.

Using the aws cli we will get the values in order to complete the URL.

1.     <rest_api_id>

From the output above, we can retrieve

This value will then be used in succeeding commands.

2.     <stage_name>

From output above we can use

3.     <resource_name>

From output above we can use

Now that we have all the values to complete the URL. We will execute the curl command.

Successful response displays “Hello World” on the screen.

That’s it! We were able to confirm that the resources are deployed and connected.

Conclusion

With the increased demand for cloud technologies nowadays it is ideal for us developers to understand how we can use, deploy, and maintain infrastructure. In this article, I showed how to use two of the easiest and well-known tools for deployment that are AWS CDK and Localstack. From this, we can explore more of the advanced and new tools that are coming out. With the help of these simple tools I hope everyone can have a wider understanding of how important knowledge of infrastructure is for a project.

That is all, thank you! 😀

【References】

  1. What is the AWS CDK?
  2. localstack.cloud
  3. apigatewayーAWS CLI Command Reference
前の記事
«
次の記事
»

技術カテゴリの最新記事