-
Notifications
You must be signed in to change notification settings - Fork 880
/
index.ts
140 lines (116 loc) · 4.22 KB
/
index.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright 2016-2019, Pulumi Corporation. All rights reserved.
import * as aws from "@pulumi/aws";
import * as awsx from "@pulumi/awsx";
import * as pulumi from "@pulumi/pulumi";
const config = new pulumi.Config("airflow");
const dbPassword = config.requireSecret("dbPassword");
const vpc = awsx.classic.ec2.Vpc.getDefault();
// Create a basic cluster and autoscaling group
const cluster = new awsx.classic.ecs.Cluster("airflow", { vpc });
const autoScalingGroup = cluster.createAutoScalingGroup("airflow", {
subnetIds: vpc.publicSubnetIds,
templateParameters: {
minSize: 4,
},
launchConfigurationArgs: {
instanceType: "t2.xlarge",
},
});
const securityGroupIds = cluster.securityGroups.map(g => g.id);
const dbSubnets = new aws.rds.SubnetGroup("dbsubnets", {
subnetIds: vpc.publicSubnetIds,
});
const db = new aws.rds.Instance("postgresdb", {
engine: "postgres",
instanceClass: "db.t3.micro",
allocatedStorage: 20,
dbSubnetGroupName: dbSubnets.id,
vpcSecurityGroupIds: securityGroupIds,
name: "airflow",
username: "airflow",
password: dbPassword,
skipFinalSnapshot: true,
});
const cacheSubnets = new aws.elasticache.SubnetGroup("cachesubnets", {
subnetIds: vpc.publicSubnetIds,
});
const cacheCluster = new aws.elasticache.Cluster("cachecluster", {
engine: "redis",
nodeType: "cache.t2.micro",
numCacheNodes: 1,
subnetGroupName: cacheSubnets.id,
securityGroupIds: securityGroupIds,
});
const hosts = pulumi.all([db.endpoint.apply(e => e.split(":")[0]), cacheCluster.cacheNodes[0].address]);
const environment = hosts.apply(([postgresHost, redisHost]) => [
{ name: "POSTGRES_HOST", value: postgresHost },
{ name: "POSTGRES_PASSWORD", value: dbPassword },
{ name: "REDIS_HOST", value: redisHost },
{ name: "EXECUTOR", value: "Celery" },
]);
const airflowControllerListener = new awsx.classic.lb.ApplicationListener("airflowcontroller", {
external: true,
port: 8080,
protocol: "HTTP",
});
const repo = new awsx.ecr.Repository("repo", {
forceDelete: true,
});
const airflowController = new awsx.classic.ecs.EC2Service("airflowcontroller", {
cluster,
desiredCount: 1,
taskDefinitionArgs: {
containers: {
"webserver": {
image: new awsx.ecr.Image("webserver", { repositoryUrl: repo.url, context: "./airflow-container" }).imageUri,
portMappings: [airflowControllerListener],
environment: environment,
command: [ "webserver" ],
memory: 128,
},
"scheduler": {
image: new awsx.ecr.Image("scheduler", { repositoryUrl: repo.url, context: "./airflow-container" }).imageUri,
environment: environment,
command: [ "scheduler" ],
memory: 128,
},
},
},
});
const airflowerListener = new awsx.classic.lb.ApplicationListener("airflower", {
port: 5555,
external: true,
protocol: "HTTP",
});
const airflower = new awsx.classic.ecs.EC2Service("airflower", {
cluster,
taskDefinitionArgs: {
containers: {
// If the container is named "flower", we create environment variables that start
// with `FLOWER_` and Flower tries and fails to parse them as configuration.
"notflower": {
image: new awsx.ecr.Image("notflower", { repositoryUrl: repo.url, context: "./airflow-container" }).imageUri,
portMappings: [airflowerListener],
environment: environment,
command: [ "flower" ],
memory: 128,
},
},
},
});
const airflowWorkers = new awsx.classic.ecs.EC2Service("airflowworkers", {
cluster,
desiredCount: 3,
taskDefinitionArgs: {
containers: {
"worker": {
image: new awsx.ecr.Image("worker", { repositoryUrl: repo.url, context: "./airflow-container" }).imageUri,
environment: environment,
command: [ "worker" ],
memory: 1024,
},
},
},
});
export let airflowEndpoint = airflowControllerListener.endpoint.hostname;
export let flowerEndpoint = airflowerListener.endpoint.hostname;