Private registry with AWS Secrets Manager sample for CodeBuild
This sample shows you how to use a Docker image that is stored in a private registry as your AWS CodeBuild runtime environment. The credentials for the private registry are stored in AWS Secrets Manager. Any private registry works with CodeBuild. This sample uses Docker Hub.
Note
Secrets are visible to actions and are not masked when written to a file.
Private registry sample requirements
To use a private registry with AWS CodeBuild, you must have the following:
-
A Secrets Manager secret that stores your Docker Hub credentials. The credentials are used to access your private repository.
Note
You will be charged for secrets that you create.
-
A private repository or account.
-
A CodeBuild service role IAM policy that grants access to your Secrets Manager secret.
Follow these steps to create these resources and then create a CodeBuild build project using the Docker images stored in your private registry.
Create a CodeBuild project with a private registry
-
For information about how to create a free private repository, see Repositories on Docker Hub
. You can also run the following commands in a terminal to pull an image, get its ID, and push it to a new repository. docker pull amazonlinux docker images amazonlinux --format {{.ID}} docker tagimage-idyour-username/repository-name:tagdocker login docker pushyour-username/repository-name -
Follow the steps in Create an AWS Secrets Manager secret in the AWS Secrets Manager User Guide.
-
In step 3, in Choose secret type, choose Other type of secret.
-
In Key/value pairs, create one key-value pair for your Docker Hub user name and one key-value pair for your Docker Hub password.
-
Continue following the steps in Create an AWS Secrets Manager secret.
-
In step 5, on the Configure automatic rotation page, turn it off because the keys correspond to your Docker Hub credentials.
-
Finish following the steps in Create an AWS Secrets Manager secret.
For more information, see What is AWS Secrets Manager?
-
-
When you create an AWS CodeBuild project in the console, CodeBuild attaches the required permission for you. If you use an AWS KMS key other than
DefaultEncryptionKey, you must add it to the service role. For more information, see Modifying a role (console) in the IAM User Guide.For your service role to work with Secrets Manager, it must have, at a minimum, the
secretsmanager:GetSecretValuepermission.
-
To use the console to create a project with an environment stored in a private registry, do the following while you create a project. For information, see Create a build project (console).
Note
If your private registry is in your VPC, it must have public internet access. CodeBuild cannot pull an image from a private IP address in a VPC.
-
In Environment image, choose Custom image.
-
For Environment type, choose Linux or Windows.
-
For Image registry, choose Other registry.
-
In External registry URL, enter the image location and in Registry credential - optional enter the ARN or name of your Secrets Manager credentials.
Note
If your credentials do not exist in your current Region, then you must use the ARN. You cannot use the credential name if the credentials exist in a different Region.
-

