Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Tagging and Policy enhancements #31

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.idea/
*.swp
node_modules
node_modules
typings
jsconfig.json
51 changes: 47 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,15 @@ npm install --save serverless-client-s3
"serverless-client-s3"
],
"custom" : {
"client": {
"bucketName": "bucket.name.for.the.client"
}
"client": {
"bucketName": "bucket.name.for.the.client"
}
}
```

* **Warning:** The plugin will overwrite any data you have in the bucket name you set above if it already exists.
* **Pro Tip:** To add staging and region functionality to your client, use Serverless Variables in the bucket name: `"bucket.name.for.the.client.${stage}.${region}"`


**Third**, Create a `client/dist` folder in the root directory of your Serverless project. This is where your distribution-ready website should live. It is recommended to have a `client/src` where you'll be developing your website, and a build script that outputs to `client/dist`. The plugin simply expects and uploads the entire `client/dist` folder to S3, configure the bucket to host the website, and make it publicly available.

Or just copy/run the following commands in the root directory of your Serverless project to get a quick sample website for deployment:
Expand All @@ -51,3 +50,47 @@ sls client deploy
```

**Fifth**, Have fun!

**Sixth**, Have more fun with custom redirection, tagging and policy options. These are optional.

```js
"plugins": [
"serverless-client-s3"
],
"custom" : {
"client": {
"bucketName": "bucket.name.for.the.client"
},
"Redirection" : {
"Index": "index.html",
"Path": "/MyAppPath"
},
"Tagging": {
"TagSet": [
{
"Key": "Region", "Value": "${region}"
}
]
},
"Policy": {
"Version": "2008-10-17",
"Id": "Policy1392681112290",
"Statement": [
{
"Sid": "Stmt1392681101677",
"Effect": "Allow",
"Principal": {
"AWS": "*"
},
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::${stage}.zzzzzzz.com",
"Condition": {
"IpAddress": {
"aws:SourceIp": "xx.yy.zzz.uuu"
}
}
}
]
}
}
```
28 changes: 27 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ module.exports = function(S) {

_this.bucketName = populatedProject.custom.client.bucketName;
_this.clientPath = path.join(_this.project.getRootPath(), 'client', 'dist');
_this.Tagging = populatedProject.custom.client.Tagging;
_this.Policy = populatedProject.custom.client.Policy;
if (populatedProject.custom.client.Redirection) {
_this.RedirectionIndex = populatedProject.custom.client.Redirection.Index;
_this.RedirectionPath = populatedProject.custom.client.Redirection.Path;
}

return BbPromise.resolve();
}
Expand Down Expand Up @@ -187,6 +193,17 @@ module.exports = function(S) {
};
return _this.aws.request('S3', 'putBucketWebsite', params, _this.evt.options.stage, _this.evt.options.region)
})
.then(function(){
if (!_this.Tagging) return BbPromise.resolve();

S.utils.sDebug(`Configuring TAGs for bucket ${_this.bucketName}...`);

let params = {
Bucket: _this.bucketName,
Tagging: _this.Tagging
};
return _this.aws.request('S3', 'putBucketTagging', params, _this.evt.options.stage, _this.evt.options.region)
})
.then(function(){

S.utils.sDebug(`Configuring policy for bucket ${_this.bucketName}...`);
Expand All @@ -206,6 +223,9 @@ module.exports = function(S) {
}
]
};
if (_this.Policy) {
policy = _this.Policy;
}

let params = {
Bucket: _this.bucketName,
Expand Down Expand Up @@ -253,7 +273,13 @@ module.exports = function(S) {
Body: fileBuffer,
ContentType: mime.lookup(filePath)
};


if (_this.RedirectionIndex && _this.RedirectionPath) {
let srcToken = '/dist/' + _this.RedirectionIndex;
if (filePath.indexOf(srcToken) > -1) {
params.WebsiteRedirectLocation = _this.RedirectionPath;
}
}
// TODO: remove browser caching
return _this.aws.request('S3', 'putObject', params, _this.evt.options.stage, _this.evt.options.region)
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "serverless-client-s3",
"version": "2.0.0",
"version": "2.0.5",
"engines": {
"node": ">=4.0"
},
Expand Down