amazon-web-servicesgoaws-lambdatelegram-bot

AWS Lambda Golang Timeout Issue When Sending Automated Telegram Birthday Messages


So i developed a bot for telegram to send birthday messages. It worked well for 2 months on EC2 instance now i want to move it to AWS Lambda and run it once a day i read some and deployed it but it always give me timeouts example error

Task timed out after 60.02 seconds

this is the main.go

package main

import (
    "context"
    "encoding/json"
    "fmt"
    "log"
    "strconv"

    "github.com/aws/aws-lambda-go/lambda"
    "github.com/aws/aws-sdk-go-v2/config"
    "github.com/aws/aws-sdk-go-v2/service/secretsmanager"
    "github.com/aws/aws-sdk-go/aws"

    tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)

type Secrets struct {
    BotAPIKey        string `json:"BOT_API_KEY"`
    ChatID           string `json:"CHAT_ID"`
    ConfluenceAPIKey string `json:"CONFLUENCE_API_KEY"`
}

func getSecrets() (*Secrets, error) {
    secretName := "HIDDEN"
    region := "eu-west-1"

    config, err := config.LoadDefaultConfig(context.TODO(), config.WithRegion(region))
    if err != nil {
        return nil, err
    }

    svc := secretsmanager.NewFromConfig(config)

    input := &secretsmanager.GetSecretValueInput{
        SecretId:     aws.String(secretName),
        VersionStage: aws.String("AWSCURRENT"),
    }

    result, err := svc.GetSecretValue(context.TODO(), input)
    if err != nil {
        return nil, err
    }

    var secrets Secrets
    err = json.Unmarshal([]byte(*result.SecretString), &secrets)
    if err != nil {
        return nil, err
    }

    return &secrets, nil
}

func initializeBot(botAPIKey string) (*tgbotapi.BotAPI, error) {
    bot, err := tgbotapi.NewBotAPI(botAPIKey)
    if err != nil {
        return nil, err
    }
    bot.Debug = true
    fmt.Println("im here")
    return bot, nil
}

func handleRequest() error {
    log.Println("Starting handleRequest")

    secrets, err := getSecrets()
    if err != nil {
        log.Printf("Error getting secrets: %v", err)
        return err
    }

    bot, err := initializeBot(secrets.BotAPIKey)
    if err != nil {
        log.Printf("Error initializing bot: %v", err)
        return err
    }

    log.Printf("Authorized on account %s", bot.Self.UserName)

    chatID, err := strconv.ParseInt(secrets.ChatID, 10, 64)
    if err != nil {
        log.Printf("Error converting chatID to int64: %v", err)
        return err
    }

    scheduleBirthdayMessages(bot, chatID, secrets.ConfluenceAPIKey)
    return nil
}

func main() {
    lambda.Start(handleRequest)

}

The functions spreads across multiple files in the project but they all work well, i think i'm doing something wrong with the handle

also the way i deploy it aws is by building it and zipping it

CGO_ENABLED=0 go build -o bootstrap
zip lambda-handler.zip bootstrap

Solution

  • Turn on AWS X-Ray for your Lambda Function.

    Check Instrumenting Go code in AWS Lambda documentation. Once done, invoke your function several times so Traces are recorded.

    Go to Lambda Function Monitoring tab, scroll down and look for Traces. Select one and look for Segments Timeline. This should give you more details about your code execution.