import {useState} from 'react' import {Braces, Boxes} from 'lucide-react' import {DynamicFormRenderer} from './DynamicFormRenderer' import type {ServiceSchema} from '@/types/schema' interface LambdaCreatePanelProps { schema: ServiceSchema isSubmitting: boolean submitError: string | null onCancel: () => void onSubmit: (values: Record) => void } export function LambdaCreatePanel({schema, isSubmitting, submitError, onCancel, onSubmit}: LambdaCreatePanelProps) { const [mode, setMode] = useState<'choose' | 'catalog' | 'scratch' | 's3' | 'api' | 'scheduled' | 'sqs'>('choose') const [bucketName, setBucketName] = useState('') const [apiName, setApiName] = useState('') const [apiPath, setApiPath] = useState('/hello') const [apiMethod, setApiMethod] = useState('GET') const [apiStage, setApiStage] = useState('dev') const [ruleName, setRuleName] = useState('') const [scheduleExpression, setScheduleExpression] = useState('rate(5 minutes)') const [queueName, setQueueName] = useState('') const [batchSize, setBatchSize] = useState('10') const [blueprintError, setBlueprintError] = useState(null) if (mode === 'choose') return

Create Lambda

How do you want to start?

if (mode === 'catalog') return

Lambda Blueprints

Choose a starting point

Each blueprint displays its source, resources, permissions, and deployment plan before it creates anything.

const blueprint = mode === 's3' ? { runtime: 'nodejs22.x', handler: 'index.handler', description: 'Processes S3 object-created events.', code: S3_UPLOAD_PROCESSOR_SOURCE, } : mode === 'api' ? { runtime: 'nodejs22.x', handler: 'index.handler', description: 'Handles API Gateway Lambda proxy requests.', code: REST_LAMBDA_API_SOURCE, } : mode === 'scheduled' ? { runtime: 'nodejs22.x', handler: 'index.handler', description: 'Runs on an EventBridge schedule.', code: SCHEDULED_LAMBDA_SOURCE, } : mode === 'sqs' ? { runtime: 'nodejs22.x', handler: 'index.handler', description: 'Processes messages from an SQS queue.', code: SQS_WORKER_SOURCE, } : {} return

{mode === 's3' ? 'Blueprint: S3 upload processor' : mode === 'api' ? 'Blueprint: REST Lambda API' : mode === 'scheduled' ? 'Blueprint: Scheduled Lambda' : mode === 'sqs' ? 'Blueprint: SQS Worker' : 'Start from scratch'}

{mode === 's3' ? 'Review the Lambda, bucket, trigger, and permission before creating them.' : mode === 'api' ? 'Review the Lambda, REST API, route, deployment, and permission before creating them.' : mode === 'scheduled' ? 'Review the Lambda and EventBridge schedule before creating them.' : mode === 'sqs' ? 'Review the Lambda, SQS queue, and event source mapping before creating them.' : 'Create a Lambda with your own source and configuration.'}

{mode === 's3' && } {mode === 's3' &&

Creation plan

Create Lambda, create bucket {bucketName || 'your-bucket'}, grant S3 permission to invoke the Lambda, then add an s3:ObjectCreated:* trigger.

} {mode === 'api' &&
} {mode === 'api' &&

Creation plan

Create Lambda, create REST API {apiName || 'your-api'}, add {apiMethod} {apiPath || '/route'}, grant API Gateway permission, and deploy stage {apiStage || 'dev'}.

} {mode === 'scheduled' &&
} {mode === 'scheduled' &&

Creation plan

Create Lambda, create EventBridge rule {ruleName || 'your-rule'}, then invoke it on {scheduleExpression || 'your schedule'}.

} {mode === 'sqs' &&
} {mode === 'sqs' &&

Creation plan

Create Lambda, create SQS queue {queueName || 'your-queue'}, then create an event source mapping with a batch size of {batchSize || '10'}.

} {blueprintError &&

{blueprintError}

} { if (mode === 's3' && !bucketName.trim()) { setBlueprintError('S3 bucket name is required.') return } if (mode === 'api' && (!apiName.trim() || !apiPath.trim() || !apiStage.trim())) { setBlueprintError('API name, route path, and stage are required.') return } if (mode === 'scheduled' && (!ruleName.trim() || !scheduleExpression.trim())) { setBlueprintError('Rule name and schedule expression are required.') return } const parsedBatchSize = Number(batchSize) if (mode === 'sqs' && (!queueName.trim() || !Number.isInteger(parsedBatchSize) || parsedBatchSize < 1 || parsedBatchSize > 10)) { setBlueprintError('SQS queue name and a batch size from 1 to 10 are required.') return } onSubmit(mode === 's3' ? {...values, blueprintBucketName: bucketName} : mode === 'api' ? {...values, blueprintApiName: apiName, blueprintApiPath: apiPath, blueprintApiMethod: apiMethod, blueprintApiStage: apiStage} : mode === 'scheduled' ? {...values, blueprintRuleName: ruleName, blueprintScheduleExpression: scheduleExpression} : mode === 'sqs' ? {...values, blueprintQueueName: queueName, blueprintSqsBatchSize: parsedBatchSize} : values) }}/>
} const S3_UPLOAD_PROCESSOR_SOURCE = `exports.handler = async (event) => { for (const record of event.Records ?? []) { console.log("S3 object created:", { bucket: record.s3.bucket.name, key: record.s3.object.key, event: record.eventName, }); } return { statusCode: 200, body: JSON.stringify({message: "Processed S3 event"}) }; };` const REST_LAMBDA_API_SOURCE = `exports.handler = async (event) => { return { statusCode: 200, headers: { "content-type": "application/json" }, body: JSON.stringify({ message: "Hello from Lambda", method: event.httpMethod, path: event.path, }), }; };` const SCHEDULED_LAMBDA_SOURCE = `exports.handler = async (event) => { console.log("Scheduled event received:", { id: event.id, time: event.time, source: event.source, }); return { statusCode: 200, body: JSON.stringify({message: "Scheduled task completed"}) }; };` const SQS_WORKER_SOURCE = `exports.handler = async (event) => { for (const record of event.Records ?? []) { console.log("SQS message received:", { messageId: record.messageId, body: record.body, attributes: record.attributes, }); } return { statusCode: 200, body: JSON.stringify({message: "Processed SQS batch"}) }; };`