agent: |
LnpLuA9mpa4Xqcvd9S89Create a VPC in the us-west-2 region for the EKS cluster.
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.
inputs
outputs
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