Efficiently Developing Serverless Functions dengan Claude Code
Learn about efficiently developing serverless functions using Claude Code. Includes practical code examples.
serverlesspengembanganにClaude Codeを使うメリット
serverlessfungsi cold start対策、メモリoptimasi、payloadサイズ制限 dll.、固有 batasan あり.Claude Code これら batasan 理解 上 、optimasiされたコード generate.
pengembangan AWS Lambdafungsi
> AWS Lambda + API Gateway dengan gambarリサイズAPI buatkan.
> SAMtemplate juga pembuatanして。TypeScript dengan implementasiして。
Lambdafungsi
// src/handlers/resize-image.ts
import { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda';
import { S3Client, GetObjectCommand, PutObjectCommand } from '@aws-sdk/client-s3';
import sharp from 'sharp';
const s3 = new S3Client({});
interface ResizeRequest {
key: string;
width: number;
height?: number;
quality?: number;
}
export async function handler(event: APIGatewayProxyEvent): Promise<APIGatewayProxyResult> {
try {
const body = JSON.parse(event.body || '{}') as ResizeRequest;
if (!body.key || !body.width) {
return {
statusCode: 400,
body: JSON.stringify({ error: 'key and width are required' }),
};
}
// S3から元gambar pengambilan
const { Body, ContentType } = await s3.send(new GetObjectCommand({
Bucket: process.env.BUCKET_NAME!,
Key: body.key,
}));
const buffer = Buffer.from(await Body!.transformToByteArray());
// リサイズ
const resized = await sharp(buffer)
.resize(body.width, body.height, { fit: 'cover', withoutEnlargement: true })
.webp({ quality: body.quality || 80 })
.toBuffer();
// リサイズ後 gambar penyimpanan
const outputKey = body.key.replace(/\.[^.]+$/, `-${body.width}w.webp`);
await s3.send(new PutObjectCommand({
Bucket: process.env.BUCKET_NAME!,
Key: outputKey,
Body: resized,
ContentType: 'image/webp',
}));
return {
statusCode: 200,
body: JSON.stringify({
key: outputKey,
size: resized.length,
width: body.width,
}),
};
} catch (err) {
console.error('Resize error:', err);
return {
statusCode: 500,
body: JSON.stringify({ error: 'Internal server error' }),
};
}
}
SAMtemplate
# template.yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Globals:
Function:
Runtime: nodejs20.x
Timeout: 30
MemorySize: 512
Environment:
Variables:
BUCKET_NAME: !Ref ImageBucket
Resources:
ResizeFunction:
Type: AWS::Serverless::Function
Properties:
Handler: dist/handlers/resize-image.handler
Events:
Api:
Type: Api
Properties:
Path: /resize
Method: post
Policies:
- S3CrudPolicy:
BucketName: !Ref ImageBucket
Metadata:
BuildMethod: esbuild
BuildProperties:
Minify: true
Target: es2022
External:
- sharp
ImageBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Sub '${AWS::StackName}-images'
Outputs:
ApiUrl:
Value: !Sub 'https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/'
pengembangan Cloudflare Workers
> Cloudflare Workers dengan 短縮URLgenerateservice buatkan.
> KVstore 使ってURLマッピング manajemenして。
// src/worker.ts
interface Env {
URL_STORE: KVNamespace;
}
export default {
async fetch(request: Request, env: Env): Promise<Response> {
const url = new URL(request.url);
// 短縮URL リダイレクト
if (request.method === 'GET' && url.pathname.length > 1) {
const slug = url.pathname.slice(1);
const targetUrl = await env.URL_STORE.get(slug);
if (targetUrl) {
return Response.redirect(targetUrl, 302);
}
return new Response('Not found', { status: 404 });
}
// URL短縮 pembuatan
if (request.method === 'POST' && url.pathname === '/api/shorten') {
const { url: targetUrl } = await request.json<{ url: string }>();
if (!targetUrl || !isValidUrl(targetUrl)) {
return Response.json({ error: 'Invalid URL' }, { status: 400 });
}
const slug = generateSlug();
await env.URL_STORE.put(slug, targetUrl, { expirationTtl: 86400 * 365 });
return Response.json({
shortUrl: `${url.origin}/${slug}`,
slug,
originalUrl: targetUrl,
});
}
return new Response('Method not allowed', { status: 405 });
},
};
function generateSlug(): string {
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
return Array.from(crypto.getRandomValues(new Uint8Array(7)))
.map(b => chars[b % chars.length])
.join('');
}
function isValidUrl(str: string): boolean {
try {
const url = new URL(str);
return url.protocol === 'http:' || url.protocol === 'https:';
} catch {
return false;
}
}
pengembangan Vercel Edge Functions
// app/api/og/route.tsx
import { ImageResponse } from 'next/og';
export const runtime = 'edge';
export async function GET(request: Request) {
const { searchParams } = new URL(request.url);
const title = searchParams.get('title') || 'My Blog';
return new ImageResponse(
(
<div
style={{
height: '100%',
width: '100%',
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
justifyContent: 'center',
backgroundColor: '#1a1a2e',
color: '#ffffff',
fontFamily: 'sans-serif',
}}
>
<h1 style={{ fontSize: 60, textAlign: 'center', padding: '0 40px' }}>
{title}
</h1>
</div>
),
{ width: 1200, height: 630 }
);
}
cold start対策
// Lambda cold start対策
// koneksi 再pemanfaatan
let dbConnection: PrismaClient | null = null;
function getDB() {
if (!dbConnection) {
dbConnection = new PrismaClient();
}
return dbConnection;
}
// ウォームアップ用handler
export async function warmupHandler(event: ScheduledEvent) {
// CloudWatch Events 定期実行 cold start 防ぐ
console.log('Warmup invocation');
return { statusCode: 200 };
}
Testing
import { handler } from './resize-image';
import { mockClient } from 'aws-sdk-client-mock';
import { S3Client, GetObjectCommand, PutObjectCommand } from '@aws-sdk/client-s3';
const s3Mock = mockClient(S3Client);
describe('resize-image handler', () => {
beforeEach(() => {
s3Mock.reset();
});
it('should return 400 when key is missing', async () => {
const event = { body: JSON.stringify({ width: 200 }) } as any;
const result = await handler(event);
expect(result.statusCode).toBe(400);
});
it('should resize and upload image', async () => {
s3Mock.on(GetObjectCommand).resolves({
Body: createMockStream(testImageBuffer),
ContentType: 'image/jpeg',
});
s3Mock.on(PutObjectCommand).resolves({});
const event = {
body: JSON.stringify({ key: 'test.jpg', width: 200 }),
} as any;
const result = await handler(event);
expect(result.statusCode).toBe(200);
});
});
Summary
Mengenai Claude Codeを使えば、Lambda・Cloudflare Workers・Vercel Functionsなど各種serverlessplatformにoptimasiされたfungsiをefisienにpengembanganできます。platform固有のbatasanを理解した上でコードをgenerateしてくれるため、cold start対策やメモリoptimasiもtepatに行われます。AWSでの本格的なoperasi, perkenalanしています。 di AWSデプロイの自動化も参照してください。開発の基本的な効率化は生産性を3倍にするTips.
Untuk detail Claude Code, lihat Dokumentasi Resmi Anthropic.
Related Posts
Cara Mempercepat Side Project dengan Claude Code [Dengan Contoh]
Pelajari cara mempercepat project development personal secara drastis menggunakan Claude Code. Dilengkapi contoh nyata dan workflow praktis dari ide hingga deployment.
Cara Mengotomatisasi Refactoring dengan Claude Code
Pelajari cara mengotomatisasi code refactoring secara efisien menggunakan Claude Code. Dilengkapi prompt praktis dan pola refactoring konkret untuk project nyata.
Panduan Lengkap Konfigurasi CORS dengan Claude Code
Pelajari tentang panduan lengkap konfigurasi CORS menggunakan Claude Code. Dilengkapi tips praktis dan contoh kode.