Deploying Ruby AWS Lambda functions with CDK

Reading Time: 2 minutes

What is Cloud Development Kit (CDK)

It is an open-source tool created by the AWS team. It allows you to use the programming language of your choice ( TypeScript, JavaScript, Python, Java, C#/.Net, or Go ) to write infrastructure as code. It uses the Construct Programming Model (CPM) to generate CloudFormation templates and materializes them as AWS resources when deployed.

There are many more construct libraries to choose from at https://constructs.dev.

Prerequisites

  • Install & Configure AWS CLI

  • Install Cloud Development Kit (CDK)

Initializing an App

The following commands will initialize a new TypeScript CDK project, a git repo with the necessary directory/file structure, and install all libraries with npm. The output should look something like this:

Additionally, you can install dependencies using yarn to generate the yarn.lock file:

You will end up with this project structure:

Boostrapping

NOTE: This requires an AWS role with policy/AdministratorAccess access.

Bootstrapping will create an AWS CloudFormation stack with the resources needed before deploying any CDK Apps.

It will create IAM roles to grant permissions required to perform deployments, an S3 bucket to store CDK App assets, and the CloudFormation template files.

You will need to do this for each AWS region you want to deploy. I will use us-west-1, defined in my ~/.aws/config file.

Now, let’s run the cdk bootstrap command, and the output should be something like:

Creating a New Ruby Lambda Function

Our first step will be to create a new lambda function with a Ruby runtime; we can easily add that to our stack code with the following:

Now we need to create the function handler. If you look at the previous snippet, our function code attribute determines how CDK will package and load the function code. CDK will look into lib/lambda/ruby_function folder and create a .zip file with the content.

Additionally, the handler attribute will be the path to our ruby method file_name.class.method_name.

We are going to create that folder path mkdir -p lib/lambda/ruby_function and add our handler code in a file named function.rb:

cdk diff will tell you what resources would change between our updated App stack and what we currently have in AWS.

Ship it!

Let’s verify whether the new lambda function is created in AWS console or with AWS CLI:

Exposing Lambda Function with API Gateway

I have just created a new lambda function, but I do not know if it is working. I will add an API gateway and set the handler as our function.

Again, let’s see the diff and what resources it will create.

Once we are comfortable with the update, we can create a new deployment.

Time to deploy the update.

The result of our deploy is showing all the new resources created. Let’s verify our deployment. You know it: go to AWS console or with AWS CLI:

The result also shows the URL of our newly created API gateway; let’s make a request and see the response:

If you want to learn more about it, visit the CDK docs.

Thanks for reading.

0 Shares:
You May Also Like
Read More

Why Google Tag Manager exists?

Reading Time: 4 minutesGoogle Tag Manager (also known as GTM) is a free tag management solution provided by Google. Through this…
Read More

RegEx Basics Guide

Reading Time: 6 minutesThis guide is a quick compilation of many Regular Expresions examples and how to use them to be…