aws_vpc
Manage AWS Virtual Private Cloud (VPC) networks by listing, creating, or deleting VPCs, configuring CIDR blocks, DNS settings, and subnets directly via the MCP SysOperator platform.
Instructions
Manage AWS VPC networks
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| action | Yes | ||
| cidrBlock | No | ||
| dnsHostnames | No | ||
| dnsSupport | No | ||
| name | No | ||
| region | Yes | ||
| subnets | No | ||
| tags | No | ||
| vpcId | No |
Input Schema (JSON Schema)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {
"action": {
"enum": [
"list",
"create",
"delete"
],
"type": "string"
},
"cidrBlock": {
"type": "string"
},
"dnsHostnames": {
"type": "boolean"
},
"dnsSupport": {
"type": "boolean"
},
"name": {
"type": "string"
},
"region": {
"minLength": 1,
"type": "string"
},
"subnets": {
"items": {
"additionalProperties": false,
"properties": {
"az": {
"type": "string"
},
"cidr": {
"type": "string"
},
"tags": {
"additionalProperties": {
"type": "string"
},
"type": "object"
}
},
"required": [
"cidr"
],
"type": "object"
},
"type": "array"
},
"tags": {
"additionalProperties": {
"type": "string"
},
"type": "object"
},
"vpcId": {
"type": "string"
}
},
"required": [
"action",
"region"
],
"type": "object"
}
Implementation Reference
- Main handler function that destructures input args, generates dynamic Ansible playbook content based on the action (list, create, delete), optionally creates subnets during VPC creation, and executes the playbook using executeAwsPlaybook helper.export async function vpcOperations(args: VPCOptions): Promise<string> { await verifyAwsCredentials(); const { action, region, vpcId, cidrBlock, name, dnsSupport, dnsHostnames, tags, subnets } = args; let playbookContent = `--- - name: AWS VPC ${action} operation hosts: localhost connection: local gather_facts: no tasks:`; switch (action) { case 'list': playbookContent += ` - name: List VPCs amazon.aws.ec2_vpc_net_info: region: "${region}" register: vpc_info - name: Display VPCs debug: var: vpc_info.vpcs`; break; case 'create': playbookContent += ` - name: Create VPC amazon.aws.ec2_vpc_net: region: "${region}" cidr_block: "${cidrBlock}" state: present ${formatYamlParams({ name, dns_support: dnsSupport, dns_hostnames: dnsHostnames, tags })} register: vpc_create - name: Display VPC details debug: var: vpc_create.vpc`; // If subnets are specified, add subnet creation task if (subnets && subnets.length > 0) { playbookContent += ` - name: Create subnets amazon.aws.ec2_vpc_subnet: region: "${region}" vpc_id: "{{ vpc_create.vpc.id }}" cidr: "{{ item.cidr }}" az: "{{ item.az | default(omit) }}" tags: "{{ item.tags | default(omit) }}" state: present loop: ${subnets.map((subnet) => ` - ${JSON.stringify(subnet)}`).join('\n')} register: subnet_create - name: Display subnet details debug: var: subnet_create`; } break; case 'delete': playbookContent += ` - name: Delete VPC amazon.aws.ec2_vpc_net: region: "${region}" vpc_id: "${vpcId}" state: absent register: vpc_delete - name: Display deletion result debug: var: vpc_delete`; break; default: throw new AnsibleError(`Unsupported VPC action: ${action}`); } // Execute the generated playbook return executeAwsPlaybook(`vpc-${action}`, playbookContent); }
- Zod schema defining input validation for aws_vpc tool, including action enum, required region, and optional parameters for VPC management like vpcId, cidrBlock, tags, and subnets array.export const VPCSchema = z.object({ action: VPCActionEnum, region: z.string().min(1, 'AWS region is required'), vpcId: z.string().optional(), cidrBlock: z.string().optional(), name: z.string().optional(), dnsSupport: z.boolean().optional(), dnsHostnames: z.boolean().optional(), tags: z.record(z.string()).optional(), subnets: z.array(z.object({ cidr: z.string(), az: z.string().optional(), tags: z.record(z.string()).optional() })).optional() }); export type VPCOptions = z.infer<typeof VPCSchema>;
- src/sysoperator/index.ts:101-105 (registration)Tool registration in the toolDefinitions map, linking name 'aws_vpc' to its description, input schema, and handler function.aws_vpc: { description: 'Manage AWS VPC networks', schema: aws.VPCSchema, handler: aws.vpcOperations, },
- Helper function to execute dynamically generated Ansible playbooks for AWS operations, handling temp directory creation, file writing, command execution, and cleanup.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); } } }
- Helper function to format parameters as YAML strings for inclusion in dynamically generated Ansible playbooks, handling escaping and different value types.const formatYamlParams = (params: Record<string, any>, indentation: number = 6): string => { // Filter out undefined/null values and format each key-value pair return Object.entries(params) .filter(([_, value]) => value !== undefined && value !== null) .map(([key, value]) => { const indent = ' '.repeat(indentation); let formattedValue; // Format based on value type if (typeof value === 'string') { // Basic YAML string escaping (double quotes, escape backslashes and double quotes) formattedValue = `"${value.replace(/\\/g, '\\\\').replace(/"/g, '\\"')}"`; } else if (Array.isArray(value) || typeof value === 'object') { // Use JSON.stringify for arrays and objects, assuming it's valid YAML subset formattedValue = JSON.stringify(value); } else { formattedValue = value; // Numbers, booleans } return `${indent}${key}: ${formattedValue}`; }) .join('\n'); };