Sign in
agent:

Create a VPC, subnets, and security groups required for the EKS cluster in the us-west-2 region.

There was a problem that the LLM was not able to address. Please rephrase your prompt and try again.

This script creates a VPC, subnets, and security groups required for the EKS cluster in the us-west-2 region, ensuring no conflicts with existing resources.

  1. 1

    Create a VPC in the us-west-2 region for the EKS cluster.

    There was a problem that the LLM was not able to address. Please rephrase your prompt and try again.

    This script checks for existing VPCs to avoid conflicts and creates a new VPC in the us-west-2 region if it doesn't already exist.

    import boto3 # Initialize a session using Amazon EC2 session = boto3.Session( aws_access_key_id=getEnvVar('AWS_ACCESS_KEY_ID'), aws_secret_access_key=getEnvVar('AWS_SECRET_ACCESS_KEY'), region_name=region ) # Create EC2 client ec2_client = session.client('ec2') # Check existing VPCs vpcs = ec2_client.describe_vpcs() vpc_id = None # Reuse if VPC already exists for vpc in vpcs['Vpcs']: if vpc['CidrBlock'] == vpc_cidr: vpc_id = vpc['VpcId'] print(f"Using existing VPC: {vpc_id}") break # Otherwise, create new VPC if not vpc_id: if len(vpcs['Vpcs']) >= 5: raise Exception('VPC limit exceeded. Please delete unused VPCs or increase the limit.') vpc_response = ec2_client.create_vpc( CidrBlock=vpc_cidr, TagSpecifications=[{ 'ResourceType': 'vpc', 'Tags': [{'Key': 'Name', 'Value': 'MyEKS-VPC'}] }] ) vpc_id = vpc_response['Vpc']['VpcId'] ec2_client.modify_vpc_attribute(VpcId=vpc_id, EnableDnsSupport={'Value': True}) ec2_client.modify_vpc_attribute(VpcId=vpc_id, EnableDnsHostnames={'Value': True}) print(f"Created VPC: {vpc_id}") # Create Internet Gateway igw_response = ec2_client.create_internet_gateway() igw_id = igw_response['InternetGateway']['InternetGatewayId'] ec2_client.attach_internet_gateway(InternetGatewayId=igw_id, VpcId=vpc_id) print(f"Attached Internet Gateway: {igw_id}") # Create route table and default route to IGW route_table = ec2_client.create_route_table(VpcId=vpc_id) rtb_id = route_table['RouteTable']['RouteTableId'] ec2_client.create_route( RouteTableId=rtb_id, DestinationCidrBlock='0.0.0.0/0', GatewayId=igw_id ) print(f"Created route table with IGW route: {rtb_id}") print('Final VPC ID:', vpc_id)
    copied
    1
  2. 2

    Create subnets within the VPC in the us-west-2 region for the EKS cluster.

    There was a problem that the LLM was not able to address. Please rephrase your prompt and try again.

    This script creates new subnets within the specified VPC in the us-west-2 region for the EKS cluster, finding non-conflicting CIDRs dynamically by iterating through potential CIDR blocks starting from a higher range to avoid conflicts.

    import boto3 import json # Initialize a session using Amazon EC2 session = boto3.Session( aws_access_key_id=getEnvVar('AWS_ACCESS_KEY_ID'), aws_secret_access_key=getEnvVar('AWS_SECRET_ACCESS_KEY'), region_name=region ) # Create EC2 client ec2_client = session.client('ec2') # Describe existing subnets to avoid conflicts existing_subnets = ec2_client.describe_subnets(Filters=[{'Name': 'vpc-id', 'Values': [vpc_id]}]) existing_cidrs = [subnet['CidrBlock'] for subnet in existing_subnets['Subnets']] # Function to find a non-conflicting CIDR def find_non_conflicting_cidr(existing_cidrs, base_cidr, start_octet): for i in range(start_octet, 256): new_cidr = f"{base_cidr}.{i}.0/24" if new_cidr not in existing_cidrs: return new_cidr raise Exception("Unable to find a non-conflicting CIDR") # Base CIDR for subnets base_cidr = "10.0" # Find CIDRs for 2 public and 2 private subnets public_subnet_cidrs, private_subnet_cidrs = [], [] for i in range(50, 256): new_cidr = f"{base_cidr}.{i}.0/24" if new_cidr not in existing_cidrs: public_subnet_cidrs.append(new_cidr) if len(public_subnet_cidrs) == 2: break for i in range(60, 256): new_cidr = f"{base_cidr}.{i}.0/24" if new_cidr not in existing_cidrs: private_subnet_cidrs.append(new_cidr) if len(private_subnet_cidrs) == 2: break # Get available AZs in the region (first 2) az_response = ec2_client.describe_availability_zones( Filters=[{'Name': 'region-name', 'Values': [region]}, {'Name': 'state', 'Values': ['available']}] ) available_azs = sorted([az['ZoneName'] for az in az_response['AvailabilityZones']])[:2] if len(available_azs) < 2: raise Exception("At least 2 Availability Zones are required.") # Create public subnets in distinct AZs public_subnet_ids = [] for i, cidr in enumerate(public_subnet_cidrs): az = available_azs[i] subnet_response = ec2_client.create_subnet( VpcId=vpc_id, CidrBlock=cidr, AvailabilityZone=az ) subnet_id = subnet_response['Subnet']['SubnetId'] public_subnet_ids.append(subnet_id) # Enable auto-assign public IP on launch ec2_client.modify_subnet_attribute( SubnetId=subnet_id, MapPublicIpOnLaunch={'Value': True} ) # Associate subnet with the route table connected to the Internet Gateway ec2_client.associate_route_table( SubnetId=subnet_id, RouteTableId=rtb_id ) # Create private subnets in distinct AZs private_subnet_ids = [] for i, cidr in enumerate(private_subnet_cidrs): az = available_azs[i] subnet_response = ec2_client.create_subnet( VpcId=vpc_id, CidrBlock=cidr, AvailabilityZone=az ) private_subnet_ids.append(subnet_response['Subnet']['SubnetId']) # Output print('Public Subnet IDs:', json.dumps(public_subnet_ids, indent=4)) print('Private Subnet IDs:', json.dumps(private_subnet_ids, indent=4))
    copied
    2
  3. 3

    Create security groups within the VPC in the us-west-2 region for the EKS cluster.

    There was a problem that the LLM was not able to address. Please rephrase your prompt and try again.

    This script checks if the security group already exists within the specified VPC in the us-west-2 region for the EKS cluster. If it doesn't exist, it creates the security group and authorizes inbound traffic.

    import boto3 # Initialize a session using Amazon EC2 session = boto3.Session( aws_access_key_id=getEnvVar('AWS_ACCESS_KEY_ID'), aws_secret_access_key=getEnvVar('AWS_SECRET_ACCESS_KEY'), region_name=region ) # Create EC2 client ec2_client = session.client('ec2') # Check if the security group already exists existing_sgs = ec2_client.describe_security_groups(Filters=[{'Name': 'vpc-id', 'Values': [vpc_id]}]) security_group_id = None for sg in existing_sgs['SecurityGroups']: if sg['GroupName'] == 'EKS-Security-Group': security_group_id = sg['GroupId'] break # Create security group if it doesn't exist if not security_group_id: security_group_response = ec2_client.create_security_group( GroupName='EKS-Security-Group', Description='Security group for EKS cluster', VpcId=vpc_id ) security_group_id = security_group_response['GroupId'] # Authorize inbound traffic for security group ec2_client.authorize_security_group_ingress( GroupId=security_group_id, IpPermissions=[ { 'IpProtocol': '-1', 'IpRanges': [{'CidrIp': '0.0.0.0/0'}] } ] ) print('Security Group ID:', security_group_id)
    copied
    3