-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathindex.ts
99 lines (86 loc) · 3.4 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
import { InlineProgramArgs, LocalWorkspace } from "@pulumi/pulumi/automation";
import { s3 } from "@pulumi/aws";
import { PolicyDocument } from "@pulumi/aws/iam";
const process = require('process');
const args = process.argv.slice(2);
let destroy = false;
if (args.length > 0 && args[0]) {
destroy = args[0] === "destroy";
}
const run = async () => {
// This is our pulumi program in "inline function" form
const pulumiProgram = async () => {
// Create a bucket and expose a website index document
const siteBucket = new s3.Bucket("s3-website-bucket", {
website: {
indexDocument: "index.html",
},
});
const indexContent = `<html><head>
<title>Hello S3</title><meta charset="UTF-8">
</head>
<body><p>Hello, world!</p><p>Made with ❤️ with <a href="https://pulumi.com">Pulumi</a></p>
</body></html>
`
// write our index.html into the site bucket
let object = new s3.BucketObject("index", {
bucket: siteBucket,
content: indexContent,
contentType: "text/html; charset=utf-8",
key: "index.html"
});
// Create an S3 Bucket Policy to allow public read of all objects in bucket
function publicReadPolicyForBucket(bucketName): PolicyDocument {
return {
Version: "2012-10-17",
Statement: [{
Effect: "Allow",
Principal: "*",
Action: [
"s3:GetObject"
],
Resource: [
`arn:aws:s3:::${bucketName}/*` // policy refers to bucket name explicitly
]
}]
};
}
// Set the access policy for the bucket so all objects are readable
let bucketPolicy = new s3.BucketPolicy("bucketPolicy", {
bucket: siteBucket.bucket, // refer to the bucket created earlier
policy: siteBucket.bucket.apply(publicReadPolicyForBucket) // use output property `siteBucket.bucket`
});
return {
websiteUrl: siteBucket.websiteEndpoint,
};
};
// Create our stack
const args: InlineProgramArgs = {
stackName: "dev",
projectName: "inlineNode",
program: pulumiProgram
};
// create (or select if one already exists) a stack that uses our inline program
const stack = await LocalWorkspace.createOrSelectStack(args);
console.info("successfully initialized stack");
console.info("installing plugins...");
await stack.workspace.installPlugin("aws", "v4.0.0");
console.info("plugins installed");
console.info("setting up config");
await stack.setConfig("aws:region", { value: "us-west-2" });
console.info("config set");
console.info("refreshing stack...");
await stack.refresh({ onOutput: console.info });
console.info("refresh complete");
if (destroy) {
console.info("destroying stack...");
await stack.destroy({ onOutput: console.info });
console.info("stack destroy complete");
process.exit(0);
}
console.info("updating stack...");
const upRes = await stack.up({ onOutput: console.info });
console.log(`update summary: \n${JSON.stringify(upRes.summary.resourceChanges, null, 4)}`);
console.log(`website url: ${upRes.outputs.websiteUrl.value}`);
};
run().catch(err => console.log(err));