Usuń 'Zadanie_4_3_b/aws.py'
This commit is contained in:
parent
7847b36cb5
commit
fab1b1dcfa
@ -1,144 +0,0 @@
|
||||
# niezbędna konfiguracja danych dostępowych, pamiętaj aby nigdy jej nie udostępniać publicznie
|
||||
aws_access_key_id=''
|
||||
aws_secret_access_key=''
|
||||
aws_session_token=''
|
||||
VPC=''
|
||||
ID='444376'
|
||||
|
||||
# biblioteka dostępowa do AWS
|
||||
import boto3
|
||||
import time
|
||||
|
||||
# tworzymy zasób, *Resource* oferuje API wyższego poziomu niż *Client*
|
||||
ec2_resource = boto3.resource(
|
||||
'ec2',
|
||||
region_name='us-east-1',
|
||||
aws_access_key_id=aws_access_key_id,
|
||||
aws_secret_access_key=aws_secret_access_key,
|
||||
aws_session_token=aws_session_token
|
||||
)
|
||||
|
||||
key_pair = ec2_resource.create_key_pair(
|
||||
KeyName=ID+'_KEY_PAIR',
|
||||
KeyFormat='pem',
|
||||
KeyType='ed25519'
|
||||
)
|
||||
|
||||
security_group = ec2_resource.create_security_group(
|
||||
Description=ID+'_SECURITY_GROUP',
|
||||
GroupName=ID+'_SECURITY_GROUP',
|
||||
VpcId=VPC
|
||||
)
|
||||
|
||||
inbound_rules = security_group.authorize_ingress(
|
||||
GroupId=security_group.group_id,
|
||||
CidrIp='0.0.0.0/0',
|
||||
IpProtocol='tcp',
|
||||
FromPort=80,
|
||||
ToPort=80
|
||||
)
|
||||
|
||||
instance_1, instance_2 = ec2_resource.create_instances(
|
||||
ImageId='ami-0b5eea76982371e91',
|
||||
MinCount=2,
|
||||
MaxCount=2,
|
||||
InstanceType='t2.micro',
|
||||
KeyName=key_pair.name,
|
||||
UserData=f'''
|
||||
#!/bin/bash
|
||||
sudo yum update -y
|
||||
sudo yum install git -y
|
||||
git clone https://git.wmi.amu.edu.pl/s444376/DPZC_Ola.git
|
||||
sudo yum install docker -y
|
||||
sudo service docker start
|
||||
sudo usermod -a -G docker ec2-user
|
||||
docker build -t webservice .
|
||||
docker run -d -p 80:8080 -t webservice
|
||||
''',
|
||||
SecurityGroups=[security_group.group_name]
|
||||
)
|
||||
|
||||
while True:
|
||||
time.sleep(1)
|
||||
instance_1 = ec2_resource.Instance(instance_1.id)
|
||||
instance_2 = ec2_resource.Instance(instance_2.id)
|
||||
if instance_1.state['Code'] == 16 and instance_2.state['Code'] == 16:
|
||||
break
|
||||
|
||||
elbv2 = boto3.client(
|
||||
'elbv2',
|
||||
region_name='us-east-1',
|
||||
aws_access_key_id=aws_access_key_id,
|
||||
aws_secret_access_key=aws_secret_access_key,
|
||||
aws_session_token=aws_session_token,
|
||||
)
|
||||
|
||||
target_group = elbv2.create_target_group(
|
||||
Name=ID+'_TARGET_GROUP',
|
||||
Protocol='TCP',
|
||||
Port=80,
|
||||
VpcId=VPC,
|
||||
TargetType='instance',
|
||||
IpAddressType='ipv4',
|
||||
)
|
||||
|
||||
register_targets = elbv2.register_targets(
|
||||
TargetGroupArn=target_group['TargetGroups'][0]['TargetGroupArn'],
|
||||
Targets=[
|
||||
{
|
||||
'Id': instance_1.id,
|
||||
'Port': 80,
|
||||
},
|
||||
{
|
||||
'Id': instance_2.id,
|
||||
'Port': 80,
|
||||
},
|
||||
]
|
||||
)
|
||||
|
||||
ec2_client = boto3.client(
|
||||
'ec2',
|
||||
region_name='us-east-1',
|
||||
aws_access_key_id=aws_access_key_id,
|
||||
aws_secret_access_key=aws_secret_access_key,
|
||||
aws_session_token=aws_session_token,
|
||||
)
|
||||
|
||||
allocation = ec2_client.allocate_address(
|
||||
Domain='vpc'
|
||||
)
|
||||
|
||||
load_balancer = elbv2.create_load_balancer(
|
||||
Name=ID+'_LOAD_BALANCER',
|
||||
SubnetMappings=[
|
||||
{
|
||||
'SubnetId': instance_1.subnet_id,
|
||||
'AllocationId': allocation['AllocationId'],
|
||||
},
|
||||
],
|
||||
Scheme='internet-facing',
|
||||
Type='network',
|
||||
IpAddressType='ipv4',
|
||||
)
|
||||
|
||||
listener = elbv2.create_listener(
|
||||
LoadBalancerArn=load_balancer['LoadBalancers'][0]['LoadBalancerArn'],
|
||||
Protocol='TCP',
|
||||
Port=80,
|
||||
DefaultActions=[
|
||||
{
|
||||
'Type': 'forward',
|
||||
'TargetGroupArn': target_group['TargetGroups'][0]['TargetGroupArn'],
|
||||
},
|
||||
],
|
||||
)
|
||||
|
||||
print(f'Done: {allocation["PublicIp"]}:80')
|
||||
|
||||
'''
|
||||
#Stopping and terminating multiple instances given a list of instance IDs uses Boto3 collection filtering:
|
||||
ids = ['instance-id-1', 'instance-id-2', ...]
|
||||
# Boto3
|
||||
ec2.instances.filter(InstanceIds=ids).stop()
|
||||
ec2.instances.filter(InstanceIds=ids).terminate()
|
||||
'''
|
Loading…
Reference in New Issue
Block a user