@@ -37,7 +37,7 @@ class Environment(object):
37
37
def __init__ (self , region = None , prefix = None ):
38
38
# target is the runtime environment to use, e.g.,
39
39
# 'local' for local mode
40
- self .region = region or DEFAULT_REGION
40
+ self .region = region or get_local_region ()
41
41
# prefix can be 'prod', 'stg', 'uat-1', etc.
42
42
self .prefix = prefix
43
43
@@ -52,7 +52,7 @@ def from_string(s):
52
52
if len (parts ) == 1 :
53
53
if s in PREDEFINED_ENVIRONMENTS :
54
54
return PREDEFINED_ENVIRONMENTS [s ]
55
- parts = [DEFAULT_REGION , s ]
55
+ parts = [get_local_region () , s ]
56
56
if len (parts ) > 2 :
57
57
raise Exception ('Invalid environment string "%s"' % s )
58
58
region = parts [0 ]
@@ -128,6 +128,11 @@ def get_boto3_session():
128
128
return boto3
129
129
130
130
131
+ def get_local_region ():
132
+ session = boto3 .session .Session ()
133
+ return session .region_name or DEFAULT_REGION
134
+
135
+
131
136
def get_local_service_url (service_name ):
132
137
if service_name == 's3api' :
133
138
service_name = 's3'
@@ -146,7 +151,7 @@ def connect_to_service(service_name, client=True, env=None, region_name=None, en
146
151
if env .region == REGION_LOCAL :
147
152
endpoint_url = get_local_service_url (service_name )
148
153
verify = False
149
- region = env .region if env .region != REGION_LOCAL else None
154
+ region = env .region if env .region != REGION_LOCAL else get_local_region ()
150
155
return method (service_name , region_name = region , endpoint_url = endpoint_url , verify = verify )
151
156
152
157
@@ -234,13 +239,13 @@ def get_iam_role(resource, env=None):
234
239
235
240
def dynamodb_table_arn (table_name , account_id = None ):
236
241
account_id = get_account_id (account_id )
237
- return 'arn:aws:dynamodb:%s:%s:table/%s' % (DEFAULT_REGION , account_id , table_name )
242
+ return 'arn:aws:dynamodb:%s:%s:table/%s' % (get_local_region () , account_id , table_name )
238
243
239
244
240
245
def dynamodb_stream_arn (table_name , account_id = None ):
241
246
account_id = get_account_id (account_id )
242
247
return ('arn:aws:dynamodb:%s:%s:table/%s/stream/%s' %
243
- (DEFAULT_REGION , account_id , table_name , timestamp ()))
248
+ (get_local_region () , account_id , table_name , timestamp ()))
244
249
245
250
246
251
def lambda_function_arn (function_name , account_id = None ):
@@ -250,22 +255,22 @@ def lambda_function_arn(function_name, account_id=None):
250
255
if ':' in function_name :
251
256
raise Exception ('Lambda function name should not contain a colon ":"' )
252
257
account_id = get_account_id (account_id )
253
- return pattern .replace ('.*' , '%s' ) % (DEFAULT_REGION , account_id , function_name )
258
+ return pattern .replace ('.*' , '%s' ) % (get_local_region () , account_id , function_name )
254
259
255
260
256
261
def cognito_user_pool_arn (user_pool_id , account_id = None ):
257
262
account_id = get_account_id (account_id )
258
- return 'arn:aws:cognito-idp:%s:%s:userpool/%s' % (DEFAULT_REGION , account_id , user_pool_id )
263
+ return 'arn:aws:cognito-idp:%s:%s:userpool/%s' % (get_local_region () , account_id , user_pool_id )
259
264
260
265
261
266
def kinesis_stream_arn (stream_name , account_id = None ):
262
267
account_id = get_account_id (account_id )
263
- return 'arn:aws:kinesis:%s:%s:stream/%s' % (DEFAULT_REGION , account_id , stream_name )
268
+ return 'arn:aws:kinesis:%s:%s:stream/%s' % (get_local_region () , account_id , stream_name )
264
269
265
270
266
271
def firehose_stream_arn (stream_name , account_id = None ):
267
272
account_id = get_account_id (account_id )
268
- return ('arn:aws:firehose:%s:%s:deliverystream/%s' % (DEFAULT_REGION , account_id , stream_name ))
273
+ return ('arn:aws:firehose:%s:%s:deliverystream/%s' % (get_local_region () , account_id , stream_name ))
269
274
270
275
271
276
def s3_bucket_arn (bucket_name , account_id = None ):
@@ -274,12 +279,12 @@ def s3_bucket_arn(bucket_name, account_id=None):
274
279
275
280
def sqs_queue_arn (queue_name , account_id = None ):
276
281
account_id = get_account_id (account_id )
277
- return ('arn:aws:sqs:%s:%s:%s' % (DEFAULT_REGION , account_id , queue_name ))
282
+ return ('arn:aws:sqs:%s:%s:%s' % (get_local_region () , account_id , queue_name ))
278
283
279
284
280
285
def sns_topic_arn (topic_name , account_id = None ):
281
286
account_id = get_account_id (account_id )
282
- return ('arn:aws:sns:%s:%s:%s' % (DEFAULT_REGION , account_id , topic_name ))
287
+ return ('arn:aws:sns:%s:%s:%s' % (get_local_region () , account_id , topic_name ))
283
288
284
289
285
290
def get_sqs_queue_url (queue_name ):
0 commit comments