3

我正在使用此docker-compose.yml文件在 docker 容器内运行 localstack。

version: '2.1'

services:
  localstack:
    image: localstack/localstack
    ports:
      - "4567-4597:4567-4597"
      - "${PORT_WEB_UI-8080}:${PORT_WEB_UI-8080}"
    environment:
      - SERVICES=${SERVICES- }
      - DEBUG=1
      - DATA_DIR=${DATA_DIR- }
      - PORT_WEB_UI=${PORT_WEB_UI- }
      - LAMBDA_EXECUTOR=docker
      - KINESIS_ERROR_PROBABILITY=${KINESIS_ERROR_PROBABILITY- }
      - DOCKER_HOST=unix:///var/run/docker.sock
    volumes:
      - "${TMPDIR:-/tmp/localstack}:/tmp/localstack"
      - "/var/run/docker.sock:/var/run/docker.sock"

要启动 localstack,我运行TMPDIR=/private$TMPDIR docker-compose up.

我创建了两个 lambda。当我运行时,aws lambda list-functions --endpoint-url http://localhost:4574 --region=us-east-1这是输出。

{
"Functions": [
    {
        "TracingConfig": {
            "Mode": "PassThrough"
        },
        "Version": "$LATEST",
        "CodeSha256": "qmDiumefhM0UutYv32By67cj24P/NuHIhKHgouPkDBs=",
        "FunctionName": "handler",
        "LastModified": "2019-08-08T17:56:58.277+0000",
        "RevisionId": "ffea379b-4913-420b-9444-f1e5d51b5908",
        "CodeSize": 5640253,
        "FunctionArn": "arn:aws:lambda:us-east-1:000000000000:function:handler",
        "Environment": {
            "Variables": {
                "DB_NAME": "somedbname",
                "IS_PRODUCTION": "FALSE",
                "SERVER": "xxx.xxx.xx.xxx",
                "DB_PASS": "somepass",
                "DB_USER": "someuser",
                "PORT": "someport"
            }
        },
        "Handler": "handler",
        "Role": "r1",
        "Timeout": 3,
        "Runtime": "go1.x",
        "Description": ""
    },
    {
        "TracingConfig": {
            "Mode": "PassThrough"
        },
        "Version": "$LATEST",
        "CodeSha256": "wbT8YzTsYW4sIOAXLtjprrveq5NBMVUaa2srNvwLxM8=",
        "FunctionName": "paymentenginerouter",
        "LastModified": "2019-08-08T18:00:28.923+0000",
        "RevisionId": "bd79cb2e-6531-4987-bdfc-25a5d87e93f4",
        "CodeSize": 6602279,
        "FunctionArn": "arn:aws:lambda:us-east-1:000000000000:function:paymentenginerouter",
        "Environment": {
            "Variables": {
                "DB_QUERY_LAMBDA": "handler",
                "AWS_REGION": "us-east-1"
            }
        },
        "Handler": "handler",
        "Role": "r1",
        "Timeout": 3,
        "Runtime": "go1.x",
        "Description": ""
    }
]

}

paymentenginerouter代码中,我试图handler通过以下方式调用 lambda:

    lambdaParams := &invoke.InvokeInput{
    FunctionName:   aws.String(os.Getenv("DB_QUERY_LAMBDA")),
    InvocationType: aws.String("RequestResponse"),
    LogType:        aws.String("Tail"),
    Payload:        payload,
}
result, err := svc.Invoke(lambdaParams)
if err != nil {
    resp.StatusCode = 500
    log.Fatal("Error while invoking lambda:\n", err.Error())
}

invoke进口在哪里:invoke "github.com/aws/aws-sdk-go/service/lambda"

当我通过以下方式运行paymentenginerouterlambda 时:

aws lambda invoke --function paymentenginerouter --payload  '{ "body": "{\"id\":\"12\",\"internalZoneCode\":\"xxxxxx\",\"vehicleId\":\"xxxxx\",\"vehicleVrn\":\"vehicleVrn\",\"vehicleVrnState\":\"vehicleVrnState\",\"durationInMinutes\":\"120\",\"verification\":{\"Token\":null,\"lpn\":null,\"ZoneCode\":null,\"IsExtension\":null,\"ParkingActionId\":null},\"selectedBillingMethodId\":\"xxxxxx\",\"startTimeLocal\":\"2019-07-29T11:36:47\",\"stopTimeLocal\":\"2019-07-29T13:36:47\",\"vehicleVin\":null,\"orderId\":\"1\",\"parkingActionType\":\"OnDemand\",\"digitalPayPaymentInfo\":{\"Provider\":\"<string>\",\"ChasePayData\":{\"ConsumerIP\":\"xxxx\",\"DigitalSessionID\":\"xxxx\",\"TransactionReferenceKey\":\"xxxx\"}}}"
}' --endpoint-url=http://localhost:4574 --region=us-east-1 out --debug

我收到此错误:

localstack_1  | 2019/08/08 20:02:28 Error while invoking lambda:
localstack_1  | UnrecognizedClientException: The security token included in the request is invalid.
localstack_1  |     status code: 403, request id: bd4e3c15-47ae-44a2-ad6a-376d78d8fd92

笔记

可以handler通过 cli 直接调用它来运行lambda 而不会出错:

aws lambda invoke --function handler --payload '{"body": "SELECT TOKEN, NAME, CREATED, ENABLED, TIMESTAMP FROM dbo.PAYMENT_TOKEN WHERE BILLING_METHOD_ID='xxxxxxx'"}' --endpoint-url=http://localhost:4574 --region=us-east-1 out --debug

我认为 AWS 凭证是根据 localstack 中的环境变量设置的,但我可能弄错了。知道如何解决这个问题吗?

我对 AWS lambdas 非常陌生,并且在 localstack 方面绝对是菜鸟,所以如果您需要它们,请询问更多详细信息。我的描述中可能遗漏了一条关键信息。

4

1 回答 1

8

您收到的错误The security token included in the request is invalid.意味着您的 lambda 正在尝试使用无效凭证调用真正的 AWS,而不是转到 Localstack。

在 localstack 中运行 lambda 并且 lambda 代码本身必须调用 localstack 中托管的任何 AWS 服务时,您需要确保使用的任何端点都重定向到 localstack。

为此,请在此处找到 localstack 中的文档功能:

https://github.com/localstack/localstack#configurations

LOCALSTACK_HOSTNAME:LocalStack 服务可用的主机名。为了从您的 Lambda 函数中访问服务(例如,将项目从 Lambda 存储到 DynamoDB 或 S3),这是必需的。变量 LOCALSTACK_HOSTNAME 可用于本地 Lambda 执行 (LAMBDA_EXECUTOR=local) 和在单独的 Docker 容器内执行 (LAMBDA_EXECUTOR=docker)。

在您的 lambda 代码中,确保您使用此环境变量来设置主机名(http://在 localstack 中以该服务的端口号开头和后缀,例如:4569对于 dynamodb)。这将确保呼叫转到正确的位置。

Go 代码片段中的示例将添加到您正在调用 DynamoDB 的 lambda 中:

awsConfig.WithEndpoint("http://" + os.Getenv("LOCALSTACK_HOSTNAME") + ":4569")
于 2019-11-06T10:02:10.500 回答