in lib/muchos/ec2.py [0:0]
def create_security_group(self):
ec2 = boto3.client("ec2")
sg = self.config.sg_name
create_group = True
group_id = None
try:
response = ec2.describe_security_groups(
Filters=[{"Name": "group-name", "Values": [sg]}]
)
if len(response["SecurityGroups"]) > 0:
group_id = response["SecurityGroups"][0]["GroupId"]
create_group = False
except ClientError:
pass
if create_group:
print("Creating security group " + sg)
request = {
"Description": "Security group created by Muchos",
"GroupName": sg,
}
if self.config.has_option("ec2", "vpc_id"):
request["VpcId"] = self.config.get("ec2", "vpc_id")
response = ec2.create_security_group(**request)
group_id = response["GroupId"]
ec2.authorize_security_group_ingress(
GroupName=sg, SourceSecurityGroupName=sg
)
ec2.authorize_security_group_ingress(
GroupName=sg,
IpProtocol="tcp",
FromPort=22,
ToPort=22,
CidrIp="0.0.0.0/0",
)
return group_id