diff --git a/sky/clouds/aws.py b/sky/clouds/aws.py index 4527a4acb88..0433a6806fe 100644 --- a/sky/clouds/aws.py +++ b/sky/clouds/aws.py @@ -257,9 +257,17 @@ def get_image_size(cls, image_id: str, region: Optional[str]) -> float: return DEFAULT_AMI_GB assert region is not None, (image_id, region) client = aws.client('ec2', region_name=region) + image_not_found_message = ( + f'Image {image_id!r} not found in AWS region {region}.\n' + f'\nTo find AWS AMI IDs: https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-images.html#examples\n' # pylint: disable=line-too-long + 'Example: ami-0729d913a335efca7') try: image_info = client.describe_images(ImageIds=[image_id]) - image_info = image_info['Images'][0] + image_info = image_info.get('Images', []) + if not image_info: + with ux_utils.print_exception_no_traceback(): + raise ValueError(image_not_found_message) + image_info = image_info[0] image_size = image_info['BlockDeviceMappings'][0]['Ebs'][ 'VolumeSize'] except aws.botocore_exceptions().NoCredentialsError: @@ -269,10 +277,7 @@ def get_image_size(cls, image_id: str, region: Optional[str]) -> float: return DEFAULT_AMI_GB except aws.botocore_exceptions().ClientError: with ux_utils.print_exception_no_traceback(): - raise ValueError( - f'Image {image_id!r} not found in AWS region {region}.\n' - f'\nTo find AWS AMI IDs: https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-images.html#examples\n' # pylint: disable=line-too-long - 'Example: ami-0729d913a335efca7') from None + raise ValueError(image_not_found_message) from None return image_size @classmethod