I want to upload a file into AWS Lambda using Postman but not from aws cli or console and without creating an API. Just like how you can post a record into Dynamo DB from postman with only the Access key and Secret Access key by hitting a standard URL (https://dynamodb.eu-west-1.amazonaws.com/) , can I do the same in AWS Lambda by just making a REST API call?
1 Answers
Yes- but you'll have to take a bit more responsibility for the process.
Just like dynamo, you can invoke a lambda function from the console or by using lambda invoke from the aws cli (https://docs.aws.amazon.com/cli/latest/reference/lambda/invoke.html)
Lambda has to have a JSON payload (that becomes your event argument in the handler). But there's no other requirements beyond that if you're invoking directly.
If you want to send a file, all you need to do, therefore, is find a way to encode it in JSON. A common approach would be to base64 encode it.
To invoke a lambda from Postman:
Method: POST
Request URL: https://lambda.$REGION.amazonaws.com/2015-03-31/functions/$FUNCTION_NAME/invocations
Headers:
- X-Amz-Invocation-Type:
RequestResponse - X-Amz-Client-Context:
e30=(this is just an empty closure ({}) in base64)
Body:
{
filedata: 'base64encodedfile-made-using-link-below'
}
To get the value of filedata you'll need to use a conversion tool like this one (https://www.base64encode.org/) to get the base64 version of your file.
Authentication: AWS Signature
(You can read the service guide for more information on this call: https://docs.aws.amazon.com/lambda/latest/dg/API_Invoke.html)
Your lambda will then need to decode the base64 encoding to get the binary file. For example:
lambda.js
exports.handler = function(event, context, callback) {
const { filedata } = event;
const data = Buffer.from(filedata, 'base64').toString(); // or whatever format you need to read.
callback(null, "some success message");
}