115 lines
3.7 KiB
Python
115 lines
3.7 KiB
Python
|
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/')
|