Reputation: 154
So i have setup a lambda function to upload a txt file to S3. How do i send data to the function using API Gateway?
Ive setup API Gateway to have a POST method.
here is my Lambda function
import boto3
s3 = boto3.resource('s3')
def lambda_handler(event, context):
data = 'Totally awesome sword design' #event['data']
filename = 'awesomeSword2' #event['filename']
object = s3.Object(BUCKET_NAME, KEY + filename + '.txt')
object.put(Body=data)
I just need to know how to send data and filename to the function (and read it)
Upvotes: 4
Views: 9770
Reputation: 1
tl;dr Use the Lambda Proxy feature of API Gateway
API Gateway passes the raw request to the integrated Lambda function. This request data includes the request headers, query string parameters, URL path variables, payload, and API configuration data. In your example, you can pass in data and filename in a body request.
Upvotes: 0
Reputation: 1553
The lambda will be invoked with the data that you send with the POST request.
For example, let's say that you make a POST request to your API gateway with this JSON:
{"data": "some data"}
The lambda function will receive in the event argument a proper Python dictionary:
{'data': 'some data'}
Then you can do something like that:
def lambda_handler(event, context):
data = event.get('data')
# this will avoid raising an error if event doesn't contain the data key
# do whatever you like with Data
Upvotes: 5
Reputation: 549
Basically, you pass a base64 encoded string as data Here is a blog post by AWS describing how to achieve that: https://aws.amazon.com/blogs/compute/binary-support-for-api-integrations-with-amazon-api-gateway/
Upvotes: 1