-
Notifications
You must be signed in to change notification settings - Fork 22
/
main.tf
133 lines (121 loc) · 3.37 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
terraform {
cloud {
organization = "govuk"
workspaces {
tags = ["ecr", "eks", "aws"]
}
}
required_version = "~> 1.5"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
github = {
source = "integrations/github"
version = "~> 6.0"
}
}
}
provider "aws" {
region = "eu-west-1"
default_tags {
tags = {
Product = "GOV.UK"
System = "Elastic Container Registry"
Environment = var.govuk_environment
Owner = "govuk-platform-engineering@digital.cabinet-office.gov.uk"
repository = "govuk-infrastructure"
terraform_deployment = basename(abspath(path.root))
}
}
}
data "aws_secretsmanager_secret" "github-token" {
name = "govuk/terraform-cloud/github-token"
}
data "aws_secretsmanager_secret_version" "github-token" {
secret_id = data.aws_secretsmanager_secret.github-token.id
}
provider "github" {
owner = "alphagov"
token = data.aws_secretsmanager_secret_version.github-token.secret_string
}
data "github_repositories" "govuk" {
query = "org:alphagov topic:container topic:govuk fork:false archived:false"
}
locals {
repositories = concat(
local.extra_repositories,
data.github_repositories.govuk.names
)
extra_repositories = [
"mongodb",
"imminence",
"toolbox",
"clamav",
"search-api-learn-to-rank",
"licensify-backend",
"licensify-feed",
"licensify-frontend",
]
}
data "aws_caller_identity" "current" {}
data "aws_region" "current" {}
resource "aws_ecr_repository" "repositories" {
for_each = toset(local.repositories)
name = each.key
image_tag_mutability = "MUTABLE" # To support a movable `latest` for developer convenience.
image_scanning_configuration { scan_on_push = true }
}
resource "aws_ecr_lifecycle_policy" "ecr_lifecycle_policy" {
for_each = toset([for repo in local.repositories : aws_ecr_repository.repositories[repo].name])
repository = each.key
policy = jsonencode({
"rules" : [
{
"rulePriority" : 1,
"description" : "Keep last 20 images with tag prefix deployed-to",
"selection" : {
"tagStatus" : "tagged",
"tagPrefixList" : ["deployed-to"],
"countType" : "imageCountMoreThan",
"countNumber" : 20
},
"action" : { "type" : "expire" }
},
{
"rulePriority" : 2,
"description" : "Expire untagged images older than 1 day",
"selection" : {
"tagStatus" : "untagged",
"countType" : "sinceImagePushed",
"countUnit" : "days",
"countNumber" : 1
},
"action" : { "type" : "expire" }
},
{
"rulePriority" : 4,
"description" : "Keep last 20 images with tag prefix v",
"selection" : {
"tagStatus" : "tagged",
"tagPrefixList" : ["v"],
"countType" : "imageCountMoreThan",
"countNumber" : 20
},
"action" : { "type" : "expire" }
},
{
"rulePriority" : 5,
"description" : "Expire images older than 30 days",
"selection" : {
"tagStatus" : "any",
"countType" : "sinceImagePushed",
"countUnit" : "days",
"countNumber" : 30
},
"action" : { "type" : "expire" }
}
]
})
}