Telegram bots offer a fast and efficient way to prototype applications without requiring custom UI or authentication. They can be deployed on a virtual machine to maintain state or in a serverless environment. A serverless Telegram bot hosted on AWS Lambda benefits from 1 million free compute requests and supports concurrent execution, unlike stateful bots on VMs with limited compute capacity. However, configuring a serverless setup can be time-consuming.
This template simplifies the deployment of a serverless Telegram bot, providing a solid foundation for users familiar with Infrastructure as Code (IaC) and AWS. It also serves as a quick-start guide for those looking to deploy a simple Telegram bot. However, extensive customization may require additional onboarding to gain familiarity with Terraform.
- Terrform - refer to installation guide.
- AWS Account credentials see AWS Build infrastructure tutorial.
- AWS_ACCESS_KEY_ID
- AWS_SECRET_ACCESS_KEY
- Create an AWS Bucket to store terraform state file (Note the bucket name)
- TELEGRAM_BOT_TOKEN - Generated from bot father see section on obtaining bot token.
- TELEGRAM_BOT_NAME - The name of your telegram bot to be used for naming AWS infrastructure.
This section explains how to get from an AWS token and telegram the ensure pre-requisites are met
The requirements.txt file will be referenced during infrastructure deployment to ensure required dependencies are present in lambda layers.
Navigate to source directory, install and activate virtual environment
cd source
pip3 -m venv venv
source venv/bin/activate
Install initial dependencies and any additional dependencies into venv
`pip3 install -r requirements.txt`
`pip3 install <your dependencies>`
Export all dependencies requirements.txt
`pip3 freeze > requirement.txt`
- Export AWS credentials to terminal
- Replace state file reference in
main.tf - Create a new file by name
terraform.tfvarsand populate variablesbot_tokenandbot_name. Seeterraform.tfvars.examplefor reference. terraform initandterraform apply. Optionally runterraform validateandterraform planto verify config before apply.
This section explains the elements of the template to aid in providing context required to perform customisations.
Terrform provider configuration with remote state file definitions.
prepare_layers.sh - bash script to install AWS Linux compatible python depedencies for lambda layers
register_telebot.sh - bash script to set webhook enabling telegram server to foward messaged send to our bot to our API Gateway
Invokes the scripts in above section on scripts based on lifecycle triggers. Only runs layers script if there is a change in requirements.txt
API gateway paths configured to invoke lambda function.
Lambda layers, function and role configuration to execute source code. Update lambda role if you plan to integrate lambda other AWS services (eg. DynamoDB).
Script to package source code and dependencies requireed for lambda source and layers. Source code packaging excludes venv folder. Update file if your virtual environment is of a different name.
