Skip to content

Commit d8eec08

Browse files
Merge pull request #221 from cloudnativedaysjp/ecr
manage ECR repositories in ap-northeast-1
2 parents be0405e + 6f7cc55 commit d8eec08

File tree

2 files changed

+100
-18
lines changed

2 files changed

+100
-18
lines changed

ecr/ecr.tf

Lines changed: 94 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,39 @@
11
#
2-
# Private repositories are created by the following policy.
2+
# Each private repositories will be pushed the following image tags.
33
#
4-
# * us-west-1:
5-
# * pushed tags formatted commit-hash
6-
# * following lifecycle policies
7-
# * against untagged image, expired 30 days after it was pushed
8-
# * against tagged image, no policy
4+
# * us-west-2:
5+
# * pushed tags formatted commit-hash & branch-name
6+
# * ap-northeast-1:
7+
# * pushed tags formatted commit-hash & semver format
98
#
109

1110
locals {
12-
repositories = [
13-
"dreamkast-ecs",
14-
"dreamkast-ui",
15-
"dreamkast-weaver",
16-
"dreamkast-external-scaler",
17-
"emtec-ecu/emtectl",
18-
"emtec-ecu/server",
19-
"seaman",
20-
]
11+
repositories = {
12+
"us-west-2" : [
13+
"dreamkast-ecs",
14+
"dreamkast-ui",
15+
"dreamkast-weaver",
16+
"dreamkast-external-scaler",
17+
"emtec-ecu/emtectl",
18+
"emtec-ecu/server",
19+
"seaman",
20+
],
21+
"ap-northeast-1" : [
22+
"dreamkast-ecs",
23+
"dreamkast-ui",
24+
"dreamkast-weaver",
25+
"seaman",
26+
],
27+
}
2128
}
2229

30+
#
31+
# us-west-2
32+
#
33+
2334
resource "aws_ecr_repository" "us_west_2" {
24-
for_each = toset(local.repositories)
35+
provider = aws
36+
for_each = toset(local.repositories.us-west-2)
2537

2638
name = each.key
2739
image_tag_mutability = "MUTABLE"
@@ -32,7 +44,8 @@ resource "aws_ecr_repository" "us_west_2" {
3244
}
3345

3446
resource "aws_ecr_lifecycle_policy" "us_west_2" {
35-
for_each = toset(local.repositories)
47+
provider = aws
48+
for_each = toset(local.repositories.us-west-2)
3649

3750
repository = aws_ecr_repository.us_west_2[each.key].name
3851
policy = <<EOF
@@ -70,7 +83,70 @@ resource "aws_ecr_lifecycle_policy" "us_west_2" {
7083
EOF
7184
}
7285

73-
resource "aws_ecr_pull_through_cache_rule" "ecr_public" {
86+
resource "aws_ecr_pull_through_cache_rule" "us_west_2" {
87+
provider = aws
88+
ecr_repository_prefix = "ecr-public"
89+
upstream_registry_url = "public.ecr.aws"
90+
}
91+
92+
#
93+
# asia-northeast-1
94+
#
95+
96+
resource "aws_ecr_repository" "ap_northeast_1" {
97+
provider = aws.ap-northeast-1
98+
for_each = toset(local.repositories.ap-northeast-1)
99+
100+
name = each.key
101+
image_tag_mutability = "MUTABLE"
102+
103+
image_scanning_configuration {
104+
scan_on_push = true
105+
}
106+
}
107+
108+
resource "aws_ecr_lifecycle_policy" "ap_northeast_1" {
109+
provider = aws.ap-northeast-1
110+
for_each = toset(local.repositories.ap-northeast-1)
111+
112+
repository = aws_ecr_repository.ap_northeast_1[each.key].name
113+
policy = <<EOF
114+
{
115+
"rules": [
116+
{
117+
"rulePriority": 1,
118+
"description": "Expire untagged images older than 3 days",
119+
"selection": {
120+
"tagStatus": "untagged",
121+
"countType": "sinceImagePushed",
122+
"countUnit": "days",
123+
"countNumber": 3
124+
},
125+
"action": {
126+
"type": "expire"
127+
}
128+
},
129+
{
130+
"rulePriority": 2,
131+
"description": "Expire images older than 30 days",
132+
"selection": {
133+
"tagStatus": "tagged",
134+
"tagPrefixList": ["commit-"],
135+
"countType": "sinceImagePushed",
136+
"countUnit": "days",
137+
"countNumber": 30
138+
},
139+
"action": {
140+
"type": "expire"
141+
}
142+
}
143+
]
144+
}
145+
EOF
146+
}
147+
148+
resource "aws_ecr_pull_through_cache_rule" "ap_northeast_1" {
149+
provider = aws.ap-northeast-1
74150
ecr_repository_prefix = "ecr-public"
75151
upstream_registry_url = "public.ecr.aws"
76152
}

ecr/terraform.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,11 @@ terraform {
1515
}
1616

1717
provider "aws" {
18+
// default provider
1819
region = "us-west-2"
1920
}
21+
22+
provider "aws" {
23+
alias = "ap-northeast-1"
24+
region = "ap-northeast-1"
25+
}

0 commit comments

Comments
 (0)