forked from pulumi/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
117 lines (113 loc) · 4.73 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
import * as aws from "@pulumi/aws";
import * as config from "./config";
import { getLinuxAmi } from "pawsami";
import { createUserData, renderConfigFile } from "pcloudinit";
const webSg = new aws.ec2.SecurityGroup("webServerSecurityGroup", {
description: "Enable HTTP and SSH access",
egress: [
{ protocol: "-1", fromPort: 0, toPort: 0, cidrBlocks: [ "0.0.0.0/0" ] },
],
ingress: [
{ protocol: "tcp", fromPort: 22, toPort: 22, cidrBlocks: [ config.sshLocation ] },
{ protocol: "tcp", fromPort: 80, toPort: 80, cidrBlocks: [ "0.0.0.0/0" ] },
],
});
const webServer = new aws.ec2.Instance("webServer", {
ami: getLinuxAmi(<any>config.instanceType),
instanceType: config.instanceType,
securityGroups: [ webSg.name ],
keyName: config.keyName,
userData: createUserData(
[ "install_ruby_2_3_1", "install_mysql", "configure_mysql", "install_application" ],
{
"install_ruby_2_3_1": {
files: {
"/tmp/install_ruby": {
content: renderConfigFile("./files/install_ruby", config),
mode: "000500",
owner: "root",
group: "root",
},
},
commands: {
"01_install_ruby": {
command: "/tmp/install_ruby > /var/log/install_ruby.log",
},
},
},
"install_mysql": {
packages: {
yum: [ "mysql", "mysql-server", "mysql-devel", "mysql-libs" ],
},
files: {
"/tmp/setup.mysql": {
content: renderConfigFile("./files/setup.mysql", config),
mode: "000400",
owner: "root",
group: "root",
},
},
services: {
"sysvinit": {
"mysqld": { enabled: true, ensureRunning: true },
},
},
},
"configure_mysql": {
commands: {
"01_set_mysql_root_password": {
command: `mysqladmin -u root password '${config.dbRootPassword}'`,
test: `$(mysql ${config.dbName} -u root --password='${config.dbRootPassword}' >/dev/null 2>&1 </dev/null); (( $? != 0 ))`,
},
"02_create_database": {
command: `mysql -u root --password='${config.dbRootPassword}' < /tmp/setup.mysql`,
test: `$(mysql ${config.dbName} -u root --password='${config.dbRootPassword}' >/dev/null 2>&1 </dev/null); (( $? != 0 ))`,
},
"03_cleanup": {
command: "rm /tmp/setup.mysql",
},
},
},
"install_application": {
files: {
"/tmp/database.yml": {
content: renderConfigFile("./files/database.yml", config),
mode: "000400",
owner: "root",
group: "root",
},
"/tmp/install_application": {
content: renderConfigFile("./files/install_application", config),
mode: "000500",
owner: "root",
group: "root",
},
"/home/ec2-user/start_application": {
content: renderConfigFile("./files/start_application", config),
mode: "000500",
owner: "root",
group: "root",
},
},
commands: {
"01_install_application": {
command: "/tmp/install_application > /var/log/install_application.log",
},
"02_configure_reboot": {
command: "echo /home/ec2-user/start_application >> /etc/rc.local",
},
"03_start_application": {
command: "/home/ec2-user/start_application > var/log/start_application.log",
},
"04_cleanup": {
command: "rm /tmp/install_application",
},
},
},
},
),
});
// Export the VM IP in case we want to SSH.
export let vmIP = webServer.publicIp;
// Export the URL for our newly created Rails application.
export let websiteURL = webServer.publicDns.apply(url => `http:https://${url}/notes`);