aws/main.py
Michal Gulczynski 5133d0f102 first try
2024-12-16 00:40:14 +01:00

76 lines
2.3 KiB
Python

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)