| 
									
										
										
										
											2024-01-07 12:03:33 +00:00
										 |  |  | from typing import Any, Tuple | 
					
						
							| 
									
										
										
										
											2021-10-05 17:11:07 +00:00
										 |  |  | from uuid import uuid4 | 
					
						
							| 
									
										
										
										
											2021-08-04 13:40:10 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-30 07:55:51 -08:00
										 |  |  | import boto3 | 
					
						
							| 
									
										
										
										
											2021-08-04 13:40:10 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | DEFAULT_REGION = "eu-central-1" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-07 12:03:33 +00:00
										 |  |  | def _get_clients() -> Tuple[Any, Any, Any, Any, Any]: | 
					
						
							| 
									
										
										
										
											2021-08-04 13:40:10 +01:00
										 |  |  |     return ( | 
					
						
							|  |  |  |         boto3.client("ec2", region_name=DEFAULT_REGION), | 
					
						
							|  |  |  |         boto3.client("iam", region_name=DEFAULT_REGION), | 
					
						
							|  |  |  |         boto3.client("ecs", region_name=DEFAULT_REGION), | 
					
						
							|  |  |  |         boto3.client("logs", region_name=DEFAULT_REGION), | 
					
						
							|  |  |  |         boto3.client("batch", region_name=DEFAULT_REGION), | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-07 12:03:33 +00:00
										 |  |  | def _setup(ec2_client: Any, iam_client: Any) -> Tuple[str, str, str, str]: | 
					
						
							| 
									
										
										
										
											2021-08-04 13:40:10 +01:00
										 |  |  |     """
 | 
					
						
							|  |  |  |     Do prerequisite setup | 
					
						
							|  |  |  |     :return: VPC ID, Subnet ID, Security group ID, IAM Role ARN | 
					
						
							|  |  |  |     :rtype: tuple | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     resp = ec2_client.create_vpc(CidrBlock="172.30.0.0/24") | 
					
						
							|  |  |  |     vpc_id = resp["Vpc"]["VpcId"] | 
					
						
							|  |  |  |     resp = ec2_client.create_subnet( | 
					
						
							|  |  |  |         AvailabilityZone="eu-central-1a", CidrBlock="172.30.0.0/25", VpcId=vpc_id | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     subnet_id = resp["Subnet"]["SubnetId"] | 
					
						
							|  |  |  |     resp = ec2_client.create_security_group( | 
					
						
							| 
									
										
										
										
											2021-10-05 17:11:07 +00:00
										 |  |  |         Description="test_sg_desc", GroupName=str(uuid4())[0:6], VpcId=vpc_id | 
					
						
							| 
									
										
										
										
											2021-08-04 13:40:10 +01:00
										 |  |  |     ) | 
					
						
							|  |  |  |     sg_id = resp["GroupId"] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-05 17:11:07 +00:00
										 |  |  |     role_name = f"{str(uuid4())[0:6]}" | 
					
						
							| 
									
										
										
										
											2021-08-04 13:40:10 +01:00
										 |  |  |     resp = iam_client.create_role( | 
					
						
							| 
									
										
										
										
											2021-10-05 17:11:07 +00:00
										 |  |  |         RoleName=role_name, AssumeRolePolicyDocument="some_policy" | 
					
						
							| 
									
										
										
										
											2021-08-04 13:40:10 +01:00
										 |  |  |     ) | 
					
						
							|  |  |  |     iam_arn = resp["Role"]["Arn"] | 
					
						
							| 
									
										
										
										
											2021-10-05 17:11:07 +00:00
										 |  |  |     iam_client.create_instance_profile(InstanceProfileName=role_name) | 
					
						
							| 
									
										
										
										
											2021-08-04 13:40:10 +01:00
										 |  |  |     iam_client.add_role_to_instance_profile( | 
					
						
							| 
									
										
										
										
											2021-10-05 17:11:07 +00:00
										 |  |  |         InstanceProfileName=role_name, RoleName=role_name | 
					
						
							| 
									
										
										
										
											2021-08-04 13:40:10 +01:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return vpc_id, subnet_id, sg_id, iam_arn |