2

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)

Zyie
  • 134
  • 2
  • 12

4 Answers4

3

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
nicor88
  • 1,398
  • 10
  • 13
  • so how do i make the post request? The app is using javascript. – Zyie Jun 15 '18 at 12:39
  • Are you using specific libraries in the App? Here you can find a way to send a POST without using any libs .https://stackoverflow.com/questions/24468459/sending-a-json-to-server-and-retrieving-a-json-in-return-without-jquery – nicor88 Jun 15 '18 at 13:15
  • i think this should work. where do i find the url to post to? – Zyie Jun 15 '18 at 13:29
  • This is something that you can retrieve from the API Gateway in AWS – nicor88 Jun 15 '18 at 13:30
  • i got the url, and used your link however im getting this error: Failed to load resource: the server responded with a status of 403 () using Gateway's console ive manged to get it work – Zyie Jun 15 '18 at 13:43
  • How the URL looks like? It should be something like https://4z9giyi2c1.execute-api.us-west-2.amazonaws.com/deployment_stage/your_endopoint – nicor88 Jun 15 '18 at 14:00
  • like this: https://---------.execute-api.eu-west-1.amazonaws.com/test/magiswordsupload – Zyie Jun 15 '18 at 14:01
  • Can you try to send something using Python requests? – nicor88 Jun 15 '18 at 14:03
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/173223/discussion-between-nicor88-and-zyie). – nicor88 Jun 15 '18 at 14:07
1

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/

Nune Isabekyan
  • 531
  • 2
  • 4
1

Can also use APIGateway s3 integration directly

https://docs.aws.amazon.com/apigateway/latest/developerguide/integrating-api-with-aws-services-s3.html

Vishal
  • 34
  • 1
0

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.

https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-create-api-as-simple-proxy-for-lambda.html