22import json
33from typing import List
44
5+ import botocore .exceptions
56from boto3 .session import Session
67
78from dstack ._internal .core .backends .aws import AWSBackend , auth , compute , resources
3536 Configurator ,
3637 raise_invalid_credentials_error ,
3738)
39+ from dstack ._internal .utils .logging import get_logger
40+
41+ logger = get_logger (__name__ )
3842
3943REGIONS = [
4044 ("US East, N. Virginia" , "us-east-1" ),
@@ -137,7 +141,8 @@ def _get_regions_element(self, selected: List[str]) -> ConfigMultiElement:
137141
138142 def _check_config (self , session : Session , config : AWSConfigInfoWithCredsPartial ):
139143 self ._check_tags_config (config )
140- self ._check_vpc_config (session = session , config = config )
144+ self ._check_iam_instance_profile_config (session , config )
145+ self ._check_vpc_config (session , config )
141146
142147 def _check_tags_config (self , config : AWSConfigInfoWithCredsPartial ):
143148 if not config .tags :
@@ -151,6 +156,31 @@ def _check_tags_config(self, config: AWSConfigInfoWithCredsPartial):
151156 except BackendError as e :
152157 raise ServerClientError (e .args [0 ])
153158
159+ def _check_iam_instance_profile_config (
160+ self , session : Session , config : AWSConfigInfoWithCredsPartial
161+ ):
162+ if config .iam_instance_profile is None :
163+ return
164+ try :
165+ iam_client = session .client ("iam" )
166+ iam_client .get_instance_profile (InstanceProfileName = config .iam_instance_profile )
167+ except botocore .exceptions .ClientError as e :
168+ if e .response ["Error" ]["Code" ] == "NoSuchEntity" :
169+ raise ServerClientError (
170+ f"IAM instance profile { config .iam_instance_profile } not found"
171+ )
172+ logger .exception (
173+ "Got botocore.exceptions.ClientError when checking iam_instance_profile"
174+ )
175+ raise ServerClientError (
176+ f"Failed to check IAM instance profile { config .iam_instance_profile } "
177+ )
178+ except Exception :
179+ logger .exception ("Got exception when checking iam_instance_profile" )
180+ raise ServerClientError (
181+ f"Failed to check IAM instance profile { config .iam_instance_profile } "
182+ )
183+
154184 def _check_vpc_config (self , session : Session , config : AWSConfigInfoWithCredsPartial ):
155185 allocate_public_ip = config .public_ips if config .public_ips is not None else True
156186 use_default_vpcs = config .default_vpcs if config .default_vpcs is not None else True
0 commit comments