DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 966,904 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Cover image for AWS CDK - Building Telegram bot with AWS Lambda and API Gateway Proxy Integration - Part 1
Akhilesh Yadav for AWS Community Builders

Posted on

AWS CDK - Building Telegram bot with AWS Lambda and API Gateway Proxy Integration - Part 1

This is the first part of the Building Telegram bot with AWS Lambda and API Gateway Proxy Integration Series and this article would feel easy incase you come from Cloudformation or Terraform background. For beginners, this would make more sense if you follow this article along with AWS CDK Official Guide.

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

It means that now you can use TypeScript, JavaScript, Python, Java, or C# to create Infrastructure instead of JSON/YAML used in Cloudformation template.

Advantage of AWS CDK over Cloudformation:

  • Use logic (if statements, for-loops, etc) when defining your infrastructure
  • Use object-oriented techniques to create a model of your system
  • Define high level abstractions, share them, and publish them to your team, company, or community
  • Organize your project into logical modules
  • Share and reuse your infrastructure as a library
  • Testing your infrastructure code using industry-standard protocols
  • Use your existing code review workflow Code completion within your IDE

With AWS CDK you can design your own reusable components that meet your organization’s security, compliance, and governance requirements. It also provides high-level components that preconfigure cloud resources with proven defaults, helping you build on AWS without needing to be an expert.

Which means Construct props can have default values for some key as mentioned in document for API-Gateway Stage. Do check the Construct Props section and scroll down to see default values for some properties.

https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-apigateway.Stage.html

new Stage(scope: Construct, id: string, props: StageProps)
Enter fullscreen mode Exit fullscreen mode

This is part 1 of building Telegram bot using AWS SDK Series.

The AWS CDK Toolkit provides the cdk command-line interface that can be used to work with AWS CDK applications.

How to setup AWS CDK Project

https://docs.aws.amazon.com/cdk/latest/guide/getting_started.html

  • Install CDK using Nodejs Package Manager

    • Lets install : npm install -g aws-cdk
    • Check version using : cdk --version
  • Configure AWS Credential on your local Machine

    • Copy and Paste Access Keys and Secret Keys : aws configure
    • Confirm if you gained the AWS Identity. aws sts get-caller-identity

Many AWS CDK stacks that you write will include assets: external files that are deployed with the stack, such as AWS Lambda functions or Docker images. The AWS CDK uploads these to an Amazon S3 bucket or other container so they are available to AWS CloudFormation during deployment. Deployment requires that these containers already exist in the account and region you are deploying into. Creating them is called bootstrapping.

To bootstrap, paste this :

cdk bootstrap aws://$(aws sts get-caller-identity --output text --query Account)/$(aws configure get region)
Enter fullscreen mode Exit fullscreen mode
mkdir telegram-bot
cd telegram-bot
Enter fullscreen mode Exit fullscreen mode

We will create separate directory for managing aws-cdk file and lambda file.

mkdir cdk-tool
mkdir lambda
Enter fullscreen mode Exit fullscreen mode

Now go to cdk-tool empty directory and initialize the app using the cdk init command, specifying the desired template ("app") and programming language. That is:

cd cdk-tool
cdk init app --language javascript
Enter fullscreen mode Exit fullscreen mode

CDK Installation

Default directory structure would look like this:

CDK Directory Structure

  • lib/cdk-tool-stack.js : is where your CDK application's main stack is defined. You will write the infrastructure configuration over here in this file.
  • bin/cdk-tool.js : This is the entrypoint of the CDK application. It will load the stack defined in lib/cdk-tool-stack.js
  • package.json : is your npm module manifest. It includes information like the name of your app, version, dependencies and build scripts.
  • cdk.json: tells the toolkit how to run your app.
  • .gitignore and .npmignore tell git and npm which files to include/exclude from source control and when publishing this module to the package manager.
  • node_modules is maintained by npm and includes all your project’s dependencies.

Creating Sample Lambda Function

Do you remember you created one more directory to store lambda function, ie. lambda directory. Lets switch to that directory from parent directory and create index.js file into telegram-bot directory containing nodejs function sourcecode.

cd lambda
mkdir telegram-bot
touch index.js
Enter fullscreen mode Exit fullscreen mode

Paste this into index.js file.

