How to integrate API Gateway with s3 in CDK
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Integrating API Gateway with S3 through AWS CDK is a common way to expose object download or upload endpoints without running an always-on backend service. This pattern is fast and cost-effective for simple file APIs, but security and mapping details matter. A correct implementation sets explicit IAM permissions, request mapping, and CORS behavior from the start.
Choose an Integration Pattern First
There are two main patterns:
- Direct service integration, where API Gateway calls S3 operations directly.
- Lambda proxy, where Lambda applies custom logic and then talks to S3.
Direct integration is ideal for simple read or write routes. Lambda proxy is better when you need complex validation, custom auth checks, or multi-step business logic.
For this article, we focus on direct integration with CDK.
CDK Stack with S3 and API Gateway
The following TypeScript stack creates:
- A private S3 bucket.
- A role that API Gateway can assume.
- A REST route for object reads by key.
This gives a minimal read endpoint while keeping the bucket private.
Add Upload Support Safely
For uploads, prefer constrained write access and predictable key prefixes.
You can define a separate PUT method and map request path key similarly. Restricting writes to uploads prevents accidental overwrite of system files.
For browser uploads, many teams choose pre-signed URLs from a small auth service instead of direct unauthenticated API Gateway writes.
CORS and Response Headers
If clients call from browser apps, configure CORS on the API resources and confirm S3 response headers are forwarded correctly.
CDK example for preflight on the files resource:
Without this, browser requests may fail even when backend integration is correct.
Deploy and Test
After deploy:
Then test the route:
Verify:
- Existing object returns
200. - Missing object returns expected
404mapping. - IAM role has only required S3 permissions.
Security and Operations Checklist
For production readiness:
- Keep bucket public access blocked.
- Scope API Gateway role to exact S3 actions and prefixes.
- Add throttling and usage plans where needed.
- Log API access and inspect error metrics in CloudWatch.
If endpoint is internet-facing and high traffic, consider WAF protections and request-size controls.
Common Pitfalls
- Granting broad S3 permissions to API role. Fix by limiting actions and key prefixes.
- Forgetting path parameter mapping from method to integration request. Fix by configuring both request parameter maps.
- Mixing direct integration and business logic-heavy workflows. Fix by moving complex logic to Lambda.
- Skipping CORS configuration for browser clients. Fix by defining preflight rules explicitly.
- Leaving bucket public while also routing through API. Fix by keeping one clear access path and enforcing private bucket policy.
Summary
- CDK can define a secure API Gateway to S3 integration with little code.
- Direct service integration is best for straightforward file APIs.
- Correct IAM scoping and request mapping are the core implementation details.
- CORS and response mapping determine whether browser clients work reliably.
- Start with minimal access, then expand capabilities only when required.
Related reading
- How to kill/terminate a running AWS Lambda function?
- How to know RDS free storage
- How to label Kubernetes node?
- How to launch local DynamoDB programmatically?
- How to invoke the Pod proxy verb using the Kubernetes Go client?
- How to iterate a dataset several times using TensorFlow's Dataset API?
- How to list _all_ objects in Amazon S3 bucket?
- How to list all AWS S3 objects in a bucket using Java

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.