This commit is contained in:
s444501 2023-01-19 15:25:26 +01:00
commit c74bcef834
4 changed files with 136 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
config.py

15
README.md Normal file
View File

@ -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``

6
deploy.sh Normal file
View File

@ -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

114
main.py Normal file
View File

@ -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/')