-
Notifications
You must be signed in to change notification settings - Fork 0
/
azure.ts
149 lines (131 loc) · 5.65 KB
/
azure.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
import { Input, Output, ComponentResourceOptions } from "@pulumi/pulumi";
import * as pulumi from "@pulumi/pulumi";
import * as azure from "@pulumi/azure";
import * as random from "@pulumi/random";
import * as types from "./types";
export interface AzureNetworkArgs extends types.NetworkArgs {
resourceGroupName: Input<string>;
};
export interface AzureWebServerArgs extends types.WebServerArgs {
resourceGroupName: Input<string>;
};
export class Network extends types.Network {
public readonly network: azure.network.VirtualNetwork;
public readonly publicSubnets: azure.network.Subnet[];
constructor(name: string, args: AzureNetworkArgs, opts?: ComponentResourceOptions) {
super("custom:AzureNetwork", name, args, opts);
// Create a network
this.network = new azure.network.VirtualNetwork(`${name}-vnet`, {
resourceGroupName: args.resourceGroupName,
addressSpaces: [args.vpcCidrBlock],
tags: args.tags,
}, { parent: this });
// Create subnets
this.publicSubnets = [];
for (let i = 0; i < (args.publicSubnetCidrBlocks?.length ?? 0); i++) {
const subnet = new azure.network.Subnet(`${name}-subnet-${i}`, {
resourceGroupName: args.resourceGroupName,
virtualNetworkName: this.network.name,
addressPrefixes: [args.publicSubnetCidrBlocks[i]],
}, { parent: this.network });
this.publicSubnets.push(subnet);
}
this.registerOutputs({});
}
public getNetworkId(): Output<string> {
return this.network.id;
}
public getPublicSubnetIds(): Output<string>[] {
return this.publicSubnets.map(it => it.id);
}
}
export class WebServer extends types.WebServer {
public readonly publicIp: azure.network.PublicIp;
public readonly virtualMachine: azure.compute.VirtualMachine;
constructor(name: string, args: AzureWebServerArgs, opts?: ComponentResourceOptions) {
super("custom:AzureWebServer", name, args, opts);
// Create a Public IP and security group resources
const webSg = new azure.network.NetworkSecurityGroup(`${name}-nsg`, {
resourceGroupName: args.resourceGroupName,
tags: args.tags,
});
const webSgHttp = new azure.network.NetworkSecurityRule(`${name}-http`, {
resourceGroupName: args.resourceGroupName,
networkSecurityGroupName: webSg.name,
priority: 100,
direction: "Inbound",
access: "Allow",
protocol: "Tcp",
sourceAddressPrefix: "*",
sourcePortRange: "*",
destinationAddressPrefix: "*",
destinationPortRange: "80",
}, { parent: webSg });
this.publicIp = new azure.network.PublicIp(`${name}-ip`, {
resourceGroupName: args.resourceGroupName,
allocationMethod: "Dynamic",
tags: args.tags,
});
const networkInterface = new azure.network.NetworkInterface(`${name}-nic`, {
resourceGroupName: args.resourceGroupName,
ipConfigurations: [{
name: `${name}-nic-ipcfg`,
subnetId: args.subnetId,
publicIpAddressId: this.publicIp.id,
privateIpAddressAllocation: "Dynamic",
}],
tags: args.tags,
}, { parent: this.publicIp });
const networkInterfaceSGAssociation = new azure.network.NetworkInterfaceSecurityGroupAssociation(`${name}-nic-nsg`, {
networkInterfaceId: networkInterface.id,
networkSecurityGroupId: webSg.id,
}, { parent: networkInterface });
const randomPassword = new random.RandomPassword("pwd", {
length: 20,
special: true,
}, { parent: this });
const userName = "pulumi-admin";
const vmName = `${name}-vm`;
this.virtualMachine = new azure.compute.VirtualMachine(vmName, {
resourceGroupName: args.resourceGroupName,
networkInterfaceIds: [networkInterface.id], // reference the networkInterface created above
vmSize: "Standard_A0",
deleteDataDisksOnTermination: true,
deleteOsDiskOnTermination: true,
osProfile: {
computerName: "hostname",
adminUsername: userName, // reference the userName variable
adminPassword: randomPassword.result,
},
osProfileLinuxConfig: {
disablePasswordAuthentication: false,
},
storageOsDisk: {
createOption: "FromImage",
name: vmName,
},
storageImageReference: {
publisher: "canonical",
offer: "UbuntuServer",
sku: "16.04-LTS",
version: "latest",
},
tags: { ...args.tags, Name: vmName, },
}, { parent: networkInterface });
this.registerOutputs();
}
public getInstanceId(): Output<string> {
return this.virtualMachine.id;
}
public getInstanceAddress(): pulumi.Output<string> {
// The public IP address is not allocated until the VM is running, so wait for that
// resource to create, and then lookup the IP address again to report its public IP.
const ready = pulumi.all({ _: this.virtualMachine.id, name: this.publicIp.name, resourceGroupName: this.publicIp.resourceGroupName });
return ready.apply(d =>
azure.network.getPublicIP({
name: d.name,
resourceGroupName: d.resourceGroupName,
}, { async: true }).then(ip => ip.ipAddress)
);
}
}