@@ -125,7 +125,7 @@ class Config(BaseSettings):
125125 GITHUB_TOKEN : str
126126 PROJECT_NAME : str
127127
128- AWS_EC2_AMI : str = "ami-0f9c346cdcac09fb5 " # Deep Learning AMI GPU PyTorch 2.0.1 (Ubuntu 20.04) 20230827
128+ AWS_EC2_AMI : str = "" # fetches the latest compatible AMI dynamically if empty
129129 AWS_EC2_DISK_SIZE : int = 100 # GB
130130 #AWS_EC2_INSTANCE_TYPE: str = "p3.2xlarge" # (V100 16GB $3.06/hr x86_64)
131131 AWS_EC2_INSTANCE_TYPE : str = "g4dn.xlarge" # (T4 16GB $0.526/hr x86_64)
@@ -323,6 +323,43 @@ def get_or_create_security_group_id(ports: list[int] = [22, config.PORT]) -> str
323323 logger .error (f"Error describing security groups: { e } " )
324324 return None
325325
326+ def get_latest_ami (
327+ name_filter : str = "Deep Learning AMI GPU PyTorch *" ,
328+ owner : str = "amazon" ,
329+ region : str = config .AWS_REGION
330+ ) -> str :
331+ """
332+ Retrieves the latest AMI ID matching the specified name filter and owner.
333+
334+ Args:
335+ name_filter (str): Filter for the AMI name. Defaults to "Deep Learning AMI GPU PyTorch *".
336+ owner (str): Owner ID for the AMI. Defaults to "amazon".
337+ region (str): AWS region. Defaults to config.AWS_REGION.
338+
339+ Returns:
340+ str: The latest AMI ID matching the criteria.
341+ """
342+ ec2_client = boto3 .client ('ec2' , region_name = region )
343+ try :
344+ response = ec2_client .describe_images (
345+ Filters = [{'Name' : 'name' , 'Values' : [name_filter ]}],
346+ Owners = [owner ]
347+ )
348+ # Sort AMIs by creation date in descending order
349+ images = sorted (
350+ response ['Images' ],
351+ key = lambda img : img ['CreationDate' ],
352+ reverse = True
353+ )
354+ if not images :
355+ raise ValueError (f"No AMIs found matching filter: { name_filter } " )
356+ latest_ami = images [0 ]['ImageId' ]
357+ logger .info (f"Latest AMI found: { latest_ami } " )
358+ return latest_ami
359+ except ClientError as e :
360+ logger .error (f"Error fetching AMI: { e } " )
361+ raise
362+
326363def deploy_ec2_instance (
327364 ami : str = config .AWS_EC2_AMI ,
328365 instance_type : str = config .AWS_EC2_INSTANCE_TYPE ,
@@ -334,7 +371,7 @@ def deploy_ec2_instance(
334371 Deploys an EC2 instance with the specified parameters.
335372
336373 Args:
337- ami (str): The Amazon Machine Image ID to use for the instance. Defaults to config.AWS_EC2_AMI .
374+ ami (str): The Amazon Machine Image ID to use for the instance. Defaults to the latest matching AMI .
338375 instance_type (str): The type of instance to deploy. Defaults to config.AWS_EC2_INSTANCE_TYPE.
339376 project_name (str): The project name, used for tagging the instance. Defaults to config.PROJECT_NAME.
340377 key_name (str): The name of the key pair to use for the instance. Defaults to config.AWS_EC2_KEY_NAME.
@@ -346,6 +383,8 @@ def deploy_ec2_instance(
346383 ec2 = boto3 .resource ('ec2' )
347384 ec2_client = boto3 .client ('ec2' )
348385
386+ ami = ami or get_latest_ami ()
387+
349388 # Check if key pair exists, if not create one
350389 try :
351390 ec2_client .describe_key_pairs (KeyNames = [key_name ])
0 commit comments