forked from pulumi/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add component version of S3 Website example
- Loading branch information
1 parent
267c4b1
commit 3fa6272
Showing
8 changed files
with
164 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters