DPZC-5/main.py

52 lines
1.2 KiB
Python
Raw Normal View History

2023-01-30 15:49:19 +01:00
import boto3
2023-01-30 18:31:16 +01:00
from credentials import *
2023-01-30 15:49:19 +01:00
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,
)
2023-01-30 10:46:34 +01:00
2023-01-30 15:49:19 +01:00
key_pair = ec2.create_key_pair(
2023-01-30 18:31:16 +01:00
KeyName=ID + '-KEY-PAIR',
2023-01-30 15:49:19 +01:00
KeyType='ed25519',
KeyFormat='pem',
)
2023-01-30 10:46:34 +01:00
2023-01-30 15:49:19 +01:00
security_group = ec2.create_security_group(
2023-01-30 18:31:16 +01:00
Description=ID + '-GROUP',
GroupName=ID + '-GROUP',
2023-01-30 15:49:19 +01:00
VpcId=VPC
)
2023-01-30 10:46:34 +01:00
2023-01-30 15:49:19 +01:00
inbound_rules = security_group.authorize_ingress(
GroupId=security_group.group_id,
CidrIp='0.0.0.0/0',
IpProtocol='tcp',
FromPort=80,
ToPort=80
)
2023-01-30 10:46:34 +01:00
2023-01-30 15:49:19 +01:00
instances = ec2.create_instances(
ImageId='ami-0b5eea76982371e91',
MinCount=1,
MaxCount=1,
InstanceType='t2.micro',
KeyName=key_pair.name,
UserData=f'''
#!/bin/bash
sudo yum update -y
sudo yum install git -y
2023-01-30 19:33:16 +01:00
git clone https://git.wmi.amu.edu.pl/s444018/DPZC-5.git
2023-01-30 20:42:08 +01:00
cd DPZC-5/
2023-01-30 15:49:19 +01:00
sudo yum install docker -y
sudo service docker start
sudo usermod -a -G docker ec2-user
docker build -t invoice .
docker run -d -p 80:8000 -t invoice
''',
SecurityGroups=[security_group.group_name]
2023-01-30 18:31:16 +01:00
)