In this example we are going to schedule a cron job that runs every minute to invoke a Lambda function. There won't be any business logic in the Lambda function but it will just log a few lines for the confirmation. If you are using Localstack, enable lambda,events services.


Create Lambda function


package main

import (
"context"
"fmt"

"github.com/aws/aws-lambda-go/lambda"
)

func main() {
lambda.Start(upload)
}

func upload(ctx context.Context, event interface{}) error {
fmt.Printf("%+v\n", event)
return nil
}

$ aws --profile localstack --endpoint-url http://localhost:4566 lambda delete-function --function-name hello-lambda

$ GOOS=linux CGO_ENABLED=0 go build -ldflags "-s -w" -o lambda main.go

$ zip lambda.zip lambda

$ aws --profile localstack --endpoint-url http://localhost:4566 lambda create-function --function-name hello-lambda --handler lambda --runtime go1.x --role create-role --zip-file fileb://lambda.zip

// This is just for the initial testing
$ aws --profile localstack --endpoint-url http://localhost:4566 lambda invoke --function-name hello-lambda response.txt

$ aws --profile localstack --endpoint-url http://localhost:4566 lambda list-functions
{
"Functions": [
{
"FunctionName": "hello-lambda",
"FunctionArn": "arn:aws:lambda:eu-west-1:000000000000:function:hello-lambda",
"Runtime": "go1.x",
"Role": "create-role",
"Handler": "lambda",
"CodeSize": 2379857,
"Description": "",
"Timeout": 3,
"LastModified": "2022-01-01T20:01:59.832+0000",
"CodeSha256": "BF5bQUYo7nqWtuIKOXpQ851JdXwbi7zuS4NMT9LPTCg=",
"Version": "$LATEST",
"VpcConfig": {},
"TracingConfig": {
"Mode": "PassThrough"
},
"RevisionId": "9b8f072c-e5e5-49c0-8121-519c37265495",
"State": "Active",
"LastUpdateStatus": "Successful",
"PackageType": "Zip"
}
]
}

Create cron event rule


$ aws --profile localstack --endpoint-url http://localhost:4566 events put-rule --name hello-cron-rule --schedule-expression "cron(* * * * *)"

$ aws --profile localstack --endpoint-url http://localhost:4566 events list-rules
{
"Rules": [
{
"Name": "hello-cron-rule",
"Arn": "arn:aws:events:eu-west-1:000000000000:rule/hello-cron-rule",
"State": "ENABLED",
"ScheduleExpression": "cron(* * * * *)",
"EventBusName": "default"
}
]
}

Link Lambda function to cron rule


$ aws --profile localstack --endpoint-url http://localhost:4566 events put-targets --rule hello-cron-rule --targets "Id"="1","Arn"="arn:aws:lambda:eu-west-1:000000000000:function:hello-lambda"

$ aws --profile localstack --endpoint-url http://localhost:4566 events list-targets-by-rule --rule hello-cron-rule
{
"Targets": [
{
"Id": "1",
"Arn": "arn:aws:lambda:eu-west-1:000000000000:function:hello-lambda"
}
]
}

Confirmation


This is what you should see in logs every minute.


Lambda arn:aws:lambda:eu-west-1:000000000000:function:hello-lambda result / log output:
localstack | null
localstack | > START RequestId: 256293c4-92d4-113b-1a27-6aade44c845e Version: $LATEST
localstack | > CRON TIME: 2022-01-01T20:12:49Z
localstack | > END RequestId: 256293c4-92d4-113b-1a27-6aade44c845e
localstack | > REPORT RequestId: 256293c4-92d4-113b-1a27-6aade44c845e Init Duration: 124.14 ms Duration: 9.74 ms Billed Duration: 10 ms Memory Size: 1536 MB Max Memory Used: 19 MB