Skip to content

Commit 64ea9f8

Browse files
committed
Add README content for using private reg creds
1 parent 3b67629 commit 64ea9f8

File tree

1 file changed

+136
-0
lines changed

1 file changed

+136
-0
lines changed

README.md

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Line Interface](http://aws.amazon.com/cli/) product detail page.
3434
- [Using Route53 Service Discovery](#using-route53-service-discovery)
3535
- [Viewing Running Tasks](#viewing-running-tasks)
3636
- [Viewing Container Logs](#viewing-container-logs)
37+
- [Using Private Registry Authentication](#using-private-registry-authentication)
3738
- [Amazon ECS CLI Commands](#amazon-ecs-cli-commands)
3839
- [Contributing to the CLI](#contributing-to-the-cli)
3940
- [License](#license)
@@ -837,6 +838,141 @@ OPTIONS:
837838
--timestamps, -t [Optional] Shows timestamps on each line in the log output.
838839
```
839840

841+
## Using Private Registry Authentication
842+
843+
If you want to use privately hosted container images with ECS, the ECS CLI can store your private registry credentials in AWS Secrets Manager and create an IAM role which ECS can use to access the credentials and private images. This allows you to:
844+
845+
* Store private registry credentials within AWS for use with ECS
846+
* Add the permissions needed to use your registry secrets to a new or existing Task Execution Role
847+
* Automatically add your private registry credentials to your task definition when running a task or service
848+
849+
Using privately hosted images with the ECS CLI is done in two parts:
850+
851+
1) Create new AWS Secrets Manager secrets and an IAM Task Execution Role with `ecs-cli registry-creds up`
852+
2) Run `ecs-cli compose` commands to create and run a task definition that includes the new resources
853+
854+
### Storing private registry credentials with `ecs-cli registry-creds up`
855+
856+
To get started, first create an input file that contains the name of your registry and the credentials needed to access it:
857+
858+
```
859+
# file name: cred_input.yml
860+
# when using environment variables, only '${VAR_NAME}' format is supported
861+
862+
version: '1'
863+
registry_credentials:
864+
my-registry.example.com:
865+
secrets_manager_arn: # required when using (with no modification) or updating an existing secret
866+
username: myUserName # required when creating or updating a new secret
867+
password: ${MY_PASSWORD} # required when creating or updating a new secret
868+
kms_key_id: # optional custom KMS Key ID to use to encrypt new secret
869+
container_names: # required to match credential resources with docker-compose services
870+
- web
871+
- log
872+
```
873+
874+
In this example, we're storing credentials for a registry called `my-registry.example.com` and passing in the password with an environment variable. `container_names` is a list of the `service_names` in your Docker Compose project which need access to images in this registry. If you don't plan to use the output of `registry-creds up` to launch a task or service with `compose`, then you can leave this field empty.
875+
876+
Other options:
877+
* To store credentials for multiple private registries, add additional (up to 10 total) registry names and their required details as separate keys under `registry_credentials`.
878+
* Existing registry secrets from other regions can be included by specifying their `secrets_manager_arn` and associated `kms_key_id`. Creating or updating secrets must be done from within that region.
879+
* If you want to encrypt the AWS Secrets Manager secret for your registry with a custom KMS Key, then add the ARN, ID or Alias of the Key in the `kms_key_id` field. Otherwise, AWS Secrets Manager will use the default key in your account.
880+
* If you don't want to create or update an IAM Task Execution Role for these secrets, use the `--no-role` flag instead of specifying a role name.
881+
* If you don't want to generate an output file for use with `compose` or for records purposes, use the `--no-output-file` flag.
882+
* If you want the output file to be created in a specific directory on your machine, you can specify it with the `--output-dir <value>` flag. Otherwise, the file will be created in your working directory.
883+
884+
After creating the input file, run the `registry-creds up` command on the file and pass in the name of the new or existing Task Execution Role you want to use for the secrets:
885+
886+
```
887+
$ ecs-cli registry-creds up ./cred_input.yml --role-name myTaskExecutionRole
888+
```
889+
890+
The command will output the names of the resources it creates, including the name of the output file which was generated:
891+
892+
```
893+
$ ecs-cli registry-creds up ./cred_input.yml --role-name myTaskExecutionRole
894+
INFO[0000] Processing credentials for registry my-registry.example.com...
895+
INFO[0000] New credential secret created: arn:aws:secretsmanager:region:aws_account_id:secret:amazon-ecs-cli-setup-my-registry.example.com-VeDqXm
896+
INFO[0000] Creating resources for task execution role myTaskExecutionRole...
897+
INFO[0000] Created new task execution role arn:aws:iam::aws_account_id:role/myTaskExecutionRole
898+
INFO[0000] Created new task execution role policy arn:aws:iam::aws_account_id:policy/amazon-ecs-cli-setup-myTaskExecutionRole-policy-20181023T210805Z
899+
INFO[0000] Attached AWS managed policy arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy to role myTaskExecutionRole
900+
INFO[0001] Attached new policy arn:aws:iam::aws_account_id:policy/amazon-ecs-cli-setup-myTaskExecutionRole-policy-20181023T210805Z to role myTaskExecutionRole
901+
INFO[0001] Writing registry credential output to new file C:\Users\myuser\regcreds\regCredTest\ecs-registry-creds_20181023T210805Z.yml
902+
```
903+
904+
The output file `ecs-registry-creds_20181023T210805Z.yml` should like like this:
905+
```
906+
version: "1"
907+
registry_credential_outputs:
908+
task_execution_role: myTaskExecutionRole
909+
container_credentials:
910+
my-registry.example.com:
911+
credentials_parameter: arn:aws:secretsmanager:region:aws_account_id:secret:amazon-ecs-cli-setup-my-registry.example.com-VeDqXm
912+
container_names:
913+
- web
914+
- log
915+
```
916+
917+
This file contains:
918+
* the name of the IAM Task Execution Role with permissions for the new secrets
919+
* the ARN of the new `credentials_parameter` created for the registry
920+
* the list of containers the new `credentials_parameter` should be used for when running a task or service
921+
922+
We can now use this file with `ecs-cli compose` commands to start a task with images in our private registry.
923+
924+
### Using private registry credentials when launching tasks or services
925+
926+
Now that we have an output file that identifies which resources we need to use our private registry, the ECS CLI will incorporate them into our Docker Compose project when we run `ecs-cli compose`.
927+
928+
In the same directory (let's call it "privateImageApp"), create a docker-compose.yml file for your application:
929+
930+
```
931+
version: "3"
932+
services:
933+
web:
934+
environment:
935+
- SERVICE_NAME=web
936+
image: my-registry.example.com/httpd
937+
ports:
938+
- "80:80"
939+
log:
940+
environment:
941+
- SERVICE_NAME=log
942+
image: my-registry.example.com/logging
943+
logging:
944+
driver: awslogs
945+
options:
946+
awslogs-group: myApps
947+
awslogs-region: us-west-2
948+
awslogs-stream-prefix: privateImageApp
949+
```
950+
951+
Now run the command `ecs-cli compose up` to launch a task. The ECS CLI will automatically detect and use the newest `ecs-registry-creds` file within the current directory:
952+
953+
```
954+
$~\privateImageApp> ecs-cli compose up
955+
INFO[0000] Found ecs-registry-creds file C:\Users\myuser\regcreds\regCredTest\ecs-registry-creds_20181023T210805Z.yml
956+
INFO[0000] Using ecs-registry-creds value arn:aws:secretsmanager:region:aws_account_id:secret:amazon-ecs-cli-setup-my-registry.example.com-VeDqXm container name=web option name=credentials_parameter
957+
Using ecs-registry-creds value arn:aws:secretsmanager:region:aws_account_id:secret:amazon-ecs-cli-setup-my-registry.example.com-VeDqXm container name=log option name=credentials_parameter
958+
INFO[0000] Using ecs-registry-creds value myTaskExecutionRole option name=task_execution_role
959+
INFO[0000] Using ECS task definition TaskDefinition="privateImageApp:1"
960+
INFO[0000] Starting container... container=bf35a813-dd76-4fe0-b5a2-c1334c2331f4/web
961+
INFO[0000] Starting container... container=bf35a813-dd76-4fe0-b5a2-c1334c2331f4/log
962+
INFO[0012] Describe ECS container status container=bf35a813-dd76-4fe0-b5a2-c1334c2331f4/web desiredStatus=RUNNING lastStatus=PENDING taskDefinition="privateImageApp:1"
963+
INFO[0013] Describe ECS container status container=bf35a813-dd76-4fe0-b5a2-c1334c2331f4/log desiredStatus=RUNNING lastStatus=PENDING taskDefinition="privateImageApp:1"
964+
INFO[0018] Started container... container=bf35a813-dd76-4fe0-b5a2-c1334c2331f4/web desiredStatus=RUNNING lastStatus=RUNNING taskDefinition="privateImageApp:1"
965+
INFO[0018] Started container... container=bf35a813-dd76-4fe0-b5a2-c1334c2331f4/log desiredStatus=RUNNING lastStatus=RUNNING taskDefinition="privateImageApp:1"
966+
```
967+
968+
The within your new task definition `privateImageApp:1`, the container definitions for both `web` and `log` should have your "my-registry.example.com" secret as a `credentialsParameter`. The `executionRoleArn` field will be the role we created in the previous step, "myTaskExecutionRole".
969+
970+
Other options:
971+
* to use an ecs-registry-creds output file from outside the current directory, you can specify it in with the `--registry-creds <value>` flag
972+
973+
For more information about using private registries with ECS, see [Private Registry Authentication for Tasks](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/private-auth.html).
974+
975+
840976
## Amazon ECS CLI Commands
841977

842978
For a complete list of commands, see the

0 commit comments

Comments
 (0)