This repository has been archived by the owner on Nov 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
tfaws-ecr.w
88 lines (75 loc) · 2.19 KB
/
tfaws-ecr.w
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
bring "@cdktf/provider-aws" as aws;
bring "cdktf" as cdktf;
bring "@cdktf/provider-null" as null_provider;
bring "./aws.w" as aws_info;
struct RepositoryProps {
directory: str;
name: str;
tag: str;
}
pub class Repository {
pub image: str;
pub deps: Array<cdktf.ITerraformDependable>;
init(props: RepositoryProps) {
let deps = MutArray<cdktf.ITerraformDependable>[];
let count = 5;
let r = new aws.ecrRepository.EcrRepository(
name: props.name,
forceDelete: true,
imageTagMutability: "IMMUTABLE",
imageScanningConfiguration: {
scanOnPush: true,
}
);
deps.push(r);
new aws.ecrLifecyclePolicy.EcrLifecyclePolicy(
repository: r.name,
policy: Json.stringify({
rules: [
{
rulePriority: 1,
description: "Keep only the last ${count} untagged images.",
selection: {
tagStatus: "untagged",
countType: "imageCountMoreThan",
countNumber: count
},
action: {
type: "expire"
}
}
]
})
);
let awsInfo = aws_info.Aws.getOrCreate(this);
let region = awsInfo.region();
let accountId = awsInfo.accountId();
let image = "${r.repositoryUrl}:${props.tag}";
let arch = "linux/amd64";
// null provider singleton
let stack = cdktf.TerraformStack.of(this);
let nullProviderId = "NullProvider";
if !stack.node.tryFindChild(nullProviderId)? {
new null_provider.provider.NullProvider() as nullProviderId in stack;
}
let publish = new null_provider.resource.Resource(
dependsOn: [r],
triggers: {
tag: image,
},
provisioners: [
{
type: "local-exec",
command: [
"aws ecr get-login-password --region ${region} | docker login --username AWS --password-stdin ${accountId}.dkr.ecr.${region}.amazonaws.com || exit 1",
"docker buildx build --platform ${arch} -t ${image} ${props.directory} || exit 1",
"docker push ${image} || exit 1",
].join("\n")
}
],
);
deps.push(publish);
this.image = image;
this.deps = deps.copy();
}
}