Skip to content

Commit

Permalink
Create an example for go s3 folder component. (pulumi#660)
Browse files Browse the repository at this point in the history
  • Loading branch information
derekbassett committed Apr 21, 2020
1 parent 50026f1 commit 9448dc1
Show file tree
Hide file tree
Showing 10 changed files with 520 additions and 0 deletions.
22 changes: 22 additions & 0 deletions aws-go-s3-folder-component/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
*.swp
/vendor/
**/node_modules/
**/bin
**/.vscode/
**/.vs/
**/.ionide/
coverage.cov
*.coverprofile

/.idea/
*.iml

# VSCode creates this binary when running tests in the debugger
**/debug.test

# Go tests run "in tree" and this folder will linger if they fail (the integration test framework cleans
# it up when they pass.)
**/command-output/

# By default, we don't check in yarn.lock files
**/yarn.lock
8 changes: 8 additions & 0 deletions aws-go-s3-folder-component/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name: aws-go-s3-folder-component
runtime: go
description: A static website hosted on AWS S3
template:
config:
aws:region:
description: The AWS region to deploy into
default: us-west-2
87 changes: 87 additions & 0 deletions aws-go-s3-folder-component/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
[![Deploy](https://get.pulumi.com/new/button.svg)](https://app.pulumi.com/new)

# Static Website on Amazon S3

The component version of [aws-go-s3-folder](../aws-go-s3-folder/). For a detailed walkthrough of this example, see [Tutorial: Pulumi Components](https://www.pulumi.com/docs/tutorials/aws/s3-folder-component/).

## Deploying and running the program

Note: some values in this example will be different from run to run. These values are indicated
with `***`.

1. Create a new stack:

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

1. Install the Pulumi AWS plugin:

```
$ pulumi plugin install resource aws 1.28.0
```

1. Set the AWS region:

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

1. Run `pulumi up` to preview and deploy changes. After showing the preview you will be
prompted if you want to continue or not.

```bash
$ pulumi up
Previewing stack 'website-component-testing'
Previewing changes:
...

Updating stack 'website-component-testing'
Performing changes:

Type Name Status
+ pulumi:pulumi:Stack aws-go-s3-folder-component-website-component-testing created
+ └─ pulumi:example:S3Folder pulumi-static-site created
+ ├─ aws:s3:Bucket pulumi-static-site created
+ ├─ aws:s3:BucketPolicy bucketPolicy created
+ ├─ aws:s3:BucketObject index.html created
+ └─ aws:s3:BucketObject favicon.png created

Outputs:
bucketName: "pulumi-static-site-***"
websiteUrl: "pulumi-static-site-***.s3-website-us-west-2.amazonaws.com"

Resources:
+ 6 created

Duration: 14s

Permalink: ***
```

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

```bash
$ pulumi stack output
Current stack outputs (2):
OUTPUT VALUE
bucketName pulumi-static-site-***
websiteUrl pulumi-static-site-***.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)
2020-04-20 22:52:15 13731 favicon.png
2020-04-20 22:52:15 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
pulumi-static-site-***.s3-website-us-west-2.amazonaws.com
```

1. To clean up resources, run `pulumi destroy` and answer the confirmation question at the prompt.
8 changes: 8 additions & 0 deletions aws-go-s3-folder-component/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module github.com/pulumi/examples/aws-go-s3-folder

go 1.13

require (
github.com/pulumi/pulumi-aws/sdk v1.28.0
github.com/pulumi/pulumi/sdk v1.13.1
)
282 changes: 282 additions & 0 deletions aws-go-s3-folder-component/go.sum

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions aws-go-s3-folder-component/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

import (
"github.com/pulumi/pulumi/sdk/go/pulumi"
)

func main() {
pulumi.Run(func(ctx *pulumi.Context) error {
// Create a bucket and expose a website index document
f, err := NewS3Folder(ctx, "pulumi-static-site", "./www", &FolderArgs{})
if err != nil {
return err
}
ctx.Export("bucketName", f.bucketName)
ctx.Export("websiteUrl", f.websiteUrl)
return nil
})
}
89 changes: 89 additions & 0 deletions aws-go-s3-folder-component/s3folder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package main

import (
"github.com/pulumi/pulumi-aws/sdk/go/aws/s3"
"github.com/pulumi/pulumi/sdk/go/pulumi"
"io/ioutil"
"mime"
"path"
"path/filepath"
"reflect"
)

type Folder struct {
pulumi.ResourceState

bucketName pulumi.IDOutput `pulumi:"bucketName"`
websiteUrl pulumi.StringOutput `pulumi:"websiteUrl"`
}

func NewS3Folder(ctx *pulumi.Context, bucketName string, siteDir string, args *FolderArgs, opts ...pulumi.ResourceOption) (*Folder, error) {
var resource Folder
// Stack exports
err := ctx.RegisterComponentResource("pulumi:example:S3Folder", bucketName, &resource, opts...)
if err != nil {
return nil, err
}
// Create a bucket and expose a website index document
siteBucket, err := s3.NewBucket(ctx, bucketName, &s3.BucketArgs{
Website: s3.BucketWebsiteArgs{
IndexDocument: pulumi.String("index.html"),
},
}, pulumi.Parent(&resource))
if err != nil {
return nil, err
}

// For each file in the directory, create an S3 object stored in `siteBucket`
files, err := ioutil.ReadDir(siteDir)
if err != nil {
return nil, err
}
for _, item := range files {
name := item.Name()
s3.NewBucketObject(ctx, name, &s3.BucketObjectArgs{
Bucket: siteBucket.ID(), // reference to the s3.Bucket object
Source: pulumi.NewFileAsset(filepath.Join(siteDir, name)), // use FileAsset to point to a file
ContentType: pulumi.String(mime.TypeByExtension(path.Ext(name))), // set the MIME type of the file
}, pulumi.Parent(&resource))
}

// Set the access policy for the bucket so all objects are readable.
if _, err := s3.NewBucketPolicy(ctx, "bucketPolicy", &s3.BucketPolicyArgs{
Bucket: siteBucket.ID(), // refer to the bucket created earlier
Policy: pulumi.Any(map[string]interface{}{
"Version": "2012-10-17",
"Statement": []map[string]interface{}{
{
"Effect": "Allow",
"Principal": "*",
"Action": []interface{}{
"s3:GetObject",
},
"Resource": []interface{}{
pulumi.Sprintf("arn:aws:s3:::%s/*", siteBucket.ID()), // policy refers to bucket name explicitly
},
},
},
}),
}, pulumi.Parent(&resource)); err != nil {
return nil, err
}
resource.bucketName = siteBucket.ID()
resource.websiteUrl = siteBucket.WebsiteEndpoint
ctx.RegisterResourceOutputs(&resource, pulumi.Map{
"bucketName": siteBucket.ID(),
"websiteUrl": siteBucket.WebsiteEndpoint,
})
return &resource, nil
}

type folderArgs struct {
}

type FolderArgs struct {
}

func (FolderArgs) ElementType() reflect.Type {
return reflect.TypeOf((*folderArgs)(nil)).Elem()
}
Binary file added aws-go-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-go-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>

0 comments on commit 9448dc1

Please sign in to comment.