GLM-5 is available to try on Modal. Get started
September 4, 20245 minute read

Upload files to S3 with AWS Lambda and AWS API Gateway in TypeScript: A Step-by-Step Guide

author
Yiren Lu@YirenLu
Solutions Engineer

Are you looking to create a serverless solution for uploading JPEG images to Amazon S3 using AWS API Gateway and Lambda? This guide will walk you through creating an API Gateway endpoint that uploads JPEG images to an S3 bucket using AWS Lambda, with the added feature of using a “filename” parameter to name the S3 object. We’ll be using TypeScript to write our Lambda function, ensuring type safety and improved developer experience.

Prerequisites

Before we begin, make sure you have:

  1. An AWS account
  2. AWS CLI installed and configured
  3. Node.js and npm installed
  4. TypeScript installed globally (npm install -g typescript)
  5. AWS SAM CLI installed

Step 1: Set Up the Project

First, let’s create a new directory for our project and initialize it:

Create a tsconfig.json file in the root directory:

Step 2: Create the Lambda Function

Create a new file src/index.ts and add the following code:

This Lambda function does the following:

  1. Checks for the presence of the file content and filename parameter
  2. Verifies that the file is a JPEG image
  3. Uploads the file to S3 using the provided filename
  4. Returns a success message with the file URL or an error message

Step 3: Create the SAM Template

Create a template.yaml file in the root directory:

This SAM template sets up:

  1. A Lambda function with the necessary permissions
  2. An S3 bucket for storing the uploaded images
  3. An API Gateway endpoint that triggers the Lambda function

Step 4: Build and Deploy

First, compile your TypeScript code:

Then, use SAM CLI to build and deploy your application:

Follow the prompts to complete the deployment. Once finished, you’ll receive an API endpoint URL.

Step 5: Test Your API

You can test your new API using cURL. Replace YOUR_API_ENDPOINT with the URL provided after deployment:

This command sends a POST request to your API with the image file and the filename parameter.

Conclusion

Congratulations! You’ve successfully created an AWS API Gateway endpoint that uploads JPEG images to an S3 bucket using AWS Lambda and TypeScript. This serverless solution provides a scalable and efficient way to handle image uploads in your applications.

Remember to implement additional security measures such as authentication and input validation based on your specific use case.

Ship your first app in minutes.

Get Started

$30 / month free compute