first try

This commit is contained in:
Michal Gulczynski 2024-12-16 00:40:14 +01:00
commit 5133d0f102
5 changed files with 111 additions and 0 deletions

5
Dockerfile Normal file
View File

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

75
main.py Normal file
View File

@ -0,0 +1,75 @@
import boto3
import argparse
import time
from user_data import user_data
# Stałe
PREFIX = 's464953'
REGION = 'us-east-1'
AMI_ID = 'ami-0b5eea76982371e91'
INSTANCE_TYPE = 't2.micro'
MIN_INSTANCES = 2
MAX_INSTANCES = 5
LOAD_BALANCER_PORT = 80
TARGET_PORT = 8080
def main(aws_access_key_id, aws_secret_access_key, aws_session_token, default_vpc):
ec2 = boto3.resource(
'ec2',
region_name=REGION,
aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key,
aws_session_token=aws_session_token,
)
# Tworzenie key pair
key_pair = ec2.create_key_pair(
KeyName=PREFIX + '-key',
KeyType='ed25519',
KeyFormat='pem',
)
# Tworzenie security group
security_group = ec2.create_security_group(
Description=PREFIX + '-group',
GroupName=PREFIX + '-group',
VpcId=default_vpc,
)
security_group.authorize_ingress(
GroupId=security_group.group_id,
IpPermissions=[
{
'IpProtocol': 'tcp',
'FromPort': LOAD_BALANCER_PORT,
'ToPort': LOAD_BALANCER_PORT,
'IpRanges': [{'CidrIp': '0.0.0.0/0'}],
},
]
)
# Tworzenie Launch Template
ec2_client = boto3.client('ec2', region_name=REGION)
launch_template = ec2_client.create_launch_template(
LaunchTemplateName=PREFIX + '-LaunchTemplate',
LaunchTemplateData={
'ImageId': AMI_ID,
'InstanceType': INSTANCE_TYPE,
'KeyName': key_pair.name,
'UserData': user_data,
'SecurityGroupIds': [security_group.group_id],
}
)
# Reszta kodu (bez zmian)
# ...
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="Create AWS infrastructure")
parser.add_argument('--aws_access_key_id', required=True, help='AWS Access Key ID')
parser.add_argument('--aws_secret_access_key', required=True, help='AWS Secret Access Key')
parser.add_argument('--aws_session_token', required=True, help='AWS Session Token')
parser.add_argument('--default_vpc', required=True, help='Default VPC ID')
args = parser.parse_args()
main(args.aws_access_key_id, args.aws_secret_access_key, args.aws_session_token, args.default_vpc)

19
scipt.sh Normal file
View File

@ -0,0 +1,19 @@
#!/bin/bash
echo "Pobieranie danych AWS CLI..."
AWS_ACCESS_KEY_ID=$(aws configure get aws_access_key_id)
AWS_SECRET_ACCESS_KEY=$(aws configure get aws_secret_access_key)
AWS_SESSION_TOKEN=$(aws sts get-session-token --duration-seconds 3600 --query 'Credentials.SessionToken' --output text)
DEFAULT_VPC=$(aws ec2 describe-vpcs --filters "Name=isDefault,Values=true" --query 'Vpcs[0].VpcId' --output text)
if [[ -z "$AWS_ACCESS_KEY_ID" || -z "$AWS_SECRET_ACCESS_KEY" || -z "$AWS_SESSION_TOKEN" || -z "$DEFAULT_VPC" ]]; then
echo "Brak wymaganych danych! Sprawdź konfigurację AWS CLI."
exit 1
fi
echo "Uruchamianie main.py..."
python3 main.py \
--aws_access_key_id "$AWS_ACCESS_KEY_ID" \
--aws_secret_access_key "$AWS_SECRET_ACCESS_KEY" \
--aws_session_token "$AWS_SESSION_TOKEN" \
--default_vpc "$DEFAULT_VPC"

12
user_data.py Normal file
View File

@ -0,0 +1,12 @@
user_data = f'''
#!/bin/bash
sudo yum update -y
sudo yum install git -y
git clone https://git.wmi.amu.edu.pl/s464953/aws.git
cd aws
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
'''

BIN
webservice Normal file

Binary file not shown.