This commit is contained in:
Adam Stelmaszyk 2025-02-10 17:28:39 +01:00
parent d4178fc207
commit 4ecb7bf70e
6 changed files with 396 additions and 0 deletions

6
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,6 @@
{
"cSpell.words": [
"elbv",
"Webservice"
]
}

Binary file not shown.

35
autoscalling.py Normal file
View File

@ -0,0 +1,35 @@
from aws_webservice import PREFIX, REGION, AWSWebservice
if __name__ == "__main__":
service = AWSWebservice(
aws_access_key_id="",
aws_secret_access_key="",
aws_session_token="",
)
service.init_ec2()
service.init_elbv2()
service.init_autoscaling()
service.init_cloudwatch()
vpc = service.create_vpc()
subnet = service.create_subnet(vpc, '10.0.1.0/24', f"{REGION}a")
subnet2 = service.create_subnet(vpc, '10.0.2.0/24', f"{REGION}b")
igw = service.create_internet_gateway(vpc)
rt_id= service.create_route_table(vpc, subnet, igw)
key = service.create_key_pair(f"{PREFIX}-key")
sg = service.create_security_group(vpc)
lt = service.create_launch_template(key, sg)
tg = service.create_target_group(vpc)
lb = service.create_load_balancer([subnet, subnet2], sg)
listener = service.create_listener(lb, tg)
asg_arn = service.create_auto_scaling_group(lt, tg, subnet, subnet2)
sp = service.put_scaling_policy()
service.create_cloudwatch_alarm(sp)
dns = service.get_dns(lb)
try:
service.save_in_file_names(vpc, [subnet, subnet2], igw, rt_id,key,sg,lt,tg,
lb,'',dns)
except:
print('problem with saving')

18
aws_names.json Normal file
View File

@ -0,0 +1,18 @@
{
"vpc_id": "",
"subnet_ids": [
"test",
"test",
""
],
"igw_id": "",
"route_table_id": "",
"key_name": "",
"security_group_id": "",
"launch_template_id": "",
"target_group_arn": "",
"load_balancer_arn": "",
"listener_arn": "",
"asg_name": "s464953-asg",
"dns_name": ""
}

303
aws_webservice.py Normal file
View File

