-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
125 lines (114 loc) · 3.98 KB
/
main.ts
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
import * as path from 'path';
import {
Construct,
} from "constructs";
import {
App,
DataResource,
TerraformStack,
TerraformVariable,
} from "cdktf";
import {
AwsProvider,
} from "@cdktf/provider-aws/lib/provider";
import {
Ecr
} from "./.gen/modules/ecr";
function toSnakeCaseIdentifier(name: string): string {
return name.toLowerCase().replace(/[^a-z0-9]/g, "_");
}
class EcrExampleStack extends TerraformStack {
constructor(scope: Construct, id: string) {
super(scope, id);
const regionInput = new TerraformVariable(this, "region", {
type: "string",
default: "eu-west-1",
});
const projectInput = new TerraformVariable(this, "project", {
type: "string",
default: id,
});
const environmentInput = new TerraformVariable(this, "environment", {
type: "string",
default: "test",
});
const region = regionInput.value;
const project = projectInput.value;
const environment = environmentInput.value;
// the tags applied to all the resources.
const defaultTags = {
project: project,
environment: environment,
};
// the source images.
// object value: source image uri.
// object key: target repository name suffix (this will be prefixed with
// project and a forward slash character).
const sourceImages = {
"example": "docker.io/ruilopes/example-docker-buildx-go:v1.10.0",
"hello-etcd": "ghcr.io/rgl/hello-etcd:0.0.1",
}
// see https://registry.terraform.io/providers/hashicorp/aws
// see https://github.com/hashicorp/terraform-provider-aws
new AwsProvider(this, "aws", {
region: region,
defaultTags: [{
tags: defaultTags,
}],
});
// create the image repositories and images.
for (const [name, sourceImage] of Object.entries(sourceImages)) {
// see https://registry.terraform.io/modules/terraform-aws-modules/ecr/aws
// see https://github.com/terraform-aws-modules/terraform-aws-ecr
const ecr = new Ecr(this, `ecr_repository_${toSnakeCaseIdentifier(name)}`, {
repositoryName: `${project}/${name}`,
repositoryType: "private",
repositoryForceDelete: true,
repositoryImageScanOnPush: false,
createLifecyclePolicy: false,
});
// see https://developer.hashicorp.com/terraform/language/resources/terraform-data
new DataResource(this, `ecr_image_${toSnakeCaseIdentifier(name)}`, {
triggersReplace: {
sourceImage: sourceImage,
targetImage: ecr.repositoryUrlOutput,
targetRegion: region,
},
provisioners: [
{
type: "local-exec",
when: "create",
environment: {
ECR_IMAGE_COMMAND: "copy",
ECR_IMAGE_SOURCE_IMAGE: sourceImage,
ECR_IMAGE_TARGET_IMAGE: ecr.repositoryUrlOutput,
ECR_IMAGE_TARGET_REGION: region,
},
interpreter: ["bash"],
command: path.resolve(__dirname, "ecr-image.sh"),
},
// NB I'm surprised that a TypeScript string is not automatically
// escaped when its translated to terraform code. I have very
// mix feelings about this behavior. It feels wrong.
// TODO review this code after the following issue is addressed.
// https://github.com/hashicorp/terraform-cdk/issues/3420
{
type: "local-exec",
when: "destroy",
environment: {
ECR_IMAGE_COMMAND: "delete",
ECR_IMAGE_SOURCE_IMAGE: "${self.triggers_replace.sourceImage}",
ECR_IMAGE_TARGET_IMAGE: "${self.triggers_replace.targetImage}",
ECR_IMAGE_TARGET_REGION: "${self.triggers_replace.targetRegion}",
},
interpreter: ["bash"],
command: path.resolve(__dirname, "ecr-image.sh"),
},
],
});
}
}
}
const app = new App();
new EcrExampleStack(app, "cdktf-typescript-aws-ecr-example");
app.synth();