aws_elb
Automate AWS Elastic Load Balancer management using Ansible. Create, delete, or list load balancers across regions, configure subnets, security groups, listeners, and health checks.
Instructions
Manage AWS Elastic Load Balancers
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| action | Yes | ||
| healthCheck | No | ||
| lbType | No | application | |
| listeners | No | ||
| name | No | ||
| region | Yes | ||
| scheme | No | ||
| securityGroups | No | ||
| subnets | No | ||
| tags | No | ||
| targetGroups | No |
Implementation Reference
- Main handler function for 'aws_elb' tool. Generates dynamic Ansible playbooks to manage AWS Elastic Load Balancers (list, create, delete) based on input action and parameters.export async function elbOperations(args: ELBOptions): Promise<string> { await verifyAwsCredentials(); const { action, region, name, lbType = 'application', scheme, subnets, securityGroups, listeners, healthCheck, tags, targetGroups } = args; // Determine module based on lbType let moduleName: string; let infoModuleName: string; switch (lbType) { case 'application': moduleName = 'amazon.aws.elb_application_lb'; infoModuleName = 'amazon.aws.elb_application_lb_info'; break; case 'network': moduleName = 'amazon.aws.elb_network_lb'; infoModuleName = 'amazon.aws.elb_network_lb_info'; break; case 'classic': moduleName = 'amazon.aws.elb_classic_lb'; infoModuleName = 'amazon.aws.elb_classic_lb_info'; break; default: throw new AnsibleError(`Unsupported ELB type: ${lbType}`); } let playbookContent = `--- - name: AWS ELB ${action} operation (${lbType}) hosts: localhost connection: local gather_facts: no tasks:`; switch (action) { case 'list': playbookContent += ` - name: List ${lbType} load balancers ${infoModuleName}: region: "${region}" register: elb_info - name: Display load balancers debug: var: elb_info`; // Adjust var based on actual module output if needed break; case 'create': playbookContent += ` - name: Create ${lbType} load balancer ${moduleName}: region: "${region}" name: "${name}" state: present ${formatYamlParams({ scheme, subnets, security_groups: securityGroups, listeners, // May need adjustment for different LB types health_check: healthCheck, // May need adjustment tags, target_groups: targetGroups // For Application/Network LBs })} register: elb_result - name: Display load balancer details debug: var: elb_result`; break; case 'delete': playbookContent += ` - name: Delete ${lbType} load balancer ${moduleName}: region: "${region}" name: "${name}" state: absent register: elb_delete - name: Display deletion result debug: var: elb_delete`; break; default: throw new AnsibleError(`Unsupported ELB action: ${action}`); } // Execute the generated playbook return executeAwsPlaybook(`elb-${action}`, playbookContent); }
- Zod schema definition for 'aws_elb' tool inputs (ELBOptions), including action enum (list/create/delete), region, name, lbType (application/network/classic), subnets, etc.export const ELBSchema = z.object({ action: ELBActionEnum, region: z.string().min(1, 'AWS region is required'), name: z.string().optional(), lbType: z.enum(['classic', 'application', 'network']).optional().default('application'), scheme: z.string().optional(), subnets: z.array(z.string()).optional(), securityGroups: z.array(z.string()).optional(), listeners: z.array(z.any()).optional(), // Consider defining a more specific listener schema healthCheck: z.any().optional(), // Consider defining a more specific health check schema tags: z.record(z.string()).optional(), targetGroups: z.array(z.any()).optional() // Added based on usage in aws.ts. Consider a specific schema. }); export type ELBOptions = z.infer<typeof ELBSchema>;
- src/sysoperator/index.ts:126-130 (registration)Tool registration in the MCP server: maps 'aws_elb' to its description, schema (aws.ELBSchema), and handler (aws.elbOperations).aws_elb: { description: 'Manage AWS Elastic Load Balancers', schema: aws.ELBSchema, handler: aws.elbOperations, },
- Zod enum for ELB actions used in the ELBSchema.export const ELBActionEnum = z.enum(['list', 'create', 'delete']); export type ELBAction = z.infer<typeof ELBActionEnum>;
- Helper function executeAwsPlaybook used by the handler to run the generated Ansible playbook in a temp directory.async function executeAwsPlaybook( operationName: string, playbookContent: string, extraParams: string = '', tempFiles: { filename: string, content: string }[] = [] // For additional files like templates, policies ): Promise<string> { let tempDir: string | undefined; try { // Create a unique temporary directory tempDir = await createTempDirectory(`ansible-aws-${operationName}`); // Write the main playbook file const playbookPath = await writeTempFile(tempDir, 'playbook.yml', playbookContent); // Write any additional temporary files for (const file of tempFiles) { await writeTempFile(tempDir, file.filename, file.content); } // Build the command const command = `ansible-playbook ${playbookPath} ${extraParams}`; console.error(`Executing: ${command}`); // Execute the playbook asynchronously const { stdout, stderr } = await execAsync(command); // Return stdout, or a success message if stdout is empty return stdout || `${operationName} completed successfully (no output).`; } catch (error: any) { // Handle execution errors const errorMessage = error.stderr || error.message || 'Unknown error'; throw new AnsibleExecutionError(`Ansible execution failed for ${operationName}: ${errorMessage}`, error.stderr); } finally { // Ensure cleanup happens even if errors occur if (tempDir) { await cleanupTempDirectory(tempDir); } } }