Welcome to the first chapter of the series "Serverless Telegram Bot." In this chapter, we set up our environment in AWS using CDK to deploy a Lambda function. In the upcoming chapters, we'll learn to build a Telegram bot, trigger the Lambda with Telegram messages, and leverage AWS's serverless products to create a scalable project.
Last week I was searching for a good expense tracking app, and after trying a couple, they all felt overly complicated—I just wanted a quick and easy way to note my expenses and have them stored somewhere.
So, I came up with the idea of using Telegram, an app I use daily, which allows you to easily create bots to perform any task you need.
If you ever had the idea of creating something similar, you probably notice the bot needs to be deployed somewhere. But like me, you probably don't want to spend too much time on it, or spend any money at all.
AWS Lambda functions to the rescue ! They offer a free tier with 1 million requests per month, which should be enough for an average person's needs. And setting them up is quite easy, we'll do it with CDK.
What's AWS Lambda ?
AWS Lambda is a service that lets you run code in response to events. They are really just functions in your favorite language, uploaded to AWS, triggered manually or when an event occurs.
Our plan here is to trigger our Lambda function whenever a user sends a Telegram message to our bot.
What's CDK ?
The AWS Cloud Development Kit (CDK) is a tool that lets you define your cloud infrastructure using code. Instead of manually setting up AWS resources, you can write your infrastructure as code in languages like TypeScript, Python, or Java. This code is then turned into a CloudFormation template, which AWS uses to create and manage your resources.
With CDK, we'll write the code that defines our Lambda function, and AWS will handle the setup for us.
Pre-requisites
You will need:
- Node.js installed
- An AWS account
- AWS CLI configured
Configure AWS CLI
If you've never used AWS before, you can easily setup an account, but configuring the CLI might be new to you. I recommend you look for another guide that tackles that part in detailed, but I'll give you a super quick overview to get you started.
Fist, install the AWS CLI by following this documentation.
Next, go to the AWS Management Console, navigate to IAM > Security Credentials > Access Keys > Create access key > Command Line Interface (CLI).
Once you got your credentials, run
aws configure
Enter your credentials when prompted, and you should be all set!
Setting up our workspace
To start, open a new folder called "telegram-bot-cdk" in your terminal, and run the following CDK command:
npx cdk init app --language [language]
You can set it up with several languages, but I'll be using Typescript, so:
npx cdk init app --language typescript
`
This will create an initial folder structure that should look like this:
The bin folder
This folder contains the entry point for CDK. The file there will be executed when we build and deploy our infrastructure
The lib folder
This folder contains our Stacks. Stacks include all the resources that will be deployed to AWS. Resources in a single Stack will be deployed together; they don't necessarily need to depend on each other, but ideally, we should group our infrastructure resources in a cohesive way using Stacks.
For now we will just use the default Stack generated by CDK.
Adding our Lambda
An AWS Lambda is a resource, so we should add it to a Stack. Let's modify the CDK stack to deploy a Lambda.
Open the telegram-bot-cdk-stack.ts
file inside the lib folder, and add the following code:
This is what I mean by creating AWS resources with code. By constructing the NodeJsFunction
class, we are instructing CDK to add a new Lambda function to our AWS account. The entry
path specifies the location of the code that we want our Lambda to execute.
Lambda code
Now, the entry
field is pointing to a file that doesn't exist yet, so let's create it. Create a new folder src
and inside another one called handlers
. Finally create the file messageHandler.ts
.
The only requirement for our Lambda is to export a function called handler
. This function will be executed when our Lambda is triggered.
NOTE: The handler function must return a Promise, an option is to making it async
This is the simplest Lambda handler, it will just return a Hello World to whoever triggers it.
Triggering our Lambda
Now you may be wondering, how are we going to trigger our Lambda? Ideally we would like to trigger it when someone sends us a message on Telegram, but for now, we will trigger it manually through a URL.
So, to make the Lambda public, we can add a Function URL to the Lambda. A Function URL is just an HTTPS endpoint that AWS creates for us to access our Lambdas.
Adding a Function URL
Because this is a infrastructure related change, we need to modify our stack. Let's quickly add this command to the default stack.
It's as simple as calling addFunctionUrl
in our Lambda object, and that's it, AWS will assign an HTTPS endpoint to our Lambda.
We won't know the exact URL until the Lambda is deployed, which is why we set it as an output, so that the final URL gets printed in our console.
Deploying to AWS
Now that we have everything set up ! We are ready to deploy our Lambda to AWS.
Run the following commands at the root of the project. If this is the first time you use CDK, then run:
npx cdk bootstrap
This will deploy a CDK toolkit to your AWS account. Once it's there, you won't need to do it again.
Then just do:
npx cdk deploy
You'll notice that it begins bundling our resources, generating a CloudFormation template, and then deploying it.
AWS recommends following the Principle of Least Privilege, which can be summarized as "Providing just enough access to make you app work—no more, no less".
CDK will take care of providing just the right access to your Lambda, and it will ask you to confirm every policy related change with a message like the following:
In our case, you can safely accept those policies.
Our CDK Hello World !
After CDK deploys your resources, you will see that it outputs the Function URL we created for our Lambda:
Feel free to open that URL and you will see your Lambda in action !
Conclusion
In this chapter, we successfully created and deployed an AWS Lambda function using CDK. CDK is one of the best options when you want to deploy AWS resources because it allows you to define your infrastructure as code, making deployments repeatable, scalable, and easy to manage.
In the next chapters we are going to learn how to test our Lambda functions locally and then start building our Telegram bot. By the end of this series, you will know how to create scalable projects with AWS leveraging their Serverless products.
Stay tuned for next chapters !
Top comments (0)