Skip to content

Commit 97a4edf

Browse files
authored
Add AWS Python S3 folder example (pulumi#266)
* Add aws-py-s3-folder example * Add example test * Add README.md
1 parent 2153a2e commit 97a4edf

File tree

9 files changed

+151
-0
lines changed

9 files changed

+151
-0
lines changed

aws-py-s3-folder/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.pyc
2+
venv/

aws-py-s3-folder/Pulumi.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
name: aws-py-s3-folder
2+
runtime: python
3+
description: A minimal AWS Python Pulumi program

aws-py-s3-folder/README.md

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
[![Deploy](https://get.pulumi.com/new/button.svg)](https://app.pulumi.com/new)
2+
3+
# Static Website Hosted on AWS S3
4+
5+
A static website that uses [S3's website support](https://docs.aws.amazon.com/AmazonS3/latest/dev/WebsiteHosting.html).
6+
For a detailed walkthrough of this example, see the tutorial [Static Website on AWS S3](https://pulumi.io/quickstart/aws-s3-website.html).
7+
8+
## Deploying and running the program
9+
10+
Note: some values in this example will be different from run to run. These values are indicated
11+
with `***`.
12+
13+
1. Create a new stack:
14+
15+
```bash
16+
$ pulumi stack init website-testing
17+
```
18+
19+
1. Set the AWS region:
20+
21+
```
22+
$ pulumi config set aws:region us-west-2
23+
```
24+
25+
1. Create a Python virtualenv, activate it, and install dependencies:
26+
27+
```
28+
$ virtualenv -p python3 venv
29+
$ source venv/bin/activate
30+
$ pip3 install -r requirements.txt
31+
```
32+
33+
1. Run `pulumi up` to preview and deploy changes. After the preview is shown you will be
34+
prompted if you want to continue or not.
35+
36+
```bash
37+
$ pulumi up
38+
Previewing update (dev):
39+
40+
Type Name Plan
41+
+ pulumi:pulumi:Stack aws-py-s3-folder-dev create
42+
+ ├─ aws:s3:Bucket s3-website-bucket create
43+
+ ├─ aws:s3:BucketObject index.html create
44+
+ ├─ aws:s3:BucketObject python.png create
45+
+ ├─ aws:s3:BucketObject favicon.png create
46+
+ └─ aws:s3:BucketPolicy bucket-policy create
47+
48+
Resources:
49+
+ 6 to create
50+
51+
Do you want to perform this update?
52+
> yes
53+
no
54+
details
55+
```
56+
57+
1. To see the resources that were created, run `pulumi stack output`:
58+
59+
```bash
60+
$ pulumi stack output
61+
Current stack outputs (2):
62+
OUTPUT VALUE
63+
bucket_name s3-website-bucket-***
64+
website_url ***.s3-website-us-west-2.amazonaws.com
65+
```
66+
67+
1. To see that the S3 objects exist, you can either use the AWS Console or the AWS CLI:
68+
69+
```bash
70+
$ aws s3 ls $(pulumi stack output bucket_name)
71+
2018-04-17 15:40:47 13731 favicon.png
72+
2018-04-17 15:40:48 249 index.html
73+
```
74+
75+
1. Open the site URL in a browser to see both the rendered HTML, the favicon, and Python splash image:
76+
77+
```bash
78+
$ pulumi stack output website_url
79+
***.s3-website-us-west-2.amazonaws.com
80+
```
81+
82+
1. To clean up resources, run `pulumi destroy` and answer the confirmation question at the prompt.

aws-py-s3-folder/__main__.py

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import json
2+
import mimetypes
3+
import os
4+
5+
from pulumi import export, FileAsset
6+
from pulumi_aws import s3
7+
8+
web_bucket = s3.Bucket('s3-website-bucket', website={
9+
"index_document": "index.html"
10+
})
11+
12+
content_dir = "www"
13+
for file in os.listdir(content_dir):
14+
filepath = os.path.join(content_dir, file)
15+
mime_type, _ = mimetypes.guess_type(filepath)
16+
obj = s3.BucketObject(file,
17+
bucket=web_bucket.id,
18+
source=FileAsset(filepath),
19+
content_type=mime_type)
20+
21+
def public_read_policy_for_bucket(bucket_name):
22+
return json.dumps({
23+
"Version": "2012-10-17",
24+
"Statement": [{
25+
"Effect": "Allow",
26+
"Principal": "*",
27+
"Action": [
28+
"s3:GetObject"
29+
],
30+
"Resource": [
31+
f"arn:aws:s3:::{bucket_name}/*",
32+
]
33+
}]
34+
})
35+
36+
bucket_name = web_bucket.id
37+
bucket_policy = s3.BucketPolicy("bucket-policy",
38+
bucket=bucket_name,
39+
policy=bucket_name.apply(public_read_policy_for_bucket))
40+
41+
# Export the name of the bucket
42+
export('bucket_name', web_bucket.id)
43+
export('website_url', web_bucket.website_endpoint)

aws-py-s3-folder/requirements.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pulumi>=0.16.4
2+
pulumi_aws>=0.16.2

aws-py-s3-folder/www/favicon.png

13.4 KB
Loading

aws-py-s3-folder/www/index.html

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<html>
2+
<head><meta charset="UTF-8">
3+
<title>Hello, Pulumi!</title></head>
4+
<body>
5+
<p>Hello, S3!</p>
6+
<p>Made with ❤️ with <a href="https://pulumi.com">Pulumi</a> and Python</p>
7+
<img src="python.png" />
8+
</body></html>

aws-py-s3-folder/www/python.png

81.6 KB
Loading

misc/test/examples_test.go

+11
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,17 @@ func TestExamples(t *testing.T) {
6969
})
7070
},
7171
}),
72+
base.With(integration.ProgramTestOptions{
73+
Dir: path.Join(cwd, "..", "..", "aws-py-s3-folder"),
74+
Config: map[string]string{
75+
"aws:region": awsRegion,
76+
},
77+
ExtraRuntimeValidation: func(t *testing.T, stack integration.RuntimeValidationStackInfo) {
78+
assertHTTPResult(t, "http://"+stack.Outputs["websiteUrl"].(string), func(body string) bool {
79+
return assert.Contains(t, body, "Hello, Pulumi!")
80+
})
81+
},
82+
}),
7283
base.With(integration.ProgramTestOptions{
7384
Dir: path.Join(cwd, "..", "..", "aws-js-s3-folder-component"),
7485
Config: map[string]string{

0 commit comments

Comments
 (0)