forked from pulumi/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
83 lines (73 loc) · 2.58 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
// Copyright 2016-2019, Pulumi Corporation. All rights reserved.
import * as aws from "@pulumi/aws";
import * as pulumi from "@pulumi/pulumi";
import * as provisioners from "./provisioners";
import { getFileHash } from "./util";
// Get the config ready to go.
const config = new pulumi.Config();
// If keyName is provided, an existing KeyPair is used, else if publicKey is provided a new KeyPair
// derived from the publicKey is created.
let keyName: pulumi.Input<string> = config.get("keyName");
const publicKey = config.get("publicKey");
// The privateKey associated with the selected key must be provided (either directly or base64 encoded), along with an optional
// passphrase if needed.
const privateKey = config.requireSecret("privateKey").apply(key => {
if (key.startsWith("-----BEGIN RSA PRIVATE KEY-----")) {
return key;
} else {
return Buffer.from(key, "base64").toString("ascii");
}
});
const privateKeyPassphrase = config.getSecret("privateKeyPassphrase");
// Create a new security group that permits SSH and web access.
const secgrp = new aws.ec2.SecurityGroup("secgrp", {
description: "Foo",
ingress: [
{ protocol: "tcp", fromPort: 22, toPort: 22, cidrBlocks: ["0.0.0.0/0"] },
{ protocol: "tcp", fromPort: 80, toPort: 80, cidrBlocks: ["0.0.0.0/0"] },
],
});
// Get the AMI
const amiId = aws.getAmi({
owners: ["amazon"],
mostRecent: true,
filters: [{
name: "name",
values: ["amzn2-ami-hvm-2.0.????????-x86_64-gp2"],
}],
}, { async: true }).then(ami => ami.id);
// Create an EC2 server that we'll then provision stuff onto.
const size = "t2.micro";
if (!keyName) {
const key = new aws.ec2.KeyPair("key", { publicKey });
keyName = key.keyName;
}
const server = new aws.ec2.Instance("server", {
instanceType: size,
ami: amiId,
keyName: keyName,
vpcSecurityGroupIds: [ secgrp.id ],
});
const conn: provisioners.ConnectionArgs = {
host: server.publicIp,
username: "ec2-user",
privateKey,
privateKeyPassphrase,
};
const changeToken = getFileHash("myapp.conf");
// Copy a config file to our server.
const cpConfig = new provisioners.CopyFile("config", {
changeToken,
conn,
src: "myapp.conf",
dest: "myapp.conf",
}, { dependsOn: server });
// Execute a basic command on our server.
const catConfig = new provisioners.RemoteExec("cat-config", {
changeToken,
conn,
command: "cat myapp.conf",
}, { dependsOn: cpConfig });
export const publicIp = server.publicIp;
export const publicHostName = server.publicDns;
export const catConfigStdout = catConfig.result.stdout;