First we create a S3 bucket to store Terraform state, a DynamoDB table to persist Terraform state lock and a S3 bucket to deploy Lambda function code bundles. The Terraform state for this init stack is being kept locally.
cd infra/init
terraform init # Run once
terraform plan \
-var aws_region=<AWS_REGION>\
-var terraform_state_bucket=<S3_TERRAFORM_STATE_BUCKET_NAME> \
-var lambda_bucket=<S3_CODE_BUCKET_NAME> \
-out init.tfplan
terraform apply "init.tfplan"
cd bot
GOARCH=amd64 GOOS=linux go build -o bootstrap main.go
Note: In order to use the binary as the Lambda handler, it should be named bootstrap
.
Create a zip bundle from the built binary
zip lambda_function.zip bootstrap
Upload the build zip bundle to S3:
aws s3 cp lambda_function.zip s3://<S3_CODE_BUCKET_NAME>/lambda_function.zip
Create a file named infra/terraform.tfvars
:
aws_region = <AWS_REGION>
lambda_bucket = <S3_CODE_BUCKET_NAME>
bot_token = <TELEGRAM_BOT_TOKEN>
You can also pass these variables to terraform plan
command using multiple -var
options.
Create a Terraform backend configuration file named infra/backend_config.hcl
:
bucket = <S3_TERRAFORM_STATE_BUCKET_NAME>
region = <AWS_REGION>
Initialize the main Terraform stack:
cd infra
terraform init -backend-config="backend_config.hcl" # Run once
Use terraform plan
to create a changeset and run terraform apply
command to deploy the changeset on AWS:
cd infra
terraform plan -out main.tfplan
terraform apply "main.tfplan"
Get the API_GATEWAY_STAGE_INVOCATION_URL
from terraform output and register it as the webhook on telegram.
curl https://api.telegram.org/bot<BOT_TOKEN>/setWebhook \
-F "url=<API_GATEWAY_STAGE_INVOCATION_URL>
Use docker compose to run local DynamoDB and also a local development web server on port 8080:
export BOT_TOKEN=<BOT_TOKEN>
docker compose up -d
After applying a change in the bot code, to update the local bot in the docker container:
docker compose restart bot
Run a reverse proxy tool with a public secure API gateway on your local 8080 port.
For example using ngrok:
ngrok http 8080
Register the local bot on Telegram using the ngrok's forwarding endpoint:
curl https://api.telegram.org/bot<BOT_TOKEN>/setWebhook \
-F "url=<NGROK_FORWARDING_ENDPOINT>
You can see the incoming update requests sent by Telegram on ngrok's web interface: http://127.0.0.1:4040
.