DPZC_3/5/main.py

59 lines
1.5 KiB
Python
Raw Normal View History

2023-01-28 20:18:57 +01:00
from credencials_aws import aws_access_key_id, aws_secret_access_key, aws_session_token, DEFAULT_VPC
2023-01-28 22:54:41 +01:00
import boto3
2023-01-28 20:18:57 +01:00
2023-01-29 00:22:54 +01:00
INDEKS = "444455"
2023-01-28 20:18:57 +01:00
key_name = f"{INDEKS}-key"
security_group_name = f"{INDEKS}-security-group"
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
2023-01-28 22:54:41 +01:00
cd DPZC_3/5
2023-01-28 20:18:57 +01:00
sudo yum install docker -y
sudo service docker start
sudo usermod -a -G docker ec2-user
2023-01-29 00:17:41 +01:00
sudo docker build -t invoice .
sudo docker run -d -p 80:8000 -t invoice
2023-01-28 20:18:57 +01:00
'''
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 = ec2.create_instances(
ImageId='ami-0b5eea76982371e91',
MinCount=1,
MaxCount=1,
InstanceType='t2.micro',
KeyName=key_pair.name,
UserData=user_data,
SecurityGroups=[security_group.group_name],
2023-01-29 21:03:14 +01:00
)