python urllib3 lambda错误:LocationParseError无法解析 [英] python urllib3 lambda Error: LocationParseError Failed to parse

查看:908
本文介绍了python urllib3 lambda错误:LocationParseError无法解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Lambda中使用urllib3库和python3代码,这些代码可从AWS Secret Manager中获取MSTeams的webhook网址,并发送http发布请求以发布通知.

I'm using urllib3 library in Lambda and python3 code that fetches the webhook url of MSTeams from AWS Secret Manager and sends a http post request to publish a notification.

我的webhook网址以https开头,看起来像这样的"https://outlook.office.com/webhook/.......".执行lambda函数时,出现如下所示的错误 LocationParseError无法解析:

My webhook url starts with https and looks like this "https://outlook.office.com/webhook/.......". On executing the lambda function, I get an error as shown below LocationParseError Failed to parse:

代码

import urllib3 

http = urllib3.PoolManager() 

MSTEAMS_WEBHOOK_SECRET_NAME = os.getenv('MSTEAMS_WEBHOOK_SECRET_NAME') 
HOOK_URL = get_secret(MSTEAMS_WEBHOOK_SECRET_NAME,"eu-west-1") 

def get_secret(secret_name, region_name):

    # Create a Secrets Manager client
    session = boto3.session.Session()
    client = session.client(
        service_name='secretsmanager',
        region_name=region_name
    )
        get_secret_value_response = client.get_secret_value(
            SecretId=secret_name,
            VersionStage="AWSCURRENT"
        )
        if 'SecretString' in get_secret_value_response:
            secret = get_secret_value_response['SecretString']
            return secret
        else:
            decoded_binary_secret = base64.b64decode(get_secret_value_response['SecretBinary'])
            return decoded_binary_secret


def lambda_handler(event, context):
 message = {
      "@context": "https://schema.org/extensions",
      "@type": "MessageCard",
      "themeColor": data["colour"],
      "title": title,
      "text": "accountId:\n"  + account_id + " <br/>\n" 
    }
 
    webhook_encoded_body = json.dumps(message).encode('utf-8')
    response = http.request('POST',HOOK_URL, body=webhook_encoded_body)

errorMessage

errorMessage

{
  "errorMessage": "Failed to parse: {\"msteams-secret\":\"https://outlook.office.com/webhook/dxxxxxx@d779xxxxx-xxxxxx/IncomingWebhook/axxxxxx5/ca746326-bxxx-4xxx-8x-xxxxx\"}",
  "errorType": "LocationParseError",
  "stackTrace": [
    [
      "/var/task/lambda_function.py",
      145,
      "lambda_handler",
      "resp = http.request('POST',HOOK_URL, body=webhook_encoded_body)"
    ],
    [
      "/var/runtime/urllib3/request.py",
      80,
      "request",
      "method, url, fields=fields, headers=headers, **urlopen_kw"
    ],
    [
      "/var/runtime/urllib3/request.py",
      171,
      "request_encode_body",
      "return self.urlopen(method, url, **extra_kw)"
    ],
    [
      "/var/runtime/urllib3/poolmanager.py",
      324,
      "urlopen",
      "u = parse_url(url)"
    ],
    [
      "/var/runtime/urllib3/util/url.py",
      392,
      "parse_url",
      "return six.raise_from(LocationParseError(source_url), None)"
    ],
    [
      "<string>",
      3,
      "raise_from",
      ""
    ]
  ]
}

推荐答案

这是我解决的方法

  • 再次部署lambda zip文件,并将正确的依赖项(如请求,urllib3)部署在同一文件夹中
  • 显然,我试图将密钥作为键/值对存储在AWS Secret Manager中,因此它无法解析字典.我将机密类型更改为纯文本

这篇关于python urllib3 lambda错误:LocationParseError无法解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