Skip to content

Commit

Permalink
Add component version of S3 Website example
Browse files Browse the repository at this point in the history
  • Loading branch information
lindydonna committed Apr 20, 2018
1 parent 267c4b1 commit 3fa6272
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 1 deletion.
3 changes: 3 additions & 0 deletions aws-js-s3-folder-component/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name: aws-js-s3-folder-component
runtime: nodejs
description: A static website hosted on AWS S3
74 changes: 74 additions & 0 deletions aws-js-s3-folder-component/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# Static Website Hosted on AWS S3

The component version of [aws-js-s3-folder](../aws-js-s3-folder). For a detailed walkthrough of this example, see [Part 2 of the Pulumi quickstart](https://docs.pulumi.com/quickstart/part2.html).

## Deploying and running the program

1. Initialize a Pulumi repository with `pulumi init`, using your GitHub username. (Note: this step will be removed in the future.)

```bash
$ pulumi init --owner githubUsername
```

1. Create a new stack:

```bash
$ pulumi stack init website-testing
```

1. Set the AWS region:

```
$ pulumi config set aws:region us-west-2
```

1. Restore NPM modules via `npm install`.

1. Run `pulumi preview` to see what AWS resources will be created.

1. Now, provision resources via `pulumi update`:

```bash
$ pulumi update
Updating stack 's3foldercomponent-testing' in the Pulumi Cloud ☁️
Performing changes:

pulumi:Stack("aws-js-s3-folder-component-s3foldercomponent-testing"): Completed
examples:S3Folder("pulumi-static-site"): + Created
aws:Bucket("pulumi-static-site"): + Created
aws:BucketPolicy("bucketPolicy"): + Created
aws:BucketObject("favicon.png"): + Created
aws:BucketObject("index.html"): + Created
info: 6 changes performed:
+ 6 resources created
Update duration: 6.952010989s

Permalink: https://pulumi.com/lindydonna/examples/aws-js-s3-folder-component/s3foldercomponent-testing/updates/3
```

1. To see the resources that were created, run `pulumi stack output`:

```bash
$ pulumi stack output
Current stack outputs (2):
OUTPUT VALUE
bucketName s3-website-bucket-e7c0411
websiteUrl s3-website-bucket-e7c0411.s3-website-us-west-2.amazonaws.com
```

1. To see that the S3 objects exist, you can either use the AWS Console or the AWS CLI:

```bash
$ aws s3 ls $(pulumi stack output bucketName)
2018-04-17 15:40:47 13731 favicon.png
2018-04-17 15:40:48 249 index.html
```

1. Open the site URL in a browser to see both the rendered HTML and the favicon:

```bash
$ pulumi stack output websiteUrl
s3-website-bucket-8533d8b.s3-website-us-west-2.amazonaws.com
```


8 changes: 8 additions & 0 deletions aws-js-s3-folder-component/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
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;
9 changes: 9 additions & 0 deletions aws-js-s3-folder-component/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "aws-js-s3-folder-component",
"main": "index.js",
"dependencies": {
"@pulumi/aws": "^0.11.0",
"@pulumi/pulumi": "^0.11.0",
"mime": "^2.2.2"
}
}
63 changes: 63 additions & 0 deletions aws-js-s3-folder-component/s3folder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
"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;
Binary file added aws-js-s3-folder-component/www/favicon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions aws-js-s3-folder-component/www/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<html><head>
<title>Hello S3</title><meta charset="UTF-8">
<link rel="shortcut icon" href="/favicon.png" type="image/png">
</head>
<body><p>Hello, world!</p><p>Made with ❤️ with <a href="https://pulumi.com">Pulumi</a></p>
</body></html>
2 changes: 1 addition & 1 deletion aws-js-s3-folder/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 3fa6272

Please sign in to comment.