cover image: Photo by Francesco Ungaro on Unsplash
In this article, we will talk about creating an AWS REST API as an AWS S3 proxy and delivering images in an S3 bucket through the API gateway.
What are the different ways to deliver images in the s3 bucket?
Deliver images using public S3 URLs
Deliver images in S3 using AWS Lambda API Gateway Integration
Creating an AWS REST API as an S3 proxy
Why deliver images in the S3 bucket through the API gateway?
The simplest way to deliver images in the S3 bucket is public S3 URLs. However, the major drawback of this approach is that the S3 bucket will be public. Public cloud resources are vulnerable to attacks, and avoiding public cloud resources is a good practice.
AWS API gateway will be a public interface for most endpoints. Many security methods can be used with AWS API gateway such as IAM, request throttling, and many more.
AWS Lambda can be integrated into the AWS API gateway. Images in an S3 bucket can be delivered using a Lambda. But then we have to develop and maintain an additional Lambda function. Also, there will be an extra cost for it.
When we use AWS API Gateway REST API as an S3 proxy, we can use security features in the AWS API gateway and we do not want to maintain an additional Lambda function. Also, we can keep our S3 bucket as a private resource.
Use AWS API Gateway REST API as an S3 proxy
I explain the method using an example scenario. Also, I write this article with multiple steps for better understanding. Names and AWS regions can be changed according to the requirements. I use the new version of the AWS console (as of March 2024) in this article.
Table of content
- Create an IAM role for allowing API gateway to access S3 bucket objects
- Create an S3 bucket and upload an image
- Create an AWS API Gateway REST API
- Create REST resource
- Create REST method
- Configure method request
- Configure integration request
- Configure integration response
- Configure Method response
- Enable binary support in API Gateway
1. Create an IAM role for allowing API gateway to access S3 bucket objects
- Log into the AWS console
- Navigate to
IAM
→Roles
- Click on the
Create role
button - Select
Custom trust policy
as the trusted entity type - Enter the following policy as the custom trust policy
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"Service": "apigateway.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
- Click on the
Next
button - Search for
AmazonS3ReadOnlyAccess
and select it - Then Click on the
Next
button - Enter a role name as
iam-test-role
- Then click on the
Create role
button
2. Create an S3 bucket and upload an image
- Log into the AWS console
- Navigate to
S3
- Click on the
Create bucket
button - Select
us-east-1
as the region - Enter
api-gw-image-test-s3
as the bucket name - Leave default values for other configurations
- Click on the
Create bucket
button - Create a folder named
images
- Upload an image into the created folder
3. Create an AWS API Gateway REST API
- Log into your AWS console
- Navigate to
API Gateway
→APIs
- Click on the
Create API
button - Click on the
Build
button onRest API
pane - Select
New API
and usetest-api
as the name - Leave default values for other configurations
- Then click on the
Create API
button
4. Create REST resource
- Click on the
Create resource
button - Create a resource and create REST resource
s3
5. Create REST method
- Click on the
Create method
button to create a method - Choose as following
- Method type → GET
- Integration type → AWS service
- AWS Region → us-east-1
- AWS service → Simple Storage Service (S3)
- HTTP method → GET
- Action type → Use path override
- Path override → api-gw-image-test-s3/images/{image}
- Execution role → arn of the created aws role in the step (1)
- Leave the other options as default
- Click on the
Create method
button
6. Configure method request
- Select
Method request
tab pane - Click on
Edit
button - Expand
URL query string parameters
- Click on
Add query string
button - Enter
image
as the Name - Check
Required
button - Click on
Save
button
7. Configure integration request
- Select
Integration request
tab pane - Click on
Edit
button - Select
When there are no templates defined (recommended)
asRequest body passthrough
- Expand
URL path parameters
- Click on
Add path parameter
button - Enter
image
as the name - Enter
method.request.querystring.image
as the path - Expand
URL request headers parameters
- Click on
Add query string parameter
button - Enter
Accept
asName
- Enter
'*/*'
(Value should be given with single quotes) asMapped from
- Click on
Save
button
8. Configure integration response
- Select
Integration responses
tab pane - Delete
Default - Response
- Click on
Create response
- Enter
2\d{2}
as HTTP status regex - Click on
Create
button
9. Configure Method response
- Select
Method responses
tab pane - Click on
Edit
button onResponse 200
- Click on
Add header
button - Enter
Content-Type
asHeader name
- Remove
Response body
items - Click on
Save
button - Then again select
Integration responses
tab pane - Click on
Edit
button - Enter
'*/*'
(Value should be given with single quotes) asMapping value
of theContent-Type
response header - Click on
Save
button
10. Enable binary support in API Gateway
- Navigate to
API settings
- Click on
Manage media type
button onBinary media types
- Click on
Add binary media type
button - Enter
*/*
asBinary media type
- Click on
Save changes
Finally,
- Click on
Deploy API
and do an API Gateway deployment - Enter the invoked URI on the browser with the image query string (e.g.: https://aabhd7xr1z.execute-api.us-east-1.amazonaws.com/test/s3?image=porsche-911.jpg)
- Ultimately, the Image is displayed in the browser
Summary
In this article, we have discussed creating an AWS REST API as an Amazon S3 proxy and delivering images in an S3 bucket through the API gateway. We can use AWS API Gateway as a secure and cost-effective way to deliver S3 images.
Top comments (0)