Blog 4: Step-by-Step Implementation — Building a Server less Dynamic Image Gallery with AWS Lambda…
Introduction
Blog 4: Step-by-Step Implementation — Building a Server less Dynamic Image Gallery with AWS Lambda + API Gateway + S3
Introduction
In the previous blog, we discussed how using AWS Lambda and API Gateway can make your image gallery dynamic — automatically updating with new images uploaded to an S3 bucket. Now it’s time to put that theory into action.
In this post, we’ll walk through the complete hands-on implementation — from setting up IAM roles to configuring API Gateway and CORS — so you can deploy your own fully serverless, auto-updating image gallery.
Step 1: Set Up the S3 Bucket
- Log in to your AWS Management Console and open the S3 service.
- Click Create bucket and give it a unique name (e.g.,
college-gallery-images). - Choose your region and leave other defaults unless you need specific configurations.
- Create a folder called
/images/inside the bucket and upload a few sample images organized by date:
/images/2025-08-29/img1.jpg
/images/2025-08-29/img2.jpg
/images/2025-08-30/img1.jpg
- Go to Permissions → Bucket Policy and ensure your objects can be read:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": "*",
"Action": ["s3:GetObject"],
"Resource": ["arn:aws:s3:::college-gallery-images/*"]
}
]
}
Tip: Keep the bucket public only for demo purposes. For production, use signed URLs or CloudFront for controlled access.
Step 2: Create the Lambda Function
- Open the Lambda service in AWS Console and click Create function.
- Choose Author from scratch, name it
listGalleryImages, and select Python 3.8 or 3.9 as the runtime. - Under Permissions, choose Create a new role with basic Lambda permissions.
- Once created, add the environment variable:
Key:
BUCKET_NAME
Value:
college-gallery-images
- Replace the default Lambda code with the following:
import boto3, os, json
s3 = boto3.client('s3')
BUCKET = os.environ['BUCKET_NAME']
def lambda_handler(event, context):
date = event.get('queryStringParameters', {}).get('date', '')
prefix = f"images/{date}/" if date else "images/"
try:
response = s3.list_objects_v2(Bucket=BUCKET, Prefix=prefix)
image_urls = [
f"https://{BUCKET}.s3.amazonaws.com/{obj['Key']}"
for obj in response.get('Contents', [])
if not obj['Key'].endswith('/')
]
return {
'statusCode': 200,
'headers': {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
'body': json.dumps({'images': image_urls})
}
except Exception as e:
return {'statusCode': 500, 'body': json.dumps({'error': str(e)})}
- Click Deploy to save your changes.
Step 3: Assign IAM Permissions to Lambda
Your Lambda function needs permission to access S3. Attach the following policy to its execution role:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": ["s3:ListBucket"],
"Resource": ["arn:aws:s3:::college-gallery-images"]
},
{
"Effect": "Allow",
"Action": ["s3:GetObject"],
"Resource": ["arn:aws:s3:::college-gallery-images/*"]
}
]
}
You can add it directly using the IAM Console → Roles → [Lambda Role] → Add permissions → Inline Policy.
Step 4: Configure API Gateway
- Open API Gateway and choose Create API → HTTP API.
- Click Add Integration → Lambda and select your
listGalleryImagesfunction. - Create a route:
Method: GET
Resource path: /images
- Resource path:
/images
- Deploy your API and note the invoke URL (e.g.,
[https://abc123.execute-api.us-east-1.amazonaws.com/images).](https://abc123.execute-api.us-east-1.amazonaws.com/images).) - Enable CORS for your route:
- Go to CORS settings and allow origins like
*or your specific domain. - Save and redeploy your API.
메타데이터
- post_id
- a18c8aef723b
- slug
- blog-4-step-by-step-implementation-building-a-server-less-dynamic-image-gallery-with-aws-lambda-a18c8aef723b
- url
- https://medium.com/@ngn22666/blog-4-step-by-step-implementation-building-a-server-less-dynamic-image-gallery-with-aws-lambda-a18c8aef723b
- canonical_url
- https://medium.com/@ngn22666/blog-4-step-by-step-implementation-building-a-server-less-dynamic-image-gallery-with-aws-lambda-a18c8aef723b
- author_url
- https://medium.com/@ngn22666
- status
- ok
- fetched_at
- 2026-07-13 06:23:13