0

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": ¿?
}
eduardosufan
  • 1,441
  • 2
  • 23
  • 51
  • Why not just pass the S3 file URL and let your Lambda function read the file from S3, instead of passing the entire file contents around? If you absolutely want to pass the file contents, then searching for something like "boto3 read file contents from S3" will return tons of results. – Mark B May 11 '23 at 15:42
  • What if the API doesn't have permissions to read to that bucket? That's a customer bucket. – eduardosufan May 11 '23 at 17:16
  • Then my suggestion doesn't work, but that wasn't a detail you included in the original question so I couldn't know that. In that case please see the many duplicate questions on here for reading S3 objects using Boto3, such as: https://stackoverflow.com/questions/36205481/read-file-content-from-s3-bucket-with-boto3 – Mark B May 11 '23 at 18:28
  • 1
    Note that the Lambda invocation payload size is limited to 6MB https://docs.aws.amazon.com/lambda/latest/dg/gettingstarted-limits.html so if your files are going to be larger than that you need to have your script that invokes the Lambda function first copy the object from the customer's S3 bucket into your own S3 bucket, and then pass the S3 URL in the payload. – Mark B May 11 '23 at 18:33

0 Answers0