Linked Questions

Popular Questions

AWS lambda boto3 invoke with file as payload

Asked by At

I'm integrating a new AWS Lambda with an existent API running also in lambda. I want to call that API in lambda directly by instead of using postman.

Right now, I have a postman collection that calls the API lambda and then the flow starts. Now, I need to code that AWS lambda invoke from another lambda.

This is the exported python code from postman:

import requests

url = "my_awesome_url/api/1.0/ingest?query_param_1=my_query_param"

payload = "<file contents here>"
headers = {
  'Content-Type': 'application/pdf'
}

response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)

In postman, I'm attaching a pdf file as binary:

enter image description here

In the API, the file content is read and stored in another S3 location. I can see in the code, that the file manipulation is performed in the next way:

document = S3.Object(bucket, renamed_file)
document.put(Body=base64.b64decode(event['body']))

Where bucket and renamed_file are pointing to the new object location. In the "body" is going the file content (?).

My particular question is: How to build the payload of the AWS invoke with the file in the body?

Now, the good thing is that I have the file located in S3. So I have the bucket name, the file path and also the object url of that pdf file.

So far, the payload I build is the next one:

    payload = {
    "path": "/api/1.0/ingest",
    "headers": {
        "Content-Type": content_type
    },
    "queryStringParameters": {
      "query_param_1": my_query_param
    },
    "body": ¿?
}

Related Questions