commit 5133d0f102b21d5b6dfa58f0296b63a7cdef97f5 Author: Michal Gulczynski Date: Mon Dec 16 00:40:14 2024 +0100 first try diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..6147f6f --- /dev/null +++ b/Dockerfile @@ -0,0 +1,5 @@ +FROM ubuntu +COPY ./webservice / +RUN chmod +x ./webservice +EXPOSE 80:8080/tcp +CMD ./webservice \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..cbbe861 --- /dev/null +++ b/main.py @@ -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) diff --git a/scipt.sh b/scipt.sh new file mode 100644 index 0000000..401cf24 --- /dev/null +++ b/scipt.sh @@ -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" diff --git a/user_data.py b/user_data.py new file mode 100644 index 0000000..b587db4 --- /dev/null +++ b/user_data.py @@ -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 +''' \ No newline at end of file diff --git a/webservice b/webservice new file mode 100644 index 0000000..afd2204 Binary files /dev/null and b/webservice differ