Skip to content

Commit 05e6fd0

Browse files
feat: Add credentials_arn to support ECR pull through cache (#30)
1 parent 6aceada commit 05e6fd0

File tree

7 files changed

+93
-28
lines changed

7 files changed

+93
-28
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
repos:
22
- repo: https://github.com/antonbabenko/pre-commit-terraform
3-
rev: v1.88.2
3+
rev: v1.88.3
44
hooks:
55
- id: terraform_fmt
66
- id: terraform_wrapper_module_for_each

README.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,19 @@ module "ecr_registry" {
111111
Resource = [
112112
"arn:aws:ecr:us-east-1:012345678901:repository/*"
113113
]
114+
}, {
115+
Sid = "dockerhub",
116+
Effect = "Allow",
117+
Principal = {
118+
"AWS" : "arn:aws:iam::012345678901:root"
119+
},
120+
Action = [
121+
"ecr:CreateRepository",
122+
"ecr:BatchImportUpstreamImage"
123+
],
124+
Resource = [
125+
"arn:aws:ecr:us-east-1:012345678901:repository/dockerhub/*"
126+
]
114127
}
115128
]
116129
})
@@ -121,6 +134,11 @@ module "ecr_registry" {
121134
ecr_repository_prefix = "ecr-public"
122135
upstream_registry_url = "public.ecr.aws"
123136
}
137+
dockerhub = {
138+
ecr_repository_prefix = "dockerhub"
139+
upstream_registry_url = "registry-1.docker.io"
140+
credential_arn = "arn:aws:secretsmanager:us-east-1:123456789:secret:ecr-pullthroughcache/dockerhub"
141+
}
124142
}
125143
126144
# Registry Scanning Configuration
@@ -193,13 +211,13 @@ Examples codified under the [`examples`](https://github.com/terraform-aws-module
193211
| Name | Version |
194212
|------|---------|
195213
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0 |
196-
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.0 |
214+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.37 |
197215

198216
## Providers
199217

200218
| Name | Version |
201219
|------|---------|
202-
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 5.0 |
220+
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 5.37 |
203221

204222
## Modules
205223

examples/complete/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ Note that this example may create resources which will incur monetary charges on
2828
| Name | Version |
2929
|------|---------|
3030
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0 |
31-
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.0 |
31+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.37 |
3232

3333
## Providers
3434

3535
| Name | Version |
3636
|------|---------|
37-
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 5.0 |
37+
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 5.37 |
3838

3939
## Modules
4040

@@ -44,14 +44,14 @@ Note that this example may create resources which will incur monetary charges on
4444
| <a name="module_ecr_disabled"></a> [ecr\_disabled](#module\_ecr\_disabled) | ../.. | n/a |
4545
| <a name="module_ecr_registry"></a> [ecr\_registry](#module\_ecr\_registry) | ../.. | n/a |
4646
| <a name="module_public_ecr"></a> [public\_ecr](#module\_public\_ecr) | ../.. | n/a |
47+
| <a name="module_secrets_manager_dockerhub_credentials"></a> [secrets\_manager\_dockerhub\_credentials](#module\_secrets\_manager\_dockerhub\_credentials) | terraform-aws-modules/secrets-manager/aws | ~> 1.0 |
4748

4849
## Resources
4950

5051
| Name | Type |
5152
|------|------|
5253
| [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |
5354
| [aws_iam_policy_document.registry](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
54-
| [aws_partition.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/partition) | data source |
5555

5656
## Inputs
5757

examples/complete/main.tf

Lines changed: 66 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ locals {
66
region = "us-east-1"
77
name = "ecr-ex-${replace(basename(path.cwd), "_", "-")}"
88

9+
account_id = data.aws_caller_identity.current.account_id
10+
911
tags = {
1012
Name = local.name
1113
Example = local.name
@@ -14,7 +16,6 @@ locals {
1416
}
1517

1618
data "aws_caller_identity" "current" {}
17-
data "aws_partition" "current" {}
1819

1920
################################################################################
2021
# ECR Repository
@@ -101,16 +102,25 @@ data "aws_iam_policy_document" "registry" {
101102
statement {
102103
principals {
103104
type = "AWS"
104-
identifiers = ["arn:${data.aws_partition.current.partition}:iam::${data.aws_caller_identity.current.account_id}:root"]
105+
identifiers = ["arn:aws:iam::${local.account_id}:root"]
105106
}
106107

107-
actions = [
108-
"ecr:ReplicateImage",
109-
]
108+
actions = ["ecr:ReplicateImage"]
109+
resources = [module.ecr.repository_arn]
110+
}
111+
112+
statement {
113+
sid = "dockerhub"
110114

111-
resources = [
112-
module.ecr.repository_arn,
115+
principals {
116+
type = "AWS"
117+
identifiers = ["arn:aws:iam::${local.account_id}:root"]
118+
}
119+
actions = [
120+
"ecr:CreateRepository",
121+
"ecr:BatchImportUpstreamImage"
113122
]
123+
resources = ["arn:aws:ecr-public::${local.account_id}:repository/dockerhub/*"]
114124
}
115125
}
116126

@@ -129,6 +139,11 @@ module "ecr_registry" {
129139
ecr_repository_prefix = "ecr-public"
130140
upstream_registry_url = "public.ecr.aws"
131141
}
142+
dockerhub = {
143+
ecr_repository_prefix = "dockerhub"
144+
upstream_registry_url = "registry-1.docker.io"
145+
credential_arn = module.secrets_manager_dockerhub_credentials.secret_arn
146+
}
132147
}
133148

134149
# Registry Scanning Configuration
@@ -159,22 +174,53 @@ module "ecr_registry" {
159174

160175
# Registry Replication Configuration
161176
create_registry_replication_configuration = true
162-
registry_replication_rules = [
163-
{
164-
destinations = [{
165-
region = "us-west-2"
166-
registry_id = data.aws_caller_identity.current.account_id
167-
}, {
168-
region = "eu-west-1"
169-
registry_id = data.aws_caller_identity.current.account_id
170-
}]
177+
registry_replication_rules = [{
178+
destinations = [{
179+
region = "us-west-2"
180+
registry_id = local.account_id
181+
}, {
182+
region = "eu-west-1"
183+
registry_id = local.account_id
184+
}]
185+
186+
repository_filters = [{
187+
filter = "prod-microservice"
188+
filter_type = "PREFIX_MATCH"
189+
}]
190+
}]
191+
192+
tags = local.tags
193+
}
171194

172-
repository_filters = [{
173-
filter = "prod-microservice"
174-
filter_type = "PREFIX_MATCH"
195+
module "secrets_manager_dockerhub_credentials" {
196+
source = "terraform-aws-modules/secrets-manager/aws"
197+
version = "~> 1.0"
198+
199+
# Secret names must contain 1-512 Unicode characters and be prefixed with ecr-pullthroughcache/
200+
name_prefix = "ecr-pullthroughcache/dockerhub-credentials"
201+
description = "Dockerhub credentials"
202+
203+
# For example only
204+
recovery_window_in_days = 0
205+
secret_string = jsonencode({
206+
username = "example"
207+
accessToken = "YouShouldNotStoreThisInPlainText"
208+
})
209+
210+
# Policy
211+
create_policy = true
212+
block_public_policy = true
213+
policy_statements = {
214+
read = {
215+
sid = "AllowAccountRead"
216+
principals = [{
217+
type = "AWS"
218+
identifiers = ["arn:aws:iam::${local.account_id}:root"]
175219
}]
220+
actions = ["secretsmanager:GetSecretValue"]
221+
resources = ["*"]
176222
}
177-
]
223+
}
178224

179225
tags = local.tags
180226
}

examples/complete/versions.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ terraform {
44
required_providers {
55
aws = {
66
source = "hashicorp/aws"
7-
version = ">= 5.0"
7+
version = ">= 5.37"
88
}
99
}
1010
}

main.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ resource "aws_ecr_pull_through_cache_rule" "this" {
270270

271271
ecr_repository_prefix = each.value.ecr_repository_prefix
272272
upstream_registry_url = each.value.upstream_registry_url
273+
credential_arn = try(each.value.credentials_arn, null)
273274
}
274275

275276
################################################################################

versions.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ terraform {
44
required_providers {
55
aws = {
66
source = "hashicorp/aws"
7-
version = ">= 5.0"
7+
version = ">= 5.37"
88
}
99
}
1010
}

0 commit comments

Comments
 (0)