用cron规则定期调用Golang AWS Lambda函数的指南

335 阅读1分钟

在这个例子中,我们将安排一个cron job,每分钟运行一次,调用一个Lambda函数。在Lambda函数中不会有任何业务逻辑,但它将只是记录几行以进行确认。如果你正在使用Localstack,请启用lambda,events 服务。

创建Lambda函数

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"
        }
    ]
}

创建cron事件规则

$ 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"
        }
    ]
}

将Lambda函数链接到cron规则

$ 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"
        }
    ]
}

确认

这是你应该在每分钟的日志中看到的内容:

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