【问题标题】:AWS SAM CLI DEPLOYMENT - Issues Parsing Out of the Box secret = get_secret_value_response['SecretString']AWS SAM CLI 部署 - 解析开箱即用的问题 secret = get_secret_value_response['SecretString']
【发布时间】:2021-10-15 13:56:47
【问题描述】:

我对 Python 非常陌生 - 对我来说太赤裸裸了。 使用 AWS 提供的开箱即用 Python3 示例代码从 AWS: Secrets Manager Service 返回“SecretString”。

那里没有问题..我得到了返回的对象(注意我已经删除了一些细节)

{"username":"postgres","password":"XXXXXXXXX","engine":"postgres","host":"srdataset.XXXXXXXXX.ap-southeast-2.rds.amazonaws.com","port":5432,"dbInstanceIdentifier":"srdataset"}  

细节都正确。

然后我使用json.loads() 将上面的内容解析到我的下一个函数中,这样我就可以像这样提取细节

    # request details
    login_details = get_secret("pg_srdataset_login_details")

    # load json
    y = json.loads(login_details)

    # extract result is a Python dictionary:
    print(y["username"])

这又在我的 IDE (PyCharm) 中正常工作。我可以运行代码,在 Docker 容器中构建......然后我使用 PyCharm AWS SAM CLI 将代码部署到云......没问题。

但是,当我在 AWS 中测试该函数时,代码错误出现在 y = json.loads(login_details) 步骤中。

错误是..

{
  "errorMessage": "Expecting value: line 1 column 1 (char 0)",
  "errorType": "JSONDecodeError",
  "stackTrace": [
    "  File \"/var/task/update_sp_changes.py\", line 229, in lambda_handler\n    y = json.loads(login_details)\n",
    "  File \"/var/lang/lib/python3.8/json/__init__.py\", line 357, in loads\n    return _default_decoder.decode(s)\n",
    "  File \"/var/lang/lib/python3.8/json/decoder.py\", line 337, in decode\n    obj, end = self.raw_decode(s, idx=_w(s, 0).end())\n",
    "  File \"/var/lang/lib/python3.8/json/decoder.py\", line 355, in raw_decode\n    raise JSONDecodeError(\"Expecting value\", s, err.value) from None\n"
  ]
}

为了测试这一点,我还复制了从 AWS 返回的 JSON 'SecretString',将其硬编码为变量,然后将此变量直接传递到 y = json.loads(login_details) step。再次测试,效果不错。

我做错了什么 - 我该如何解决这个问题。

【问题讨论】:

  • 可能只是报价问题什么的。您能分享您指的是哪个 AWS 文档吗?我认为内置函数中没有get_secret
  • 澄清一下。我可以成功连接到 AWS Secrets Manager 服务并从中检索响应。我遇到的问题是使用 json.loads() 解析 json 字符串,该字符串作为 aws lambda 函数的一部分运行,这给了我错误。 github.com/aws-samples/aws-secrets-manager-rotation-lambdas/…
  • 我无法重现您的错误,但 json.loads() 在 Lambda 控制台中工作。我不确定为什么您的 cli 和 sam 案例有效,但您可能会将 dict 的输出复制为字符串?

标签: json amazon-web-services aws-lambda aws-secrets-manager aws-sam-cli


【解决方案1】:

作为参考,我会分享 Lambda 打印调试代码。

准备工作:

  • 从 Lambda-py38 初始状态,SecretsManagerReadWrite 策略附加到 Lamda
  • 设置SecretIdVersionId

输出:

  • json.loads()plaintext 工作
  • print() 的输出对于plain_text:str 和secret_dict:dict 几乎相同。 (区别是引号:单/双。复制粘贴时被视为相同的字符串。)
import json
import boto3

def lambda_handler(event, context):

    client = boto3.client('secretsmanager')

    response = client.get_secret_value(
        SecretId='arn:aws:secretsmanager:xxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
        VersionId='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
        VersionStage='AWSCURRENT'
    )

    print("type of response: ", type(response))
    print("response: ", response)

    plaintext = response['SecretString']
    print("type of plaintext: ", type(plaintext))
    print("plaintext: ", plaintext)

    secret_dict = json.loads(plaintext)
    print("type of secret_dict: ", type(secret_dict))
    print("secret_dict: ", secret_dict)

    return 200

【讨论】:

  • 确保“SecretsManagerReadWrite 策略已附加到 Lamda”。原来这就是为什么响应返回为 null .. 它不是代码 .. 它是缺少的权限。一个年轻球员的陷阱。固定..现在我们继续前进。谢谢
猜你喜欢
  • 2021-02-20
  • 2021-11-03
  • 2021-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-03
  • 2023-02-02
  • 2019-06-01
相关资源
最近更新 更多