【问题标题】:Unable to connect AWS Lambda to Elastic Search. Getting a 403 error无法将 AWS Lambda 连接到 Elastic Search。收到 403 错误
【发布时间】:2021-03-20 17:53:10
【问题描述】:

我正在尝试将流数据从 Amazon Kinesis Data Streams 加载到 Amazon ES 中,如教程中所述:https://docs.aws.amazon.com/elasticsearch-service/latest/developerguide/es-aws-integrations.html#es-aws-integrations-kinesis

如教程中所述,我的 lambda 函数是:

import base64
import boto3
import json
import requests
from requests_aws4auth import AWS4Auth

region = 'us-east-1'
service = 'es'
credentials = boto3.Session().get_credentials()
awsauth = AWS4Auth(credentials.access_key, credentials.secret_key, region, service, session_token=credentials.token)

host = '' # the ES domain has been specified here
index = 'lambda-kine-index'
type = 'lambda-kine-type'
url = host + '/' + index + '/' + type + '/'

headers = { "Content-Type": "application/json" }

def handler(event, context):
    count = 0
    for record in event['Records']:
        id = record['eventID']
        timestamp = record['kinesis']['approximateArrivalTimestamp']
        
        # Kinesis data is base64-encoded, so decode here
        message = base64.b64decode(record['kinesis']['data'])
        
        # Create the JSON document
        document = { "id": id, "timestamp": timestamp, "message": message }
        # Index the document
        r = requests.put(url + id, auth=awsauth, json=document, headers=headers)
        count += 1
    return 'Processed ' + str(count) + ' items.'

此外,如教程中所述,IAM 角色是:


{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "es:ESHttpPost",
        "es:ESHttpPut",
        "logs:CreateLogGroup",
        "logs:CreateLogStream",
        "logs:PutLogEvents",
        "kinesis:GetShardIterator",
        "kinesis:GetRecords",
        "kinesis:DescribeStream",
        "kinesis:ListStreams"
      ],
      "Resource": "*"
    }
  ]
}

信任关系是:


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

执行此操作后,我在运行 lambda 时得到的响应是:

<Response [403]>

感谢您提供解决此问题的任何帮助。

【问题讨论】:

  • 我认为你没有分配 ES host 变量。

标签: amazon-web-services elasticsearch aws-lambda aws-elasticsearch


【解决方案1】:

仅当您使用 IAM 用户时才适用凭证,但此处并非如此,因为这是一个 Lambda 函数并且它需要一个 IAM 角色。

您可能会启用细粒度的访问控制,这与域策略不能很好地配合。

阅读更多here 并注意突出显示的部分 re-user / IAM 混合并且无法正常工作。

【讨论】:

    【解决方案2】:

    对于那些获得 403 且上述解决方案不适用的人...

    如果您使用细粒度权限,则需要将您的 lambda 执行角色添加为后端角色(在 kibana 中配置)。

    在 Kibana 中 -> 安全/角色

    • 将您的角色添加到“all_access”(或任何对您的用例有意义的角色)

    【讨论】:

      【解决方案3】:

      确保您的凭据有效。您可以使用aws-cli 进行验证。参考文档here

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-09-25
        • 2021-12-04
        • 2015-09-18
        • 2017-10-15
        • 2021-06-10
        • 2019-11-08
        • 1970-01-01
        • 2015-11-30
        相关资源
        最近更新 更多