Skip to content

Commit 1518fc1

Browse files
committed
ecr updates
1 parent e847b21 commit 1518fc1

File tree

5 files changed

+75
-52
lines changed

5 files changed

+75
-52
lines changed

bootstrap/terraform-fully-private/README.md

+10-20
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,9 @@ git clone https://github.com/awslabs/crossplane-on-eks.git
6969
For that reason `upbound_aws_provider.enable` is set to `true` and `aws_provider.enable` is set to `false`. If you use the examples for `aws_provider`, adjust the terraform [main.tf](https://github.com/awslabs/crossplane-on-eks/blob/main/bootstrap/terraform/main.tf) in order install only the necessary CRDs to the Kubernetes cluster.
7070

7171
#### Step 1: ECR settings
72-
Replace `your-docker-username` and `your-docker-password` with your actual Docker credentials and run the following command to create an aws secretmanager secret:
73-
```
74-
aws secretsmanager create-secret --name ecr-pullthroughcache/docker --description "Docker credentials" --secret-string '{"username":"your-docker-username","accessToken":"your-docker-password"}'
75-
```
76-
Create an ecr creation template trough the AWS Console. Creation templates is in Preview and there is no aws cli command or api to create the template.
77-
Navigate to ECR -> Private registry -> Settings -> Creation templates -> Create template ->
78-
Select "Any prefix in your ECR registry" and keep the defaults.
79-
80-
![ecr-createtemplate](../../docs/images/ecr-template.gif)
81-
8272
Note: You can change the default `us-east-1` region in the following scripts before executing them.
8373

84-
to Create Crossplane private ECR repos, run the following script:
74+
To Create Crossplane private ECR repos, run the following script:
8575

8676
```
8777
./scripts/create-crossplane-ecr-repos.sh
@@ -105,33 +95,33 @@ terraform init
10595
```
10696

10797
#### Step 3: Run Terraform PLAN
108-
Before running the Terraform plan, ensure you adjust the variables.tf file to include the following required variables:
98+
If your ECR repo is in different account or region than where the Terraform is pointing to, you can adjust the variables.tf file:
10999

110100
```
111101
variable "ecr_account_id" {
112102
type = string
113-
description = "ECR repository AWS Account ID"
114-
default = ""
103+
description = "ECR repository AWS Account ID"
104+
default = "" #defaults to var.region
115105
}
116106
117107
variable "ecr_region" {
118108
type = string
119109
description = "ECR repository AWS Region"
120-
default = ""
110+
default = "" #defaults to current account
121111
}
122112
```
123-
Make sure to replace the default values with your specific AWS Account ID and Region.
124113

114+
Run Terraform plan:
125115
```shell script
126-
export TF_VAR_region=<ENTER YOUR REGION> # Select your own region
116+
export TF_VAR_region=<ENTER YOUR REGION> # if ommited, defaults to var.region
127117
terraform plan
128118
```
129119

130120
#### Step 4: Finally, Terraform APPLY
131-
to create resources
121+
To create resources:
132122

133123
```shell script
134-
terraform apply --auto-approve
124+
terraform apply -var='docker_secret={"username":"your-docker-username", "accessToken":"your-docker-password"}' --auto-approve
135125
```
136126

137127
### Configure `kubectl` and test cluster
@@ -142,7 +132,7 @@ This following command used to update the `kubeconfig` in your local machine whe
142132

143133
`~/.kube/config` file gets updated with cluster details and certificate from the below command
144134
```shell script
145-
aws eks --region <enter-your-region> update-kubeconfig --name <cluster-name>
135+
aws eks --region <enter-your-region> update-kubeconfig --name <cluster-name> --alias <cluster-name>
146136
```
147137
#### Step 6: List all the worker nodes by running the command below
148138
```shell script
+46-29
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,53 @@
1-
data "aws_secretsmanager_secret" "docker" {
2-
name = "ecr-pullthroughcache/docker"
1+
locals {
2+
ecr_account_id = var.ecr_account_id != "" ? var.ecr_account_id : data.aws_caller_identity.current.account_id
3+
ecr_region = var.ecr_region != "" ? var.ecr_region : local.region
34
}
45

5-
resource "aws_ecr_registry_scanning_configuration" "configuration" {
6-
scan_type = "BASIC"
6+
module "secrets-manager" {
7+
source = "terraform-aws-modules/secrets-manager/aws"
8+
version = "1.1.2"
79

8-
rule {
9-
scan_frequency = "SCAN_ON_PUSH"
10-
repository_filter {
11-
filter = "*"
12-
filter_type = "WILDCARD"
13-
}
14-
}
15-
}
16-
17-
resource "aws_ecr_pull_through_cache_rule" "docker-hub" {
18-
ecr_repository_prefix = "docker-hub"
19-
upstream_registry_url = "registry-1.docker.io"
20-
credential_arn = data.aws_secretsmanager_secret.docker.arn
10+
name = "ecr-pullthroughcache/docker"
11+
secret_string = jsonencode(var.docker_secret)
2112
}
2213

23-
resource "aws_ecr_pull_through_cache_rule" "ecr" {
24-
ecr_repository_prefix = "ecr"
25-
upstream_registry_url = "public.ecr.aws"
26-
}
14+
module "ecr" {
15+
source = "terraform-aws-modules/ecr/aws"
16+
version = "2.2.1"
2717

28-
resource "aws_ecr_pull_through_cache_rule" "k8s" {
29-
ecr_repository_prefix = "k8s"
30-
upstream_registry_url = "registry.k8s.io"
31-
}
18+
create_repository = false
3219

33-
resource "aws_ecr_pull_through_cache_rule" "quay" {
34-
ecr_repository_prefix = "quay"
35-
upstream_registry_url = "quay.io"
36-
}
20+
registry_pull_through_cache_rules = {
21+
ecr = {
22+
ecr_repository_prefix = "ecr"
23+
upstream_registry_url = "public.ecr.aws"
24+
}
25+
k8s = {
26+
ecr_repository_prefix = "k8s"
27+
upstream_registry_url = "registry.k8s.io"
28+
}
29+
quay = {
30+
ecr_repository_prefix = "quay"
31+
upstream_registry_url = "quay.io"
32+
}
33+
dockerhub = {
34+
ecr_repository_prefix = "docker-hub"
35+
upstream_registry_url = "registry-1.docker.io"
36+
credential_arn = module.secrets-manager.secret_arn
37+
}
38+
}
39+
40+
manage_registry_scanning_configuration = true
41+
registry_scan_type = "BASIC"
42+
registry_scan_rules = [
43+
{
44+
scan_frequency = "SCAN_ON_PUSH"
45+
filter = [
46+
{
47+
filter = "*"
48+
filter_type = "WILDCARD"
49+
},
50+
]
51+
}
52+
]
53+
}

bootstrap/terraform-fully-private/main.tf

-3
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,6 @@ locals {
4646
name = var.name
4747
region = var.region
4848

49-
ecr_account_id = var.ecr_account_id != "" ? var.ecr_account_id : data.aws_caller_identity.current.account_id
50-
ecr_region = var.ecr_region != "" ? var.ecr_region : local.region
51-
5249
cluster_version = var.cluster_version
5350
cluster_name = local.name
5451

bootstrap/terraform-fully-private/variables.tf

+19
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,22 @@ variable "ecr_region" {
6161
default = ""
6262
}
6363

64+
variable "docker_secret" {
65+
type = object({
66+
username = string
67+
accessToken = string
68+
})
69+
default = {
70+
username = ""
71+
accessToken = ""
72+
}
73+
sensitive = true
74+
validation {
75+
condition = !(var.docker_secret.username == "" || var.docker_secret.accessToken == "")
76+
error_message = <<EOT
77+
Both username and accessToken must be provided.
78+
Use the following command to pass these variables:
79+
terraform plan -var='docker_secret={"username":"your_username", "accessToken":"your_access_token"}'
80+
EOT
81+
}
82+
}

docs/images/ecr-template.gif

-2.82 MB
Binary file not shown.

0 commit comments

Comments
 (0)