This commit is contained in:
mikolajpaterka 2023-01-26 19:28:52 +01:00
parent 4ae7dfef68
commit ffda1d8e38
6 changed files with 156 additions and 1 deletions

5
.gitignore vendored
View File

@ -1,4 +1,7 @@
.DS_Store
ssh.txt
token.txt
/3_4/token.txt
/3_2/token.txt
tests.ipynb
/4_aws/test.ipynb

5
4_aws/Dockerfile Normal file
View File

@ -0,0 +1,5 @@
FROM ubuntu
COPY ./webservice /
RUN chmod +x ./webservice
EXPOSE 80:8080/tcp
CMD ./webservice

5
4_aws/credencials_aws.py Normal file
View File

@ -0,0 +1,5 @@
aws_access_key_id='ASIARJHPFIYPOSK2KKHF'
aws_secret_access_key='MGAN4JsXBffXHKGxabf4migUnApCV2ZB4wyIKFPP'
aws_session_token='FwoGZXIvYXdzEMv//////////wEaDPqjsdci8pf3n4trQiLBAV8zfYu8D3jLLwiS8k/tsFJ4WFI56VvRJem5g3h6Uond1m78jALXEvAg9HjuNXXVwlRBYDpwmKKTuNgfxwfzmytQbVFHIUyj2mPVsCIaPaq1MJsL0IVLssFZBG1MPkkobTSilFHKMXg9BqFIpd/hNioCBcluzQ7z80WZAoMwxjifXuZrrqrLfx+pm0aS3joAn0CMBMl5wiF0qysEd+KjYt+0DDr2KCtPLSfeVyDS5hci5YkcrHZayMYU/9F0Dua2BRgokvbKngYyLUHgJI2ODq99pQVCxafWFIWSXzPf091nOXO4EZAVxJ2PrVRMntrIa9zShGes3A=='
DEFAULT_VPC = 'vpc-03a807e9eb1952c59'

142
4_aws/main.py Normal file
View File

@ -0,0 +1,142 @@
from credencials_aws import aws_access_key_id, aws_secret_access_key, aws_session_token, DEFAULT_VPC
import boto3, time
INDEKS = "s444455"
key_name = f"{INDEKS}-key"
security_group_name = f"{INDEKS}-security_group"
target_group_name = f"{INDEKS}-target_group"
load_balancer_name = f"{INDEKS}-load-balancer"
user_data = f'''
#!/bin/bash
sudo yum update -y
sudo yum install git -y
git clone https://git.wmi.amu.edu.pl/s444455/DPZC_3.git
cd 4_aws/get
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
'''
if __name__ == '__main__':
ec2 = 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.create_key_pair(
KeyName=key_name,
KeyType='ed25519',
KeyFormat='pem',
)
security_group = ec2.create_security_group(
Description=security_group_name,
GroupName=security_group_name,
VpcId=DEFAULT_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.create_instances(
ImageId='ami-0b5eea76982371e91',
MinCount=2,
MaxCount=2,
InstanceType='t2.micro',
KeyName=key_pair.name,
UserData=user_data,
SecurityGroups=[security_group.group_name],
)
while True:
time.sleep(1)
instance_1 = ec2.Instance(instance_1.id)
instance_2 = ec2.Instance(instance_2.id)
if instance_1.state['Code'] == 16 and instance_2.state['Code'] == 16:
break
#TODO target group
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=target_group_name,
Protocol='TCP',
Port=80,
VpcId=DEFAULT_VPC,
TargetType='instance',
IpAddressType='ipv4',
)
registered_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=load_balancer_name,
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'{allocation["PublicIp"]}:80')

BIN
4_aws/webservice Normal file

Binary file not shown.