77Shows how to use the AWS SDK for Python (Boto3) with Amazon Elastic Container Registry (Amazon ECR) to perform
88basic operations.
99
10- This Boto3 code example requires an IAM Role that has permissions to interact with the Amazon ECR service.
10+ To demonstrate granting permissions with a policy, an AWS Identity and Access Management (IAM) role Amazon Resource Name (ARN)
11+ can be passed as a script argument.
1112
1213To create an IAM role, see:
1314
2728
2829script_dir = os .path .dirname (os .path .abspath (__file__ ))
2930
30- # Add relative path to include ecrWrapper .
31+ # Add relative path to include ecr_wrapper .
3132sys .path .append (os .path .dirname (script_dir ))
3233from ecr_wrapper import ECRWrapper
3334
@@ -71,7 +72,7 @@ def __init__(
7172 self .ecr_wrapper = ecr_wrapper
7273 self .docker_client = docker_client
7374 self .tag = "echo-text"
74- self .repo_name = "ecr-basics"
75+ self .repository_name = "ecr-basics"
7576 self .docker_image = None
7677 self .full_tag_name = None
7778 self .repository = None
@@ -110,8 +111,8 @@ def run(self, role_arn: str) -> None:
110111to store, manage, and deploy Docker container images.
111112 """
112113 )
113- print (f"Creating a repository named { self .repo_name } " )
114- self .repository = ecr_wrapper .create_repository (self .repo_name )
114+ print (f"Creating a repository named { self .repository_name } " )
115+ self .repository = self . ecr_wrapper .create_repository (self .repository_name )
115116 print (f"The ARN of the ECR repository is { self .repository ['repositoryArn' ]} " )
116117 repository_uri = self .repository ["repositoryUri" ]
117118 press_enter_to_continue ()
@@ -128,7 +129,7 @@ def run(self, role_arn: str) -> None:
128129 )
129130 print (f"Building a docker image from 'docker_files/Dockerfile'" )
130131 self .full_tag_name = f"{ repository_uri } :{ self .tag } "
131- self .docker_image = docker_client .images .build (
132+ self .docker_image = self . docker_client .images .build (
132133 path = "docker_files" , tag = self .full_tag_name
133134 )[0 ]
134135 self .docker_image .tag (repository = repository_uri , tag = self .tag )
@@ -153,6 +154,7 @@ def run(self, role_arn: str) -> None:
153154repository.
154155 """
155156 )
157+
156158 self .grant_role_download_access (role_arn )
157159 print (f"Download access granted to the IAM role ARN { role_arn } " )
158160 press_enter_to_continue ()
@@ -165,7 +167,8 @@ def run(self, role_arn: str) -> None:
165167Now we will retrieve the ECR policy to ensure it was successfully set.
166168 """
167169 )
168- policy_text = ecr_wrapper .get_repository_policy (self .repo_name )
170+
171+ policy_text = self .ecr_wrapper .get_repository_policy (self .repository_name )
169172 print ("Policy Text:" )
170173 print (f"{ policy_text } " )
171174 press_enter_to_continue ()
@@ -184,7 +187,8 @@ def run(self, role_arn: str) -> None:
184187ECR repository, such as pushing, pulling, or managing your Docker images.
185188 """
186189 )
187- authorization_token = ecr_wrapper .get_authorization_token ()
190+
191+ authorization_token = self .ecr_wrapper .get_authorization_token ()
188192 print ("Authorization token retrieved." )
189193 press_enter_to_continue ()
190194 print_dashes ()
@@ -199,7 +203,9 @@ def run(self, role_arn: str) -> None:
199203correct container image from the ECR repository.
200204 """
201205 )
202- repository_descriptions = ecr_wrapper .describe_repositories ([self .repo_name ])
206+ repository_descriptions = self .ecr_wrapper .describe_repositories (
207+ [self .repository_name ]
208+ )
203209 repository_uri = repository_descriptions [0 ]["repositoryUri" ]
204210 print (f"Repository URI found: { repository_uri } " )
205211 press_enter_to_continue ()
@@ -229,14 +235,12 @@ def run(self, role_arn: str) -> None:
229235
230236The method uses the authorization token to create an `AuthConfig` object, which is used to authenticate
231237the Docker client when pushing the image. Finally, the method tags the Docker image with the specified
232- repository name and image tag , and then pushes the image to the ECR repository using the Docker client.
238+ repository name and image image_tag , and then pushes the image to the ECR repository using the Docker client.
233239 """
234240 )
235241 decoded_authorization = base64 .b64decode (authorization_token ).decode ("utf-8" )
236242 username , password = decoded_authorization .split (":" )
237243
238- print (f"username { username } \n { password } " )
239-
240244 resp = self .docker_client .api .push (
241245 repository = repository_uri ,
242246 auth_config = {"username" : username , "password" : password },
@@ -250,7 +254,9 @@ def run(self, role_arn: str) -> None:
250254 print_dashes ()
251255
252256 print ("* Verify if the image is in the ECR Repository." )
253- image_descriptions = ecr_wrapper .describe_images (self .repo_name , [self .tag ])
257+ image_descriptions = self .ecr_wrapper .describe_images (
258+ self .repository_name , [self .tag ]
259+ )
254260 if len (image_descriptions ) > 0 :
255261 print ("Image found in ECR Repository." )
256262 else :
@@ -273,7 +279,7 @@ def run(self, role_arn: str) -> None:
273279
2742802. Describe the image using this command:
275281
276- aws ecr describe-images --repository-name { self .repo_name } --image-ids imageTag={ self .tag }
282+ aws ecr describe-images --repository-name { self .repository_name } --image-ids imageTag={ self .tag }
277283
2782843. Run the Docker container and view the output using this command:
279285
@@ -291,11 +297,11 @@ def cleanup(self, ask: bool):
291297 if self .repository is not None and (
292298 not ask
293299 or q .ask (
294- f"Would you like to delete the ECR repository '{ self .repo_name } ? (y/n) "
300+ f"Would you like to delete the ECR repository '{ self .repository_name } ? (y/n) "
295301 )
296302 ):
297- print (f"Deleting the ECR repository '{ self .repo_name } '." )
298- self .ecr_wrapper .delete_repository (self .repo_name )
303+ print (f"Deleting the ECR repository '{ self .repository_name } '." )
304+ self .ecr_wrapper .delete_repository (self .repository_name )
299305
300306 if self .full_tag_name is not None and (
301307 not ask
@@ -325,7 +331,9 @@ def grant_role_download_access(self, role_arn: str):
325331 ],
326332 }
327333
328- self .ecr_wrapper .set_repository_policy (self .repo_name , json .dumps (policy_json ))
334+ self .ecr_wrapper .set_repository_policy (
335+ self .repository_name , json .dumps (policy_json )
336+ )
329337
330338 # snippet-end:[python.example_code.ecr.grant_role_download_access]
331339
@@ -350,7 +358,10 @@ def put_expiration_policy(self):
350358 ]
351359 }
352360
353- self .ecr_wrapper .put_lifecycle_policy (self .repo_name , json .dumps (policy_json ))
361+ self .ecr_wrapper .put_lifecycle_policy (
362+ self .repository_name , json .dumps (policy_json )
363+ )
364+
354365 # snippet-end:[python.example_code.ecr.put_expiration_policy]
355366
356367
@@ -374,15 +385,15 @@ def put_expiration_policy(self):
374385 no_art = args .no_art
375386 iam_role_arn = args .iam_role_arn
376387 demo = None
377- docker_client = None
388+ a_docker_client = None
378389 try :
379- docker_client = docker .from_env ()
380- if not docker_client .ping ():
390+ a_docker_client = docker .from_env ()
391+ if not a_docker_client .ping ():
381392 raise docker .errors .DockerException ("Docker is not running." )
382393 except docker .errors .DockerException as err :
383394 logging .error (
384395 """
385- The Python Docker could not be created.
396+ The Python Docker client could not be created.
386397 Do you have Docker installed and running?
387398 Here is the error message:
388399 %s
@@ -391,8 +402,8 @@ def put_expiration_policy(self):
391402 )
392403 sys .exit ("Error with Docker." )
393404 try :
394- ecr_wrapper = ECRWrapper .from_client ()
395- demo = ECRGettingStarted (ecr_wrapper , docker_client )
405+ an_ecr_wrapper = ECRWrapper .from_client ()
406+ demo = ECRGettingStarted (an_ecr_wrapper , a_docker_client )
396407 demo .run (iam_role_arn )
397408
398409 except Exception as exception :
0 commit comments