27
27
import botocore
28
28
import boto3
29
29
30
- from .utils import default_bucket , get_execution_role
30
+ from .utils import default_bucket , domain_for_region , get_execution_role
31
31
32
32
abbrev_image_pat = re .compile (
33
- r"(?P<account>\d+).dkr.ecr.(?P<region>[^.]+).amazonaws.com/(?P<image>[^:/]+)(?P<tag>:[^:]+)?"
33
+ r"(?P<account>\d+).dkr.ecr.(?P<region>[^.]+).( amazonaws.com|amazonaws.com.cn) /(?P<image>[^:/]+)(?P<tag>:[^:]+)?"
34
34
)
35
35
36
36
@@ -46,7 +46,8 @@ def abbreviate_image(image):
46
46
return image
47
47
48
48
49
- abbrev_role_pat = re .compile (r"arn:aws:iam::(?P<account>\d+):role/(?P<name>[^/]+)" )
49
+ abbrev_role_pat = re .compile (
50
+ r"arn:aws([^:]*):iam::(?P<account>\d+):role/(?P<name>[^/]+)" )
50
51
51
52
52
53
def abbreviate_role (role ):
@@ -124,13 +125,17 @@ def execute_notebook(
124
125
if not role :
125
126
role = get_execution_role (session )
126
127
elif "/" not in role :
127
- account = session .client ("sts" ).get_caller_identity ()["Account" ]
128
- role = "arn:aws:iam::{}:role/{}" .format (account , role )
128
+ identity = session .client ("sts" ).get_caller_identity ()
129
+ account = identity ["Account" ]
130
+ partition = identity ["Arn" ].split (':' )[1 ]
131
+ role = "arn:{}:iam::{}:role/{}" .format (partition , account , role )
129
132
130
133
if "/" not in image :
131
134
account = session .client ("sts" ).get_caller_identity ()["Account" ]
132
135
region = session .region_name
133
- image = "{}.dkr.ecr.{}.amazonaws.com/{}:latest" .format (account , region , image )
136
+ domain = domain_for_region (region )
137
+ image = "{}.dkr.ecr.{}.{}/{}:latest" .format (
138
+ account , region , domain , image )
134
139
135
140
if notebook == None :
136
141
notebook = input_path
@@ -140,7 +145,8 @@ def execute_notebook(
140
145
timestamp = time .strftime ("%Y-%m-%d-%H-%M-%S" , time .gmtime ())
141
146
142
147
job_name = (
143
- ("papermill-" + re .sub (r"[^-a-zA-Z0-9]" , "-" , nb_name ))[: 62 - len (timestamp )]
148
+ ("papermill-" + re .sub (r"[^-a-zA-Z0-9]" ,
149
+ "-" , nb_name ))[: 62 - len (timestamp )]
144
150
+ "-"
145
151
+ timestamp
146
152
)
@@ -628,8 +634,10 @@ def create_lambda(role=None, session=None):
628
634
# time.sleep(30) # wait for eventual consistency, we hope
629
635
630
636
if "/" not in role :
631
- account = session .client ("sts" ).get_caller_identity ()["Account" ]
632
- role = "arn:aws:iam::{}:role/{}" .format (account , role )
637
+ identity = session .client ("sts" ).get_caller_identity ()
638
+ account = identity ["Account" ]
639
+ partition = identity ["Arn" ].split (':' )[1 ]
640
+ role = "arn:{}:iam::{}:role/{}" .format (partition , account , role )
633
641
634
642
code_bytes = zip_bytes (code_file )
635
643
@@ -780,7 +788,9 @@ def proc(extras):
780
788
if "/" not in image :
781
789
account = session .client ("sts" ).get_caller_identity ()["Account" ]
782
790
region = session .region_name
783
- image = "{}.dkr.ecr.{}.amazonaws.com/{}:latest" .format (account , region , image )
791
+ domain = domain_for_region (region )
792
+ image = "{}.dkr.ecr.{}.{}/{}:latest" .format (
793
+ account , region , domain , image )
784
794
785
795
if not role :
786
796
try :
@@ -789,8 +799,10 @@ def proc(extras):
789
799
role = "BasicExecuteNotebookRole-{}" .format (session .region_name )
790
800
791
801
if "/" not in role :
792
- account = session .client ("sts" ).get_caller_identity ()["Account" ]
793
- role = "arn:aws:iam::{}:role/{}" .format (account , role )
802
+ identity = session .client ("sts" ).get_caller_identity ()
803
+ account = identity ["Account" ]
804
+ partition = identity ["Arn" ].split (':' )[1 ]
805
+ role = "arn:{}:iam::{}:role/{}" .format (partition , account , role )
794
806
795
807
if input_path is None :
796
808
input_path = upload_notebook (notebook )
@@ -849,7 +861,7 @@ def schedule(
849
861
850
862
Creates a CloudWatch Event rule to invoke the installed Lambda either on the provided schedule or in response
851
863
to the provided event. \
852
-
864
+
853
865
:meth:`schedule` can upload a local notebook file to run or use one previously uploaded to S3.
854
866
To find jobs run by the schedule, see :meth:`list_runs` using the `rule` argument to filter to
855
867
a specific rule. To download the results, see :meth:`download_notebook` (or :meth:`download_all`
@@ -905,7 +917,9 @@ def proc(extras):
905
917
if "/" not in image :
906
918
account = session .client ("sts" ).get_caller_identity ()["Account" ]
907
919
region = session .region_name
908
- image = "{}.dkr.ecr.{}.amazonaws.com/{}:latest" .format (account , region , image )
920
+ domain = domain_for_region (region )
921
+ image = "{}.dkr.ecr.{}.{}/{}:latest" .format (
922
+ account , region , domain , image )
909
923
910
924
if not role :
911
925
try :
@@ -914,8 +928,10 @@ def proc(extras):
914
928
role = "BasicExecuteNotebookRole-{}" .format (session .region_name )
915
929
916
930
if "/" not in role :
917
- account = session .client ("sts" ).get_caller_identity ()["Account" ]
918
- role = "arn:aws:iam::{}:role/{}" .format (account , role )
931
+ identity = session .client ("sts" ).get_caller_identity ()
932
+ account = identity ["Account" ]
933
+ partition = identity ["Arn" ].split (':' )[1 ]
934
+ role = "arn:{}:iam::{}:role/{}" .format (partition , account , role )
919
935
920
936
if input_path is None :
921
937
input_path = upload_notebook (notebook )
@@ -945,11 +961,12 @@ def proc(extras):
945
961
Description = 'Rule to run the Jupyter notebook "{}"' .format (notebook ),
946
962
** kwargs ,
947
963
)
948
-
949
- account = session .client ("sts" ).get_caller_identity ()["Account" ]
964
+ identity = session .client ("sts" ).get_caller_identity ()
965
+ account = identity ["Account" ]
966
+ partition = identity ["Arn" ].split (':' )[1 ]
950
967
region = session .region_name
951
- target_arn = "arn:aws :lambda:{}:{}:function:{}" .format (
952
- region , account , lambda_function_name
968
+ target_arn = "arn:{} :lambda:{}:{}:function:{}" .format (
969
+ partition , region , account , lambda_function_name
953
970
)
954
971
955
972
result = events .put_targets (
@@ -1082,7 +1099,7 @@ def base_image(s):
1082
1099
return s
1083
1100
1084
1101
1085
- role_pat = re .compile (r"arn:aws:iam::([0-9]+):role/(.*)$" )
1102
+ role_pat = re .compile (r"arn:aws([^:]*) :iam::([0-9]+):role/(.*)$" )
1086
1103
1087
1104
1088
1105
def base_role (s ):
0 commit comments