Skip to content

Commit bcb88ca

Browse files
authored
Fix/add region for boto3 (#70)
* fix: add region for all boto3 objects * fix: add region support for all boto3 objects
1 parent ce83bdf commit bcb88ca

File tree

8 files changed

+35
-32
lines changed

8 files changed

+35
-32
lines changed

src/emd/cfn/codepipeline/template.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ AWSTemplateFormatVersion: '2010-09-09'
22
Description: |
33
Easy Model Deployer bootstrap environment.
44
If you delete this stack, you will not be able to deploy any new models.
5-
5+
Please go to https://github.com/aws-samples/easy-model-deployer for more information.
66
Parameters:
77
ArtifactBucketName:
88
Type: String

src/emd/cfn/ecs/template.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Parameters:
3939
Description: The emd model ID to be used for the ECS Endpoint
4040
ModelTag:
4141
Type: String
42-
Default: "latest"
42+
Default: "dev"
4343
Description: The model tag to be used for the ECS Endpoint
4444
FrameWorkType:
4545
Type: String

src/emd/sdk/deploy.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def prepare_deploy(
6767
f"A model with the ID: {model_id} and tag: {model_tag} already exists. Kindly use a different tag to proceed."
6868
)
6969

70-
client = boto3.client("codepipeline")
70+
client = boto3.client("codepipeline", region_name=region)
7171
try:
7272
active_executuion_infos = get_pipeline_active_executions(
7373
pipeline_name=CODEPIPELINE_NAME,
@@ -114,7 +114,7 @@ def deploy(
114114
waiting_until_deploy_complete=True,
115115
) -> dict:
116116
# Check if AWS environment is properly configured
117-
logger.info("check if model is exists...")
117+
logger.info("checking if model is exists...")
118118
assert (
119119
model_stack_name is None
120120
), f"It is currently not supported to custom model stack name."
@@ -228,7 +228,8 @@ def deploy(
228228
while True:
229229
try:
230230
status_info = get_pipeline_execution_status(
231-
pipeline_execution_id=response["pipelineExecutionId"]
231+
pipeline_execution_id=response["pipelineExecutionId"],
232+
region=region
232233
)
233234
except client.exceptions.PipelineExecutionNotFoundException as e:
234235
logger.info("Waiting for pipeline execution to start...")
@@ -244,7 +245,7 @@ def deploy(
244245
log_info += f", status_summary: {status_summary}"
245246
if stage_name:
246247
log_info += f", in stage: {stage_name}"
247-
log_info += f", Execution ID: {execution_id}"
248+
# log_info += f", Execution ID: {execution_id}"
248249
log_info += f", Duration time: {int(time.time() - start_deploy_time)}s"
249250
logger.info(log_info)
250251
if status_code == 0:

src/emd/sdk/destroy.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
)
1818
from emd.models.utils.constants import ServiceType
1919
from emd.models import Model
20-
20+
from emd.utils.aws_service_utils import get_current_region
2121
logger = get_logger(__name__)
2222

2323

@@ -36,7 +36,7 @@ def stop_pipeline_execution(
3636
cur_uuid = Model.get_model_stack_name_prefix(model_id,model_tag)
3737
if cur_uuid in active_executuion_infos_d:
3838
pipeline_execution_id = active_executuion_infos_d[cur_uuid]['pipeline_execution_id']
39-
client = boto3.client('codepipeline')
39+
client = boto3.client('codepipeline', region_name=get_current_region())
4040
try:
4141
client.stop_pipeline_execution(
4242
pipelineName=pipeline_name,
@@ -59,7 +59,7 @@ def stop_pipeline_execution(
5959

6060

6161
def destroy_ecs(model_id,model_tag,stack_name):
62-
cf_client = boto3.client('cloudformation')
62+
cf_client = boto3.client('cloudformation', region_name=get_current_region())
6363
cf_client.delete_stack(StackName=stack_name)
6464

6565
def destroy(model_id:str,model_tag=MODEL_DEFAULT_TAG,waiting_until_complete=True):
@@ -74,7 +74,7 @@ def destroy(model_id:str,model_tag=MODEL_DEFAULT_TAG,waiting_until_complete=True
7474
if parameters['ServiceType'] == ServiceType.ECS:
7575
return destroy_ecs(model_id, model_tag,stack_name)
7676

77-
cf_client = boto3.client('cloudformation')
77+
cf_client = boto3.client('cloudformation', region_name=get_current_region())
7878
cf_client.delete_stack(StackName=stack_name)
7979

8080
logger.info(f"Delete stack initiated: {stack_name}")

src/emd/sdk/status.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@
1010
check_env_stack_exist_and_complete
1111
)
1212
from emd.utils.aws_service_utils import get_account_id
13-
13+
from emd.utils.aws_service_utils import get_current_region
1414
def get_pipeline_execution_status(
1515
pipeline_execution_id:str,
1616
pipeline_name:str = CODEPIPELINE_NAME,
1717
region=None
1818
):
19+
if region is None:
20+
region = get_current_region()
1921
client = boto3.client('codepipeline', region_name=region)
2022
response = client.get_pipeline_execution(
2123
pipelineName=pipeline_name,

src/emd/utils/aws_service_management.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def check_aws_environment():
2929
"""
3030
try:
3131
# Try to create a boto3 client and make a simple API call
32-
sts = boto3.client('sts')
32+
sts = boto3.client('sts', region_name=get_current_region())
3333
response = sts.get_caller_identity()
3434
typer.echo("AWS environment is properly configured.")
3535
account_id = response['Account']
@@ -47,7 +47,7 @@ def log_pipeline_execution_details(execution_id: str = None, logs: bool = False)
4747
Get the details of a pipeline execution and optionally log the events.
4848
"""
4949
try:
50-
sts = boto3.client("sts")
50+
sts = boto3.client("sts", region_name=get_current_region())
5151
region = get_current_region()
5252
cfn = boto3.client("cloudformation", region_name=region)
5353
codepipeline = boto3.client("codepipeline", region_name=region)

src/emd/utils/aws_service_utils.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def check_aws_environment():
5656
"""
5757
try:
5858
# Try to create a boto3 client and make a simple API call
59-
sts = boto3.client("sts")
59+
sts = boto3.client("sts", region_name=get_current_region())
6060
response = sts.get_caller_identity()
6161
logger.info("AWS environment is properly configured.")
6262
account_id = response["Account"]
@@ -69,7 +69,7 @@ def check_aws_environment():
6969

7070

7171
def get_account_id():
72-
sts_client = boto3.client("sts")
72+
sts_client = boto3.client("sts", region_name=get_current_region())
7373
account_id = sts_client.get_caller_identity()["Account"]
7474
return account_id
7575

@@ -123,7 +123,7 @@ def get_role_create_template(
123123

124124

125125
def get_stack_info(stack_name):
126-
cf = boto3.client("cloudformation")
126+
cf = boto3.client("cloudformation", region_name=get_current_region())
127127
stack_info = cf.describe_stacks(StackName=stack_name)["Stacks"][0]
128128
parameters = {}
129129
for parameter in stack_info["Parameters"]:
@@ -134,7 +134,7 @@ def get_stack_info(stack_name):
134134

135135
def check_stack_exists(stack_name):
136136
try:
137-
cf = boto3.client("cloudformation")
137+
cf = boto3.client("cloudformation", region_name=get_current_region())
138138
cf.describe_stacks(StackName=stack_name)
139139
return True
140140
except ClientError as e:
@@ -150,7 +150,7 @@ def check_stack_status(stack_name) -> StackStatus:
150150
is_stack_exist = True
151151
stack_info = {}
152152
try:
153-
cf = boto3.client("cloudformation")
153+
cf = boto3.client("cloudformation", region_name=get_current_region())
154154
stack_info = cf.describe_stacks(StackName=stack_name)["Stacks"][0]
155155
except ClientError as e:
156156
if e.response["Error"][
@@ -163,7 +163,7 @@ def check_stack_status(stack_name) -> StackStatus:
163163

164164

165165
def get_pipeline_stages(pipeline_name: str) -> list[str]:
166-
client = boto3.client("codepipeline")
166+
client = boto3.client("codepipeline", region_name=get_current_region())
167167
response = client.get_pipeline_state(name=pipeline_name)
168168
stages = [i["stageName"] for i in response["stageStates"]]
169169
return stages
@@ -173,7 +173,7 @@ def get_pipeline_execution_info(
173173
pipeline_name: str, pipeline_execution_id: str, client=None
174174
):
175175

176-
client = client or boto3.client("codepipeline")
176+
client = client or boto3.client("codepipeline", region_name=get_current_region())
177177
execution_info = client.get_pipeline_execution(
178178
pipelineName=pipeline_name, pipelineExecutionId=pipeline_execution_id
179179
)["pipelineExecution"]
@@ -187,7 +187,7 @@ def get_pipeline_active_executions(
187187
filter_stoped=True,
188188
filter_failed=True,
189189
) -> list[dict]:
190-
client = client or boto3.client("codepipeline")
190+
client = client or boto3.client("codepipeline", region_name=get_current_region())
191191
try:
192192
stage_states = client.get_pipeline_state(name=pipeline_name)[
193193
"stageStates"
@@ -264,7 +264,7 @@ def get_pipeline_active_executions(
264264

265265

266266
def get_model_stacks():
267-
cf = boto3.client("cloudformation")
267+
cf = boto3.client("cloudformation", region_name=get_current_region())
268268
stacks = cf.list_stacks(
269269
StackStatusFilter=[
270270
"CREATE_COMPLETE",
@@ -322,21 +322,21 @@ def get_model_stacks():
322322

323323

324324
def get_model_stack_info(model_stack_name: str):
325-
cf = boto3.client("cloudformation")
325+
cf = boto3.client("cloudformation", region_name=get_current_region())
326326
stack_info = cf.describe_stacks(StackName=model_stack_name)["Stacks"][0]
327327
return stack_info
328328

329329

330330
def s3_bucket_version(bucket, s3_key):
331-
s3_client = boto3.client("s3")
331+
s3_client = boto3.client("s3", region_name=get_current_region())
332332
version_id: str = s3_client.head_object(Bucket=bucket, Key=s3_key)[
333333
"VersionId"
334334
]
335335
return version_id
336336

337337

338338
def check_stack_exist_and_complete(stack_name: str):
339-
client = boto3.client("cloudformation")
339+
client = boto3.client("cloudformation", region_name=get_current_region())
340340
try:
341341
response = client.describe_stacks(StackName=stack_name)
342342
stack_status = response["Stacks"][0]["StackStatus"]
@@ -357,7 +357,7 @@ def monitor_stack(stack_name):
357357
response = get_stack_info(stack_name=stack_name)
358358
stack_id = response["StackId"]
359359
seen_events = set()
360-
cloudformation = boto3.client("cloudformation")
360+
cloudformation = boto3.client("cloudformation", region_name=get_current_region())
361361
# Determine if this is a create or update operation
362362
while True:
363363
events = cloudformation.describe_stack_events(StackName=stack_id)[
@@ -398,7 +398,7 @@ def monitor_stack(stack_name):
398398

399399

400400
def monitor_pipeline(pipeline_name, pipeline_execution_id):
401-
client = boto3.client("codepipeline")
401+
client = boto3.client("codepipeline", region_name=get_current_region())
402402
while True:
403403
response = client.get_pipeline_state(name=pipeline_name)
404404
for stage in response["stageStates"]:
@@ -435,7 +435,7 @@ def get_sagemaker_instance_quota(instance_type: str) -> float:
435435
ValueError: If the instance type doesn't have a corresponding quota code
436436
ClientError: If there's an error calling AWS Service Quotas
437437
"""
438-
service_quota_client = boto3.client("service-quotas")
438+
service_quota_client = boto3.client("service-quotas", region_name=get_current_region())
439439

440440
quota_code = ServiceQuotaCode.get_service_quota_code(instance_type)
441441
response = service_quota_client.get_service_quota(
@@ -459,7 +459,7 @@ def get_sagemaker_instance_count_by_type(instance_type: str):
459459
list: List of endpoint names using this instance type
460460
"""
461461
# Initialize SageMaker client
462-
sagemaker_client = boto3.client("sagemaker")
462+
sagemaker_client = boto3.client("sagemaker", region_name=get_current_region())
463463

464464
sagemaker_instance_type = InstanceType.convert_instance_type_to_sagemaker(
465465
instance_type
@@ -566,7 +566,7 @@ def get_current_region():
566566

567567

568568
def get_aws_account_id():
569-
sts = boto3.client("sts")
569+
sts = boto3.client("sts", region_name=get_current_region())
570570
response = sts.get_caller_identity()
571571
account_id = response["Account"]
572572
return account_id

src/pipeline/utils/aws_service_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def check_aws_environment():
3333

3434

3535
def get_account_id():
36-
sts_client = boto3.client("sts")
36+
sts_client = boto3.client("sts", region_name=get_current_region())
3737
account_id = sts_client.get_caller_identity()["Account"]
3838
return account_id
3939

@@ -90,7 +90,7 @@ def get_role_create_template(
9090

9191

9292
def get_stack_info(stack_name):
93-
cf = boto3.client('cloudformation')
93+
cf = boto3.client('cloudformation', region_name=get_current_region())
9494
stack_info = cf.describe_stacks(StackName=stack_name)['Stacks'][0]
9595
return stack_info
9696

0 commit comments

Comments
 (0)