Read and write to S3 with AWS Lambda
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
IAM_policy.json Restrict actions Jan 5, 2019
README.md Update README.md Jan 6, 2019
bucket_policy.json Added required actions Jan 5, 2019
handler.py I shot myself in the foot Jan 10, 2019

README.md

Read and write to S3 with AWS Lambda

This is a guide on creating a serverless API using AWS Lambda with API Gateway and S3, by providing a single endpoint for reading and writing JSON from a file in a S3 bucket.

Usage

You can either update the file with POST or read it with GET.

Update

curl <your_api_endpoint> \
    --request POST \
    --header "Content-Type: application/json" \
    --header "File-Target: <s3_file.json>" \
    --header "X-Api-key: <your_api_key>" \
    --data '{ "object_1": "example", "object_2": 44 }'

Read

curl <your_api_endpoint> \
    --request GET \
    --header "Content-Type: application/json" \
    --header "File-Target: <s3_file.json>" \
    --header "X-Api-key: <your_api_key>"

Setup instructions

Follow the instructions step by step to set everything up.

1. Lambda

  1. Create your Lambda function
    • Use Python 3.7 runtime
  2. Create a new role (or use lambda_basic_execution)
  3. Add the contents of handler.py to the lambda_function
    • Replace <bucket_name> with the name of your S3 bucket
  4. Under Add triggers, select API Gateway
  5. Go to Configure triggers and choose the following:
    • API: Create new API
    • Security: Open with API key
  6. Save

2. S3

  1. Create a S3 bucket
  2. Add the contents of bucket_policy.json to your S3 Bucket Policy
    • Replace <bucket_name> with the name of your bucket
    • Replace <lambda_role> with the name of your Lambda role

3. IAM

  1. Go to the IAM Management Console
  2. Go to Roles
  3. Choose the role you created during the Lambda setup
  4. Click Attach policies and then on Create policy
  5. Select JSON
  6. Add the contents of IAM_policy.json
    • Replace <bucket_name> with the name of your bucket
  7. Press Review policy