DEV Community

Cover image for Running Cron Jobs with Go on AWS Lambda
Rodrigo Burgos
Rodrigo Burgos

Posted on

Running Cron Jobs with Go on AWS Lambda

Prerequisites

  • AWS account
  • AWS CLI installed and configured
  • Go installed on your development machine

Step 1: Create a Lambda Execution Role

First, we need to create an IAM role with the necessary permissions for our Lambda function.

1 - Create a file named trust-policy.json with the following content:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "Service": "lambda.amazonaws.com"
            },
            "Action": "sts:AssumeRole"
        }
    ]
}
Enter fullscreen mode Exit fullscreen mode

2 - Create the IAM role using the AWS CLI:

aws iam create-role --role-name lambda-execute-role --assume-role-policy-document file://trust-policy.json

Enter fullscreen mode Exit fullscreen mode

3 - Attach the AWSLambdaBasicExecutionRole policy to the role:

aws iam attach-role-policy --role-name lambda-execute-role --policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
Enter fullscreen mode Exit fullscreen mode

Step 2: Create the Lambda Function with Go

1 - Write your Go code. For this tutorial, let's create a simple function that logs a message. Create a file named main.go:

package main

import (
    "github.com/aws/aws-lambda-go/lambda"
    "log"
)

func handler() {
    log.Println("Lambda function invoked!")
}

func main() {
    lambda.Start(handler)
}
Enter fullscreen mode Exit fullscreen mode

2 - Build the Go binary for Linux:

GOOS=linux GOARCH=amd64 go build -o bootstrap main.go
Enter fullscreen mode Exit fullscreen mode

3 - Zip the binary:

zip function.zip bootstrap
Enter fullscreen mode Exit fullscreen mode

4 - Create the Lambda function using the AWS CLI:

aws lambda create-function --function-name new-pairs-binance \
    --zip-file fileb://function.zip --handler bootstrap \
    --runtime provided.al2 --role arn:aws:iam::AWS-ID:role/lambda-execute-role
Enter fullscreen mode Exit fullscreen mode

Step 3: Create a CloudWatch Event Rule

1 - Create a CloudWatch Event rule to trigger the Lambda function once a day:

aws events put-rule --name DailyTrigger --schedule-expression "rate(1 day)"
Enter fullscreen mode Exit fullscreen mode

2 - Add permission for CloudWatch to invoke your Lambda function:

aws lambda add-permission --function-name new-pairs-binance \
    --statement-id DailyTrigger --action 'lambda:InvokeFunction' \
    --principal events.amazonaws.com --source-arn arn:aws:events:us-east-1:AWS-ID:rule/DailyTrigger
Enter fullscreen mode Exit fullscreen mode

3 - Add the Lambda function as a target to the CloudWatch rule:

aws events put-targets --rule DailyTrigger --targets "Id"="1","Arn"="arn:aws:lambda:us-east-1:601139476230:function:func-name"
Enter fullscreen mode Exit fullscreen mode

Step 4: Verify the Setup

1 - Manually invoke the Lambda function to ensure it's working correctly:

aws lambda invoke --function-name new-pairs-binance out.txt
Enter fullscreen mode Exit fullscreen mode

Check the contents of out.txt for any errors or output from your Lambda function.

2 - Check CloudWatch Logs to see if the log group has been created and logs are available:

aws logs describe-log-streams --log-group-name /aws/lambda/new-pairs-binance
Enter fullscreen mode Exit fullscreen mode

If the log group does not exist, it might mean the function hasn't run yet. After manually invoking the function, the log group should be created, and you can check the logs again.

Conclusion
You now have a Lambda function written in Go that runs once a day, triggered by a CloudWatch Event rule. This setup is useful for running scheduled tasks without needing to manage a server.

Feel free to extend this tutorial with more complex logic in your Go function or add more CloudWatch rules for different schedules.

Top comments (1)

Collapse
 
defikanth profile image
Lakshmi kanth

A nice and good start for a go developer to go aws cloud. Will execute this today thanks for the blog