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 :
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
This is the aws cli command I'm sending
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
{"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
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.
CodePudding user response:
Here is the documentation, and here is how you extract the AMI_PROCESS_ID
from the payload:
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