aws_lambda
Manage AWS Lambda functions via MCP SysOperator. Perform actions like create, update, delete, invoke, and list functions across specified regions with customizable runtime, handler, role, and environment settings.
Instructions
Manage AWS Lambda functions
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| action | Yes | ||
| description | No | ||
| environment | No | ||
| functionCode | No | ||
| handler | No | ||
| memorySize | No | ||
| name | No | ||
| payload | No | ||
| region | Yes | ||
| role | No | ||
| runtime | No | ||
| s3Bucket | No | ||
| s3Key | No | ||
| tags | No | ||
| timeout | No | ||
| zipFile | No |
Implementation Reference
- The main handler function for the 'aws_lambda' tool. Generates and executes Ansible playbooks using amazon.aws.lambda modules for actions like list, create, update, delete, invoke AWS Lambda functions.export async function lambdaOperations(args: LambdaOptions): Promise<string> { await verifyAwsCredentials(); const { action, region, name, zipFile, s3Bucket, s3Key, functionCode, runtime, handler, role, description, timeout, memorySize, environment, tags, payload } = args; const tempFiles: { filename: string, content: string }[] = []; let codeParams = ''; let zipPath: string | undefined; // To track zip file for cleanup if (zipFile) { // Assuming zipFile is a path accessible to the server running this code codeParams = `zip_file: "${zipFile}"`; } else if (s3Bucket && s3Key) { codeParams = `s3_bucket: "${s3Bucket}"\n s3_key: "${s3Key}"`; } else if (functionCode) { // If code is provided directly, write it to a temp file and prepare to zip it const codeFilename = 'lambda_function.py'; // Assuming Python, adjust if needed tempFiles.push({ filename: codeFilename, content: functionCode }); // We'll need to zip this file before executing Ansible // This requires the 'zip' utility on the server zipPath = 'lambda_function.zip'; // Relative path within temp dir codeParams = `zip_file: "${zipPath}"`; } else if (action === 'create' || action === 'update') { throw new AnsibleError('Lambda code source (zipFile, S3, or functionCode) must be provided for create/update actions.'); } let playbookContent = `--- - name: AWS Lambda ${action} operation hosts: localhost connection: local gather_facts: no tasks:`; switch (action) { case 'list': playbookContent += ` - name: List Lambda functions amazon.aws.lambda_info: region: "${region}" register: lambda_info - name: Display Lambda functions debug: var: lambda_info.functions`; break; case 'create': case 'update': playbookContent += ` - name: ${action === 'create' ? 'Create' : 'Update'} Lambda function amazon.aws.lambda: region: "${region}" name: "${name}" state: present ${codeParams} # Use determined code source params ${formatYamlParams({ runtime, handler, role, description, timeout, memory_size: memorySize, environment_variables: environment, tags })} register: lambda_result - name: Display function details debug: var: lambda_result`; break; case 'delete': playbookContent += ` - name: Delete Lambda function amazon.aws.lambda: region: "${region}" name: "${name}" state: absent register: lambda_delete - name: Display deletion result debug: var: lambda_delete`; break; case 'invoke': playbookContent += ` - name: Invoke Lambda function amazon.aws.lambda_invoke: region: "${region}" function_name: "${name}" invocation_type: RequestResponse # Or Event, DryRun ${formatYamlParams({ payload })} register: lambda_invoke - name: Display invocation result debug: var: lambda_invoke`; break; default: throw new AnsibleError(`Unsupported Lambda action: ${action}`); } // Special handling for zipping code before executing Ansible let tempDir: string | undefined; try { tempDir = await createTempDirectory(`ansible-aws-lambda-${action}`); // Write the main playbook file const playbookPath = await writeTempFile(tempDir, 'playbook.yml', playbookContent); // Write any additional temporary files (like the function code) for (const file of tempFiles) { await writeTempFile(tempDir, file.filename, file.content); } // If we need to zip the code, do it now using execAsync if (zipPath && tempFiles.some(f => f.filename === 'lambda_function.py')) { const codeFilePath = `${tempDir}/lambda_function.py`; const zipFilePath = `${tempDir}/${zipPath}`; const zipCommand = `zip -j "${zipFilePath}" "${codeFilePath}"`; console.error(`Executing: ${zipCommand}`); await execAsync(zipCommand, { cwd: tempDir }); // Run zip in the temp directory } // Build the final Ansible command const command = `ansible-playbook ${playbookPath}`; console.error(`Executing: ${command}`); // Execute the playbook asynchronously const { stdout, stderr } = await execAsync(command); return stdout || `Lambda ${action} completed successfully (no output).`; } catch (error: any) { const errorMessage = error.stderr || error.message || 'Unknown error'; throw new AnsibleExecutionError(`Ansible execution failed for lambda-${action}: ${errorMessage}`, error.stderr); } finally { if (tempDir) { await cleanupTempDirectory(tempDir); } } }
- Zod schema defining input parameters for the aws_lambda tool handler, including action, region, function name, code sources, configuration options.export const LambdaSchema = z.object({ action: LambdaActionEnum, region: z.string().min(1, 'AWS region is required'), name: z.string().optional(), zipFile: z.string().optional(), s3Bucket: z.string().optional(), s3Key: z.string().optional(), functionCode: z.string().optional(), runtime: z.string().optional(), handler: z.string().optional(), role: z.string().optional(), description: z.string().optional(), timeout: z.number().optional(), memorySize: z.number().optional(), environment: z.record(z.string()).optional(), tags: z.record(z.string()).optional(), payload: z.any().optional() // Added based on usage in aws.ts. Payload structure varies. }); export type LambdaOptions = z.infer<typeof LambdaSchema>;
- src/sysoperator/index.ts:131-135 (registration)Registration of the 'aws_lambda' tool in the toolDefinitions map, linking description, schema, and handler.aws_lambda: { description: 'Manage AWS Lambda functions', schema: aws.LambdaSchema, handler: aws.lambdaOperations, },