From c74bcef83464c3f6ccad3d46cc1d701cf9ca2106 Mon Sep 17 00:00:00 2001 From: s444501 Date: Thu, 19 Jan 2023 15:25:26 +0100 Subject: [PATCH] aws --- .gitignore | 1 + README.md | 15 +++++++ deploy.sh | 6 +++ main.py | 114 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 136 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 deploy.sh create mode 100644 main.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f85c6b1 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +config.py \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..c0541ce --- /dev/null +++ b/README.md @@ -0,0 +1,15 @@ +## Przed uruchomieniem +W głównym katalogu repozytorium utwórz plik ``config.py`` + +```python +aws_access_key_id = '' +aws_secret_access_key = '' +aws_session_token = '' +VpcId = '' +``` +Wypełnij go swoimi danymi. + +Upewnij się, że zainstalowany jest ``Python3.9`` lub nowszy oraz ``pip`` + +## Uruchamianie +Webserwis uruchamiany jest poprzez wywołanie pliku ``deploy.sh`` diff --git a/deploy.sh b/deploy.sh new file mode 100644 index 0000000..a27c6ed --- /dev/null +++ b/deploy.sh @@ -0,0 +1,6 @@ +chmod +x main.py +echo "Creating virtual environment..." +python3 -m venv venv +source venv/bin/activate +pip install -q boto3 +./main.py diff --git a/main.py b/main.py new file mode 100644 index 0000000..b437388 --- /dev/null +++ b/main.py @@ -0,0 +1,114 @@ +import time + +import boto3 + +from config import aws_access_key_id, aws_secret_access_key, aws_session_token, VpcId + +cloud_init = r'''#cloud-config +runcmd: + - git clone https://git.wmi.amu.edu.pl/s444501/chmury-loadbalancer.git + - cd chmury-loadbalancer + - chmod +x webservice + - ./webservice +''' + +NAME = 'zadanie-loadbalancer' +WORKERS = 5 + +session = boto3.Session( + aws_access_key_id=aws_access_key_id, + aws_secret_access_key=aws_secret_access_key, + aws_session_token=aws_session_token, + region_name='us-east-1', +) + +ec2 = session.resource('ec2') +print('Creating security group...') +security_group = ec2.create_security_group(Description=NAME, + GroupName=NAME, + TagSpecifications=[{ + 'ResourceType': 'security-group', + 'Tags': [{ + 'Key': NAME, + 'Value': NAME + }]}]) +security_group.authorize_ingress( + GroupId=security_group.group_id, + CidrIp='0.0.0.0/0', + IpProtocol='tcp', + FromPort=8080, + ToPort=8080) +security_group.authorize_ingress( + GroupId=security_group.group_id, + CidrIp='0.0.0.0/0', + IpProtocol='tcp', + FromPort=80, + ToPort=80) + +print('Initializing instances...') +instances = ec2.create_instances( + ImageId='ami-00874d747dde814fa', # Ubuntu + MinCount=WORKERS, + MaxCount=WORKERS, + InstanceType='t2.small', + UserData=cloud_init, + SecurityGroups=[security_group.group_name], + TagSpecifications=[{ + 'ResourceType': 'instance', + 'Tags': [{ + 'Key': NAME, + 'Value': NAME + }]}]) + +print('Waiting for instance status to be "running"...') +while not all([ec2.Instance(instance.id).state['Code'] == 16 for instance in instances]): + time.sleep(1) +print('Instances ready!') + +print('Allocating elastic ip...') +elastic_ip = session.client('ec2').allocate_address(Domain='vpc', + TagSpecifications=[{ + 'ResourceType': 'elastic-ip', + 'Tags': [{ + 'Key': NAME, + 'Value': NAME + }]}]) + +print('Creating load balancer...') +elbv2 = session.client('elbv2') +load_balancer = elbv2.create_load_balancer( + Name=NAME, + Type='network', + SubnetMappings=[{ + 'SubnetId': instances[0].subnet_id, + 'AllocationId': elastic_ip['AllocationId'] + }]) + +print('Creating a target group...') +target_group = elbv2.create_target_group( + Name=NAME, + Protocol='TCP', + Port=8080, + VpcId=VpcId) + +print('Registering targets...') +targets = [{'Id': instance.id, 'Port': 8080} for instance in instances] +elbv2.register_targets( + TargetGroupArn=target_group['TargetGroups'][0]['TargetGroupArn'], + Targets=targets) + +print('Creating a listener...') +elbv2.create_listener( + LoadBalancerArn=load_balancer['LoadBalancers'][0]['LoadBalancerArn'], + Protocol='TCP', + Port=8080, + DefaultActions=[{ + 'Type': 'forward', + 'TargetGroupArn': target_group['TargetGroups'][0]['TargetGroupArn'] + }]) + +print('Waiting for load balancer to become available...') +waiter = elbv2.get_waiter('load_balancer_available') +waiter.wait(LoadBalancerArns=[load_balancer['LoadBalancers'][0]['LoadBalancerArn']]) + +print(f'Webserwis będzie dostępny w ciągu 1 minuty pod adresem http://{elastic_ip["PublicIp"]}:8080/factors/')