agent: |
lHvPhITJlz6mtgVVpretCreate security groups within the VPC in the us-west-2 region for the EKS cluster.
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.
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 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