AWS Image Optimization
readyHigh-throughput on-demand image optimization and AVIF/WebP transcoding on AWS backed by S3 origins, Sharp, and CloudFront edge caching.
Source reference: shared/nextcore/nextconfig.go, cli/internal/serverless/aws_cloudfront.go
Overview
In Next.js, the <Image /> component delivers dynamically resized, modern format (WebP/AVIF) images via /_next/image?url=...&w=...&q=....
When deploying to AWS, running image transcoding inside the primary Next.js SSR Lambda can cause severe CPU contention, memory bloat, and slow page response times. NextDeploy solves this by architecting a dedicated image optimization architecture:
Dedicated Image Lambda Architecture
When serverless.image_optimization: true is configured in nextdeploy.yml:
- Independent Lambda Function: NextDeploy provisions an isolated Lambda function (
<app>-image-optimizer) configured specifically for CPU-intensive image resizing:- Architecture:
arm64(AWS Graviton) for 20% lower cost and higher Sharp SIMD throughput. - Memory: 1536 MB (optimal balance between vCPU allocation and Lambda cost).
- Binary Engine: Bundles precompiled Linux ARM64 libvips and Sharp binaries.
- Architecture:
- Dedicated CloudFront Cache Behavior:
- Path Pattern:
/_next/image* - Cache Policy: Keys on
url,w(width), andq(quality) query parameters. - Origin Request Policy: Forwards
Acceptheaders (image/avif,image/webp) so CloudFront caches format variants separately. - TTL: Default 30 days (
Cache-Control: public, max-age=2592000, immutable).
- Path Pattern:
Configuration
Enable image optimization in nextdeploy.yml:
serverless:
provider: aws
region: us-east-1
image_optimization: true # Deploys dedicated image optimizer Lambda
NextDeploy automatically inspects next.config.{js,mjs,ts} during compilation:
// next.config.mjs
export default {
images: {
formats: ['image/avif', 'image/webp'],
deviceSizes: [640, 750, 828, 1080, 1200, 1920, 2048],
imageSizes: [16, 32, 48, 64, 96, 128, 256, 384],
remotePatterns: [
{
protocol: 'https',
hostname: 'images.unsplash.com',
},
{
protocol: 'https',
hostname: 'cdn.sanity.io',
},
],
},
};
Security & SSRF Protection
NextDeploy enforces strict Server-Side Request Forgery (SSRF) defenses:
- Allowlisted Remote Hosts: Only hostnames declared in
remotePatternsordomainscan be transcoded. External unallowlisted URLs return400 Bad Request. - Private S3 Fetching: Relative images (
/hero.png) are fetched from your private S3 bucket using SigV4 credentials. The S3 bucket requires no public read access. - Payload Clamping: Width parameters are clamped to declared
deviceSizesandimageSizesto prevent denial-of-service via arbitrary dimension generation.
Cost Optimization & Unoptimized Fallback
For applications hosting static banners or offloading images to third-party CDNs (Cloudinary, Imgix):
// next.config.mjs
export default {
images: {
unoptimized: true,
},
};
Setting unoptimized: true instructs CloudFront to serve original assets directly from S3 without deploying or invoking the image optimization Lambda.