exports.handler = async function(event) {
    console.log("request:", JSON.stringify(event, undefined, 2));
    return {
      statusCode: 200,
      headers: { "Content-Type": "text/plain" },
      body: `Hello, CDK! You've hit ${event.path}\n`
    };
  };
Enter fullscreen mode Exit fullscreen mode

Now, lets go back to cdk-tool directory from parent directory and link the lambda function sourcecode with AWS Lambda Construct and later bind it with API Gateway route.

Before that, you would need to install lambda and apigateway cdk constructs using

npm install @aws-cdk/aws-lambda
npm install @aws-cdk/aws-apigateway
Enter fullscreen mode Exit fullscreen mode

Now move to cdk-tool directory :

cd cdk-tool

Go to lib/cdk-tool-stack.js and paste this imports at top:

const lambda = require("@aws-cdk/aws-lambda");
const apigw = require("@aws-cdk/aws-apigateway");
Enter fullscreen mode Exit fullscreen mode

A Lambda CDK construct would look lik below.

    const lambdaTelegram = new lambda.Function(this, "telegramBotHandler", {
      runtime: lambda.Runtime.NODEJS_14_X,
      handler: "index.handler",
      code: lambda.Code.fromAsset("../lambda/telegram-bot"), // from parent directory containing package.json
      architecture: lambda.Architecture.ARM_64,
      environment: {
        'CURRENT_ENV': 'dev',
      }
    });
Enter fullscreen mode Exit fullscreen mode

As seen above, we have mapped lamba sourcecode using lambda.Code.fromAsset and have setup runtime environment, entry of the function, architecture and Lambda Environment variables.

We would also require API Gateway Proxy integration to map it with this lambda function and get invokable API Link using apigw.RestApi

    const restApi = new apigw.RestApi(this, "telegrambot-api", { deploy: false });
Enter fullscreen mode Exit fullscreen mode

Create a new resource path and setup GET method with it and attach it with lambda function construct.

    const method = restApi.root
      .addResource("bot")
      .addMethod("GET", new apigw.LambdaIntegration(lambdaTelegram, { proxy: true }));
Enter fullscreen mode Exit fullscreen mode

Since, we have passed { deploy: false } inside api.RestApi construct props, we would need to create manual deployment and stage by ourselves. Incase, if you do not pass that option, it would create deployment and default prod stage for you and you can ignore next 2 snippet as well.

    const devDeploy = new apigw.Deployment(this, "dev-deployment", { api: restApi });
Enter fullscreen mode Exit fullscreen mode

Creating our own Stage with customized properties using apigw.Stage

    const devStage = new apigw.Stage(this, "devStage", {
      deployment: devDeploy,
      stageName: 'dev' // If not passed, by default it will be 'prod'
    });
Enter fullscreen mode Exit fullscreen mode

Emitting the output values after deployment using cdk.CfnOutput.

    new cdk.CfnOutput(this, "BotURL", {
      value: `https://${restApi.restApiId}.execute-api.${this.region}.amazonaws.com/dev/bot`,
    });
Enter fullscreen mode Exit fullscreen mode

Final code of lib/cdk-tool-stack.js should look as below.

Now, lets validate the written constructs and see if it generates valid cloudformation template.

Enter command in root directory

cdk synth
and then you will see some new directories in your folder structure which contains cloudformation templates and metadata.
CDK Synth

Lets deploy this application using:

cdk deploy
CDK Deploy start
You can see in above screenshot with resources that would be created. Confirm by pressing y.
CDK Deploy done

Now, call the API on browser and check the response.
Image description

To destroy the application:

cdk destroy

CDK Destroy

Confirm deletion by pressing y.
CDK Destroyed

You can find the sourcecode on Git repository at :
https://github.com/arki7n/aws-cdk-telegram-bot-example.git

That's it! Hope you have get to complete all of the above steps and achieved the desired output. You can customize the lambda function source code and experiment things on your own as well. I shall be back with part 2 of this series in next blog post. Feel free to follow me to remain updated with next part 2 post.

Top comments (0)

Classic DEV Post from 2020:

js visualized

πŸš€βš™οΈ JavaScript Visualized: the JavaScript Engine

As JavaScript devs, we usually don't have to deal with compilers ourselves. However, it's definitely good to know the basics of the JavaScript engine and see how it handles our human-friendly JS code, and turns it into something machines understand! πŸ₯³

Happy coding!