forked from pulumi/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
174 lines (154 loc) · 5.71 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright 2016-2020, Pulumi Corporation. All rights reserved.
import * as aws from "@pulumi/aws";
import * as awsx from "@pulumi/awsx";
import * as postgresql from "@pulumi/postgresql";
import * as pulumi from "@pulumi/pulumi";
import { table } from "console";
import { Schema } from "./PostgreSqlDynamicProvider";
const config = new pulumi.Config();
const sqlAdminName = config.require("sql-admin-name");
const sqlAdminPassword = config.requireSecret("sql-admin-password");
const sqlUserName = config.require("sql-user-name");
const sqlUserPassword = config.requireSecret("sql-user-password");
const availabilityZone = aws.config.region;
const appVpc = new aws.ec2.Vpc("app-vpc", {
cidrBlock: "172.31.0.0/16",
enableDnsHostnames: true,
});
const appGateway = new aws.ec2.InternetGateway("app-gateway", {
vpcId: appVpc.id,
});
const appRoutetable = new aws.ec2.RouteTable("app-routetable", {
routes: [
{
cidrBlock: "0.0.0.0/0",
gatewayId: appGateway.id,
},
],
vpcId: appVpc.id,
});
const appRoutetableAssociation = new aws.ec2.MainRouteTableAssociation("app-routetable-association", {
routeTableId: appRoutetable.id,
vpcId: appVpc.id,
});
const rdsSecurityGroup = new aws.ec2.SecurityGroup("rds-security-group", {
vpcId: appVpc.id,
description: "Enables HTTP access",
ingress: [{
protocol: "tcp",
fromPort: 0,
toPort: 65535,
cidrBlocks: ["0.0.0.0/0"],
}],
egress: [{
protocol: "-1",
fromPort: 0,
toPort: 0,
cidrBlocks: ["0.0.0.0/0"],
}],
});
const firstRdsSubnet = new aws.ec2.Subnet("first-rds-subnet", {
vpcId: appVpc.id,
cidrBlock: "172.31.0.0/20",
availabilityZone: availabilityZone + "a",
});
const secondRdsSubnet = new aws.ec2.Subnet("second-rds-subnet", {
vpcId: appVpc.id,
cidrBlock: "172.31.128.0/20",
availabilityZone: availabilityZone + "b",
});
const rdsSubnetGroup = new aws.rds.SubnetGroup("rds-subnet-group", {
subnetIds: [firstRdsSubnet.id, secondRdsSubnet.id],
});
const postgresqlRdsServer = new aws.rds.Instance("postgresql-rds-server", {
engine: "postgres",
username: sqlAdminName,
password: sqlAdminPassword,
instanceClass: "db.t2.micro",
allocatedStorage: 20,
skipFinalSnapshot: true,
publiclyAccessible: true,
port: 2000,
dbSubnetGroupName: rdsSubnetGroup.name,
vpcSecurityGroupIds: [rdsSecurityGroup.id],
});
const postgresqlProvider = new postgresql.Provider("postgresql-provider", {
host: postgresqlRdsServer.address,
port: postgresqlRdsServer.port,
username: sqlAdminName,
password: sqlAdminPassword,
superuser: false,
});
const postgresDatabase = new postgresql.Database("postgresql-database", {
name: "votes"}, {
provider: postgresqlProvider,
});
const postgresUser = new postgresql.Role("postgres-standard-role", {
name: sqlUserName,
password: sqlUserPassword,
superuser: false,
login: true,
connectionLimit: -1}, {
provider: postgresqlProvider,
});
// The database schema and initial data to be deployed to the database
const creationScript = `
CREATE SCHEMA voting_app;
CREATE TABLE voting_app.choice(
choice_id SERIAL PRIMARY KEY,
text VARCHAR(255) NOT NULL,
vote_count INTEGER NOT NULL
);
GRANT USAGE ON SCHEMA voting_app TO ${sqlUserName};
GRANT SELECT, UPDATE ON ALL TABLES IN SCHEMA voting_app TO ${sqlUserName};
INSERT INTO voting_app.choice (text, vote_count) VALUES('Tabs', 0);
INSERT INTO voting_app.choice (text, vote_count) VALUES('Spaces', 0);
`;
// The SQL commands the database performs when deleting the schema
const deletionScript = "DROP SCHEMA IF EXISTS voting_app CASCADE";
// Creating our dynamic resource to deploy the schema during `pulumi up`. The arguments
// are passed in as a SchemaInputs object
const postgresqlVotesTable = new Schema("postgresql-votes-schema", {
creatorName: sqlAdminName,
creatorPassword: sqlAdminPassword,
serverAddress: postgresqlRdsServer.address,
databaseName: postgresDatabase.name,
creationScript: creationScript,
deletionScript: deletionScript,
postgresUserName: postgresUser.name,
});
const serversideListener = new awsx.elasticloadbalancingv2.NetworkListener("server-side-listener", { port: 5000 });
const serversideService = new awsx.ecs.FargateService("server-side-service", {
taskDefinitionArgs: {
containers: {
serversideService: {
image: awsx.ecs.Image.fromPath("server-side-service", "./serverside"),
memory: 512,
portMappings: [serversideListener],
environment: [
{ name: "USER_NAME", value: sqlUserName },
{ name: "USER_PASSWORD", value: sqlUserPassword },
{ name: "RDS_ADDRESS", value: postgresqlRdsServer.address },
{ name: "RDS_PORT", value: String(2000) },
{ name: "DATABASE_NAME", value: postgresDatabase.name },
],
},
},
},
});
const clientsideListener = new awsx.elasticloadbalancingv2.NetworkListener("client-side-listener", { port: 3000 });
const clientsideService = new awsx.ecs.FargateService("client-side-service", {
taskDefinitionArgs: {
containers: {
clientsideService: {
image: awsx.ecs.Image.fromPath("client-side-service", "./clientside"),
memory: 512,
portMappings: [clientsideListener],
environment: [
{ name: "SERVER_HOSTNAME", value: serversideListener.endpoint.hostname },
],
},
},
},
});
export let URL = clientsideListener.endpoint.hostname;