Add component version of S3 Website example
ADD
aws-js-s3-folder-component/Pulumi.yaml
+name: aws-js-s3-folder-component
+runtime: nodejs
+description: A static website hosted on AWS S3

ADD
aws-js-s3-folder-component/index.js
+const s3folder = require("./s3folder.js");
+
+// Create an instance of the S3Folder component
+let folder = new s3folder.S3Folder("pulumi-static-site", "./www");
+
+// Export `folder` output properties as stack outputs
+exports.bucketName = folder.bucketName;
+exports.websiteUrl = folder.websiteUrl;

ADD
aws-js-s3-folder-component/package.json
+{
+ "name": "aws-js-s3-folder-component",
+ "main": "index.js",
+ "dependencies": {
+ "@pulumi/aws": "^0.11.0",
+ "@pulumi/pulumi": "^0.11.0",
+ "mime": "^2.2.2"
+ }
+}

ADD
aws-js-s3-folder-component/s3folder.js
+"use strict";
+
+const aws = require("@pulumi/aws");
+const pulumi = require("@pulumi/pulumi");
+const mime = require("mime");
+
+// Define a component for serving a static website on S3
+class S3Folder extends pulumi.ComponentResource {
+
+ constructor(bucketName, path, opts) {
+ super("examples:S3Folder", bucketName, {}, opts); // Register this component with name examples:S3Folder
+
+ // Create a bucket and expose a website index document
+ let siteBucket = new aws.s3.Bucket(bucketName, {
+ websites: [{
+ indexDocument: "index.html",
+ }],
+ }, { parent: this }); // specify resource parent
+
+ // For each file in the directory, create an S3 object stored in `siteBucket`
+ for (let item of require("fs").readdirSync(path)) {
+ let filePath = require("path").join(path, item);
+ let object = new aws.s3.BucketObject(item, {
+ bucket: siteBucket, // reference the s3.Bucket object
+ source: new pulumi.asset.FileAsset(filePath), // use FileAsset to point to a file
+ contentType: mime.getType(filePath) || undefined, // set the MIME type of the file
+ }, { parent: this }); // specify resource parent
+ }
+
+ // Set the access policy for the bucket so all objects are readable
+ let bucketPolicy = new aws.s3.BucketPolicy("bucketPolicy", {
+ bucket: siteBucket.bucket,
+ policy: siteBucket.bucket.apply(this.publicReadPolicyForBucket),
+ }, { parent: this }); // specify resource parent
+
+ this.bucketName = siteBucket.bucket,
+ this.websiteUrl = siteBucket.websiteEndpoint,
+
+ // Register output properties for this component
+ this.registerOutputs({
+ bucketName: this.bucketName,
+ websiteUrl: this.websiteEndpoint,
+ });
+ }
+
+ publicReadPolicyForBucket(bucketName) {
+ return JSON.stringify({
+ Version: "2012-10-17",
+ Statement: [{
+ Effect: "Allow",
+ Principal: "*",
+ Action: [
+ "s3:GetObject"
+ ],
+ Resource: [
+ `arn:aws:s3:::${bucketName}/*` // policy refers to bucket name explicitly
+ ]
+ }]
+ });
+ }
+}
+
+module.exports.S3Folder = S3Folder;

ADD
aws-js-s3-folder-component/www/favicon.png
aws-js-s3-folder-component/www/favicon.png
Binary files /dev/null and b/aws-js-s3-folder-component/www/favicon.png differ

MODIFY
aws-js-s3-folder/README.md
aws-js-s3-folder/README.md
@@ -13,7 +13,7 @@ A static website that uses [S3's website support](https://docs.aws.amazon.com/Am
1. Create a new stack:
```bash
- $ pulumi stack init website-testing`
+ $ pulumi stack init website-testing
```
1. Set the AWS region:

Add component version of S3 Website example