简体   繁体   English

AWS Lambda function 使用有效负载

[英]AWS Lambda function use payload

I have a python lambda function that i use to get a status report on an AMI export, I need to send a variable as a payload using an AWS cli command but for some reasons I cannot get the function to use the data in the payload. I have a python lambda function that i use to get a status report on an AMI export, I need to send a variable as a payload using an AWS cli command but for some reasons I cannot get the function to use the data in the payload.

This is my function:这是我的 function:

import json
import boto3
import os

AMI_PROCESS_ID = os.environ['AMI_PROCESS_ID']
client = boto3.client('ec2')

def lambda_handler(event, context):
    response = client.describe_export_image_tasks(
        DryRun=False,
        ExportImageTaskIds=[
            AMI_PROCESS_ID
        ],
        MaxResults=123,
        NextToken='teststeatasrfdasdas'
    )
    return response

If I set the environmental variable AMI_PROCESS_ID in the function it works without any issue如果我在 function 中设置环境变量 AMI_PROCESS_ID 它可以正常工作

This is the aws cli command I'm sending这是我发送的 aws cli 命令

 aws lambda invoke --function-name test_deleteme --cli-binary-format raw-in-base64-out --payload '{"AMI_PROCESS_ID": "export-ami-12354564534d"}' response.json --profile my-profile  --no-verify-ssl

This is the content of the response.json这是响应的内容。json

{"errorMessage": "'AMI_PROCESS_ID'", "errorType": "KeyError", "requestId": "", "stackTrace": ["  File \"/var/lang/lib/python3.9/importlib/__init__.py\", line 127, in import_module\n    return _bootstrap._gcd_import(name[level:], package, level)\n", "  File \"<frozen importlib._bootstrap>\", line 1030, in _gcd_import\n", "  File \"<frozen importlib._bootstrap>\", line 1007, in _find_and_load\n", "  File \"<frozen importlib._bootstrap>\", line 986, in _find_and_load_unlocked\n", "  File \"<frozen importlib._bootstrap>\", line 680, in _load_unlocked\n", "  File \"<frozen importlib._bootstrap_external>\", line 850, in exec_module\n", "  File \"<frozen importlib._bootstrap>\", line 228, in _call_with_frames_removed\n", "  File \"/var/task/lambda_function.py\", line 5, in <module>\n    AMI_PROCESS_ID = os.environ['AMI_PROCESS_ID']\n", "  File \"/var/lang/lib/python3.9/os.py\", line 679, in __getitem__\n    raise KeyError(key) from None\n"]}

This is pretty much telling me that the content of the variable AMI_PROCESS_ID is empty这几乎告诉我变量 AMI_PROCESS_ID 的内容是空的

I cannot find documentation on how to unpack a payload, It seems that the os.environ is the way to go but it is not processing it.我找不到有关如何解压有效负载的文档,似乎 os.environ 是通往 go 的方式,但它没有处理它。

Here is the documentation, and here is how you extract the AMI_PROCESS_ID from the payload: 是文档,这里是您从有效负载中提取AMI_PROCESS_ID的方法:

import json
import boto3
import os


client = boto3.client('ec2')

def lambda_handler(event, context):
    AMI_PROCESS_ID = event['AMI_PROCESS_ID']
    response = client.describe_export_image_tasks(
        DryRun=False,
        ExportImageTaskIds=[
            AMI_PROCESS_ID
        ],
        MaxResults=123,
        NextToken='teststeatasrfdasdas'
    )
    return response

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM