Use CreateRepository with an AWS SDK or CLI - AWS SDK Code Examples

There are more AWS SDK examples available in the AWS Doc SDK Examples GitHub repo.

Use CreateRepository with an AWS SDK or CLI

The following code examples show how to use CreateRepository.

Action examples are code excerpts from larger programs and must be run in context. You can see this action in context in the following code example:

CLI
AWS CLI

Example 1: To create a repository

The following create-repository example creates a repository inside the specified namespace in the default registry for an account.

aws ecr create-repository \ --repository-name project-a/nginx-web-app

Output:

{ "repository": { "registryId": "123456789012", "repositoryName": "sample-repo", "repositoryArn": "arn:aws:ecr:us-west-2:123456789012:repository/project-a/nginx-web-app" } }

For more information, see Creating a Repository in the Amazon ECR User Guide.

Example 2: To create a repository configured with image tag immutability

The following create-repository example creates a repository configured for tag immutability in the default registry for an account.

aws ecr create-repository \ --repository-name sample-repo \ --image-tag-mutability IMMUTABLE

Output:

{ "repository": { "registryId": "123456789012", "repositoryName": "sample-repo", "repositoryArn": "arn:aws:ecr:us-west-2:123456789012:repository/sample-repo", "imageTagMutability": "IMMUTABLE" } }

For more information, see Image Tag Mutability in the Amazon ECR User Guide.

Example 3: To create a repository configured with a scanning configuration

The following create-repository example creates a repository configured to perform a vulnerability scan on image push in the default registry for an account.

aws ecr create-repository \ --repository-name sample-repo \ --image-scanning-configuration scanOnPush=true

Output:

{ "repository": { "registryId": "123456789012", "repositoryName": "sample-repo", "repositoryArn": "arn:aws:ecr:us-west-2:123456789012:repository/sample-repo", "imageScanningConfiguration": { "scanOnPush": true } } }

For more information, see Image Scanning in the Amazon ECR User Guide.

Java
SDK for Java 2.x
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

/** * Creates an Amazon Elastic Container Registry (Amazon ECR) repository. * * @param repoName the name of the repository to create. * @return the Amazon Resource Name (ARN) of the created repository, or an empty string if the operation failed. * @throws IllegalArgumentException If repository name is invalid. * @throws RuntimeException if an error occurs while creating the repository. */ public String createECRRepository(String repoName) { if (repoName == null || repoName.isEmpty()) { throw new IllegalArgumentException("Repository name cannot be null or empty"); } CreateRepositoryRequest request = CreateRepositoryRequest.builder() .repositoryName(repoName) .build(); CompletableFuture<CreateRepositoryResponse> response = getAsyncClient().createRepository(request); try { CreateRepositoryResponse result = response.join(); if (result != null) { System.out.println("The " + repoName + " repository was created successfully."); return result.repository().repositoryArn(); } else { throw new RuntimeException("Unexpected response type"); } } catch (CompletionException e) { Throwable cause = e.getCause(); if (cause instanceof EcrException ex) { if ("RepositoryAlreadyExistsException".equals(ex.awsErrorDetails().errorCode())) { System.out.println("The Amazon ECR repository already exists, moving on..."); DescribeRepositoriesRequest describeRequest = DescribeRepositoriesRequest.builder() .repositoryNames(repoName) .build(); DescribeRepositoriesResponse describeResponse = getAsyncClient().describeRepositories(describeRequest).join(); return describeResponse.repositories().get(0).repositoryArn(); } else { throw new RuntimeException(ex); } } else { throw new RuntimeException(e); } } }
Kotlin
SDK for Kotlin
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

/** * Creates an Amazon Elastic Container Registry (Amazon ECR) repository. * * @param repoName the name of the repository to create. * @return the Amazon Resource Name (ARN) of the created repository, or an empty string if the operation failed. * @throws RepositoryAlreadyExistsException if the repository exists. * @throws EcrException if an error occurs while creating the repository. */ suspend fun createECRRepository(repoName: String?): String? { val request = CreateRepositoryRequest { repositoryName = repoName } return try { EcrClient { region = "us-east-1" }.use { ecrClient -> val response = ecrClient.createRepository(request) response.repository?.repositoryArn } } catch (e: RepositoryAlreadyExistsException) { println("Repository already exists: $repoName") repoName?.let { getRepoARN(it) } } catch (e: EcrException) { println("An error occurred: ${e.message}") null } }