Skip to main content
Glama

aws_lambda

Manage AWS Lambda functions by listing, creating, updating, deleting, or invoking serverless functions to automate cloud operations.

Instructions

Manage AWS Lambda functions

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
actionYes
regionYes
nameNo
zipFileNo
s3BucketNo
s3KeyNo
functionCodeNo
runtimeNo
handlerNo
roleNo
descriptionNo
timeoutNo
memorySizeNo
environmentNo
tagsNo
payloadNo

Implementation Reference

  • Main handler function for aws_lambda tool. Generates dynamic Ansible playbooks using amazon.aws.lambda module to perform operations like list, create/update, delete, invoke on 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 (LambdaSchema) defining the input parameters and validation for the aws_lambda tool.
    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>;
  • Tool registration in the central toolDefinitions map used by the MCP server to handle tool calls.
    aws_lambda: {
      description: 'Manage AWS Lambda functions',
      schema: aws.LambdaSchema,
      handler: aws.lambdaOperations,
    },

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/tarnover/mcp-sysoperator'

If you have feedback or need assistance with the MCP directory API, please join our Discord server