@ -0,0 +1,303 @@
import base64
import json
import random
import string
import time
import boto3
REPOSITORY_URL="https://git.wmi.amu.edu.pl/s495731/public-cloud-aws"
PREFIX = "s495731"
AMI_ID = "ami-0b5eea76982371e91"
INSTANCE_TYPE = "t2.micro"
SECURITY_GROUP_NAME = f"{PREFIX}-sg"
REGION = "us-east-1"
NAME_FILE_PATH="aws_names.json"
USER_DATA=f"""
#!/bin/bash
sudo yum update -y
sudo yum install -y git
sudo git clone {REPOSITORY_URL}
cd aws
sudo chmod +x webservice
./webservice
"""
class AWSWebservice:
def __init__(self, aws_access_key_id, aws_secret_access_key,
aws_session_token):
self.aws_access_key_id = aws_access_key_id
self.aws_secret_access_key = aws_secret_access_key
self.aws_session_token = aws_session_token
def init_client(self, name):
return boto3.client(name, region_name=REGION,
aws_access_key_id=self.aws_access_key_id,
aws_secret_access_key=self.aws_secret_access_key,
aws_session_token=self.aws_session_token,
)
def init_ec2(self):
self.ec2 = self.init_client('ec2')
def init_elbv2(self):
self.elbv2 = self.init_client('elbv2')
def init_autoscaling(self):
self.autoscaling = self.init_client('autoscaling')
def init_cloudwatch(self):
self.cloudwatch = self.init_client('cloudwatch')
def create_vpc(self):
vpc_id = self.ec2.create_vpc(CidrBlock='10.0.0.0/16')['Vpc']['VpcId']
self.ec2.modify_vpc_attribute(VpcId=vpc_id, EnableDnsSupport={'Value': True})
self.ec2.modify_vpc_attribute(VpcId=vpc_id, EnableDnsHostnames={'Value': True})
return vpc_id
def create_subnet(self, vpc_id, cidr, az):
subnet_id = self.ec2.create_subnet(VpcId=vpc_id, CidrBlock=cidr, AvailabilityZone=az)['Subnet']['SubnetId']
self.ec2.modify_subnet_attribute(SubnetId=subnet_id, MapPublicIpOnLaunch={'Value': True})
return subnet_id
def create_internet_gateway(self, vpc_id):
internet_gateway_id = self.ec2.create_internet_gateway()['InternetGateway']['InternetGatewayId']
self.ec2.attach_internet_gateway(InternetGatewayId=internet_gateway_id, VpcId=vpc_id)
return internet_gateway_id
def create_route_table(self, vpc_id, subnet_id, igw_id):
rt_id = self.ec2.create_route_table(VpcId=vpc_id)['RouteTable']['RouteTableId']
self.ec2.create_route(
RouteTableId=rt_id,
DestinationCidrBlock='0.0.0.0/0',
GatewayId=igw_id
)
self.ec2.associate_route_table(RouteTableId=rt_id, SubnetId=subnet_id)
return rt_id
def create_key_pair(self, name):
random_suffix = ''.join(random.choices(string.ascii_lowercase + string.digits, k=8))
unique_key = f"{name}-{random_suffix}"
key_material = self.ec2.create_key_pair(KeyName=unique_key)['KeyMaterial']
with open(f"{unique_key}.pem", 'w') as file:
file.write(key_material)
return unique_key
def create_security_group(self, vpc_id):
security_group_id = self.ec2.create_security_group(GroupName=SECURITY_GROUP_NAME, Description="Security group", VpcId=vpc_id)['GroupId']
self.ec2.authorize_security_group_ingress(
GroupId=security_group_id,
IpPermissions=[
{
'IpProtocol': 'tcp',
'FromPort': 22,
'ToPort': 22,
'IpRanges': [{'CidrIp': '0.0.0.0/0', 'Description': 'SSH access'}]
},
{
'IpProtocol': 'tcp',
'FromPort': 8080,
'ToPort': 8080,
'IpRanges': [
{
'CidrIp': '0.0.0.0/0'
}
]
}
])
return security_group_id
def create_launch_template(self, key_name, sg_id):
launch_template = self.ec2.create_launch_template(
LaunchTemplateName=f"{PREFIX}-lt",
LaunchTemplateData=
{
'ImageId': AMI_ID,
'InstanceType': INSTANCE_TYPE,
'SecurityGroupIds': [sg_id],
'KeyName': key_name,
'UserData': base64.b64encode(USER_DATA.encode('utf-8')).decode('utf-8')
}
)['LaunchTemplate']['LaunchTemplateId']
return launch_template
def create_target_group(self, vpc_id):
tg_arn = self.elbv2.create_target_group(
Name=f"{PREFIX}-tg",
Protocol='HTTP',
Port=8080,
VpcId=vpc_id,
TargetType='instance',
IpAddressType='ipv4',
HealthCheckProtocol='HTTP',
HealthCheckPort='8080',
HealthCheckPath='/factors/6',
HealthCheckIntervalSeconds=30,
HealthCheckTimeoutSeconds=5,
HealthyThresholdCount=3,
UnhealthyThresholdCount=3
)['TargetGroups'][0]['TargetGroupArn']
return tg_arn
def create_load_balancer(self, subnets, sg_id):
lb_arn = self.elbv2.create_load_balancer(
Name=f"{PREFIX}-lb",
Subnets=subnets,
SecurityGroups=[sg_id],
Scheme='internet-facing',
Type='application',
IpAddressType='ipv4'
)['LoadBalancers'][0]['LoadBalancerArn']
return lb_arn
def create_listener(self, lb_arn, tg_arn):
listener_arn = self.elbv2.create_listener(
LoadBalancerArn=lb_arn,
Protocol='HTTP',
Port=8080,
DefaultActions=[
{
'Type': 'forward',
'TargetGroupArn': tg_arn
}
]
)['Listeners'][0]['ListenerArn']
return listener_arn
def create_auto_scaling_group(self, launch_template_id, target_group_arn, subnet_id_1, subnet_id_2):
self.autoscaling.create_auto_scaling_group(
AutoScalingGroupName=f"{PREFIX}-asg",
LaunchTemplate={
'LaunchTemplateId': launch_template_id,
'Version': '$Latest'
},
MinSize=2,
MaxSize=5,
DesiredCapacity=2,
TargetGroupARNs=[target_group_arn],
AvailabilityZones=[f"{REGION}a", f"{REGION}b"],
HealthCheckType='EC2',
HealthCheckGracePeriod=90,
VPCZoneIdentifier=f"{subnet_id_1},{subnet_id_2}",
Tags=[
{
'Key': 'Name',
'Value': f"{PREFIX}-asg",
'PropagateAtLaunch': True
}
]
)
describe_response = self.autoscaling.describe_auto_scaling_groups(
AutoScalingGroupNames=[f"{PREFIX}-asg"]
)
asg_arn = describe_response['AutoScalingGroups'][0]['AutoScalingGroupARN']
return asg_arn
def put_scaling_policy(self):
policy = self.autoscaling.put_scaling_policy(
AutoScalingGroupName=f"{PREFIX}-asg",
PolicyName=f"{PREFIX}-scale-policy-cpu",
PolicyType='SimpleScaling',
AdjustmentType='ChangeInCapacity',
ScalingAdjustment=1,
Cooldown=180
)
return policy['PolicyARN']
def create_cloudwatch_alarm(self, sp_arn):
self.cloudwatch.put_metric_alarm(
AlarmName=f"{PREFIX}-cpu-utilization-alarm",
ComparisonOperator='GreaterThanOrEqualToThreshold',
EvaluationPeriods=1,
MetricName='CPUUtilization',
Namespace='AWS/EC2',
Period=60,
Statistic='Average',
Threshold=30.0,
ActionsEnabled=True,
AlarmActions=[sp_arn],
AlarmDescription='Alarm to scale out/in based on CPU utilization',
Dimensions=[{
'Name': 'AutoScalingGroupName',
'Value': f"{PREFIX}-asg"
}]
)
def create_instances(self, lt_id, subnet_id, tg):
ids = [i['InstanceId'] for i in self.ec2.run_instances(
LaunchTemplate={'LaunchTemplateId': lt_id, 'Version': '$Latest'},
MinCount=2, MaxCount=2, SubnetId=subnet_id)['Instances']]
while True:
states = [r['Instances'][0]['State']['Name'] for r in self.ec2.describe_instances(InstanceIds=ids)['Reservations']]
if all(s == 'running' for s in states):
break
time.sleep(10)
self.elbv2.register_targets(TargetGroupArn=tg, Targets=[{'Id': i} for i in ids])
return ids
def get_dns(self, lb):
dns = self.elbv2.describe_load_balancers(LoadBalancerArns=[lb])['LoadBalancers'][0]['DNSName']
print(dns)
return dns
def save_in_file_names(self, vpc_id, subnet_ids, igw_id,
route_table_id, key_name, security_group_id,
launch_template_id, target_group_arn,
load_balancer_arn, listener_arn,
dns_name):
aws_resources = {
'vpc_id': vpc_id,
'subnet_ids': subnet_ids,
'igw_id': igw_id,
'route_table_id': route_table_id,
'key_name': key_name,
'security_group_id': security_group_id,
'launch_template_id': launch_template_id,
'target_group_arn': target_group_arn,
'load_balancer_arn': load_balancer_arn,
'listener_arn': listener_arn,
'asg_name': f"{PREFIX}-asg",
'dns_name': dns_name,
}
with open(NAME_FILE_PATH, 'w') as file:
json.dump(aws_resources, file, indent=4)
def delete_resources(self, resources):
with open(NAME_FILE_PATH, "r") as file:
resources = json.load(file)
self.ec2.terminate_instances(InstanceIds= resources["instance_ids"])
self.elbv2.delete_load_balancer(LoadBalancerArn= resources["load_balancer_arn"])
self.elbv2.delete_target_group(TargetGroupArn=resources["target_group_arn"])
self.ec2.revoke_security_group_ingress(
GroupId=resources["security_group_id"],
IpPermissions=[
{
'IpProtocol': 'tcp',
'FromPort': 22,
'ToPort': 22,
'IpRanges': [{'CidrIp': '0.0.0.0/0'}]
},
{
'IpProtocol': 'tcp',
'FromPort': 8080,
'ToPort': 8080,
'IpRanges': [{'CidrIp': '0.0.0.0/0'}]
}
]
)
self.ec2.delete_security_group(GroupId=resources["security_group_id"])
self.ec2.delete_key_pair(KeyName=resources["key_name"])
self.ec2.detach_internet_gateway(InternetGatewayId=resources["igw_id"], VpcId=resources["vpc_id"])
self.ec2.delete_internet_gateway(InternetGatewayId=resources["igw_id"])
self.ec2.delete_route_table(RouteTableId=resources["route_table_id"])
self.ec2.delete_subnet(SubnetId=resources["subnet_ids"][0])
self.ec2.delete_subnet(SubnetId=resources["subnet_ids"][1])
self.ec2.delete_vpc(VpcId=resources["vpc_id"])

34
only_load_balancer.py Normal file
View File

@ -0,0 +1,34 @@
from aws_webservice import PREFIX, REGION, AWSWebservice
if __name__ == "__main__":
service = AWSWebservice(
aws_access_key_id="",
aws_secret_access_key="",
aws_session_token="",
)
service.init_ec2()
service.init_elbv2()
vpc = service.create_vpc()
subnet = service.create_subnet(vpc, '10.0.1.0/24', f"{REGION}a")
subnet2 = service.create_subnet(vpc, '10.0.2.0/24', f"{REGION}b")
igw = service.create_internet_gateway(vpc)
rt_id = service.create_route_table(vpc, subnet, igw)
key = service.create_key_pair(f"{PREFIX}-key")
sg = service.create_security_group(vpc)
lt = service.create_launch_template(key, sg)
tg = service.create_target_group(vpc)
lb = service.create_load_balancer([subnet, subnet2], sg)
listener = service.create_listener(lb, tg)
ids = service.create_instances(lt, subnet, tg)
dns = service.get_dns(lb)
try:
service.save_in_file_names(vpc, [subnet, subnet2], igw, rt_id,key,sg,lt,tg,
lb,ids,dns)
except:
print('problem with saving')