forked from pulumi/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
129 lines (113 loc) · 4.77 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
// Copyright 2016-2019, Pulumi Corporation. All rights reserved.
import { CosmosClient } from "@azure/cosmos";
import * as azure from "@pulumi/azure";
import * as pulumi from "@pulumi/pulumi";
const config = new pulumi.Config();
// Read a list of target locations from the config file:
// Expecting a comma-separated list, e.g., "westus,eastus,westeurope"
const locations = config.require("locations").split(",");
// The first location is considered primary
const primaryLocation = locations[0];
const resourceGroup = new azure.core.ResourceGroup("UrlShorterner", {
location: primaryLocation,
});
// Cosmos DB with a single write region (primary location) and multiple read replicas
const account = new azure.cosmosdb.Account("UrlStore", {
resourceGroupName: resourceGroup.name,
location: primaryLocation,
geoLocations: locations.map((location, failoverPriority) => ({ location, failoverPriority })),
offerType: "Standard",
consistencyPolicy: {
consistencyLevel: "Session",
maxIntervalInSeconds: 300,
maxStalenessPrefix: 100000,
},
});
// Define a database under the Cosmos DB Account
const database = new azure.cosmosdb.SqlDatabase("Database", {
resourceGroupName: resourceGroup.name,
accountName: account.name,
});
// Define a SQL Collection under the Cosmos DB Database
const collection = new azure.cosmosdb.SqlContainer("Urls", {
resourceGroupName: resourceGroup.name,
accountName: account.name,
databaseName: database.name,
});
// Traffic Manager as a global HTTP endpoint
const profile = new azure.trafficmanager.Profile("UrlShortEndpoint", {
resourceGroupName: resourceGroup.name,
trafficRoutingMethod: "Performance",
dnsConfigs: [{
// Subdomain must be globally unique, so we default it with the full resource group name
relativeName: resourceGroup.name,
ttl: 60,
}],
monitorConfigs: [{
protocol: "HTTP",
port: 80,
path: "/api/ping",
}],
});
// Azure Function to accept new URL shortcodes and save to Cosmos DB
const fn = new azure.appservice.HttpEventSubscription("AddUrl", {
resourceGroup,
location: primaryLocation,
methods: ["POST"],
callbackFactory: () => {
const endpoint = account.endpoint.get();
const key = account.primaryMasterKey.get();
const client = new CosmosClient({ endpoint, key, connectionPolicy: { preferredLocations: [primaryLocation] } });
const container = client.database(database.name.get()).container(collection.name.get());
return async (_, request: azure.appservice.HttpRequest) => {
await container.items.create(request.body);
return { status: 200, body: "Short URL saved" };
};
},
});
export const addEndpoint = fn.url;
for (const location of locations) {
// URL redirection function - one per region
const fn = new azure.appservice.HttpEventSubscription(`GetUrl-${location}`, {
resourceGroup,
location,
route: "{key}",
callbackFactory: () => {
const endpoint = account.endpoint.get();
const key = account.primaryMasterKey.get();
const client = new CosmosClient({ endpoint, key, connectionPolicy: { preferredLocations: [location] } });
const container = client.database(database.name.get()).container(collection.name.get());
return async (_, request: azure.appservice.HttpRequest) => {
const key = request.params["key"];
if (key === "ping") {
// Handle traffic manager live pings
return { status: 200, body: "Ping ACK" };
}
try {
const response = await container.item(key, undefined).read();
return response.resource && response.resource.url
// HTTP redirect for known URLs
? { status: 301, headers: { location: response.resource.url }, body: "" }
// 404 for malformed documents
: { status: 404, body: "" };
} catch (e) {
// Cosmos SDK throws an error for non-existing documents
return { status: 404, body: "" };
}
};
},
});
const app = fn.functionApp;
// An endpoint per region for Traffic Manager, link to the corresponding Function App
const endpoint = new azure.trafficmanager.Endpoint(`tme${location}`, {
resourceGroupName: resourceGroup.name,
profileName: profile.name,
type: "azureEndpoints",
targetResourceId: app.id,
target: app.defaultHostname,
endpointLocation: app.location,
});
}
export const endpoint = profile.fqdn.apply(h => {
return `http://${h}/api/`;
});