├── .gitignore ├── .eslintrc.yml ├── package.json ├── LICENSE ├── README.md └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Dependency directories 7 | node_modules 8 | -------------------------------------------------------------------------------- /.eslintrc.yml: -------------------------------------------------------------------------------- 1 | env: 2 | node: true 3 | es6: true 4 | extends: 'eslint:recommended' 5 | parserOptions: 6 | sourceType: module 7 | rules: 8 | indent: 9 | - error 10 | - 2 11 | linebreak-style: 12 | - error 13 | - unix 14 | quotes: 15 | - error 16 | - single 17 | semi: 18 | - error 19 | - always 20 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "serverless-s3-deploy", 3 | "version": "0.10.1", 4 | "description": "S3 Asset copy for serverless", 5 | "main": "index.js", 6 | "scripts": { 7 | "lint": "eslint index.js", 8 | "test": "echo \"Error: no test specified\" && exit 1" 9 | }, 10 | "author": "Curtis Maloney", 11 | "contributors": [ 12 | "Dan Rumney " 13 | ], 14 | "license": "MIT", 15 | "repository": { 16 | "type": "git", 17 | "url": "git+https://github.com/funkybob/serverless-s3-deploy.git" 18 | }, 19 | "dependencies": { 20 | "bluebird": "^3.7.2", 21 | "glob-all": "^3.3.0", 22 | "mime-types": "^2.1.35" 23 | }, 24 | "devDependencies": { 25 | "eslint": "^8.35.0", 26 | "eslint-config-standard": "^17.0.0", 27 | "eslint-plugin-import": "^2.26.0", 28 | "eslint-plugin-node": "^11.1.0", 29 | "eslint-plugin-promise": "^6.1.1", 30 | "eslint-plugin-standard": "^5.0.0" 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Curtis Maloney 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # serverless-s3-deploy 2 | 3 | Plugin for serverless to deploy files to a variety of S3 Buckets 4 | 5 | > **Note: This project is currently not maintained.** 6 | 7 | # Installation 8 | 9 | ``` 10 | npm install --save-dev serverless-s3-deploy 11 | ``` 12 | 13 | # Usage 14 | 15 | Add to your serverless.yml: 16 | 17 | ``` 18 | plugins: 19 | - serverless-s3-deploy 20 | 21 | custom: 22 | assets: 23 | targets: 24 | - bucket: my-bucket 25 | files: 26 | - source: ../assets/ 27 | globs: '**/*.css' 28 | - source: ../app/ 29 | globs: 30 | - '**/*.js' 31 | - '**/*.map' 32 | - bucket: my-other-bucket 33 | empty: true 34 | prefix: subdir 35 | files: 36 | - source: ../email-templates/ 37 | globs: '**/*.html' 38 | ``` 39 | 40 | You can specify any number of `target`s that you want. Each `target` has a 41 | `bucket` and a `prefix`. 42 | 43 | `bucket` is either the name of your S3 bucket or a reference to a 44 | CloudFormation resources created in the same serverless configuration file. 45 | See below for additional details. 46 | 47 | You can specify `source` relative to the current directory. 48 | 49 | Each `source` has its own list of `globs`, which can be either a single glob, 50 | or a list of globs. 51 | 52 | Setting `empty` to `true` will delete all files inside the bucket before 53 | uploading the new content to S3 bucket. The `prefix` value is respected and 54 | files outside will not be deleted. 55 | 56 | Now you can upload all of these assets to your bucket by running: 57 | 58 | ``` 59 | $ sls s3deploy 60 | ``` 61 | 62 | If you have defined multiple buckets, you can limit your deployment to 63 | a single bucket with the `--bucket` option: 64 | 65 | ``` 66 | $ sls s3deploy --bucket my-bucket 67 | ``` 68 | 69 | ## ACL 70 | 71 | You can optionally specificy an ACL for the files uploaded on a per target 72 | basis: 73 | 74 | ``` 75 | custom: 76 | assets: 77 | targets: 78 | - bucket: my-bucket 79 | acl: private 80 | files: 81 | ``` 82 | 83 | The default value is `private`. Options are defined 84 | [here](http://docs.aws.amazon.com/AmazonS3/latest/dev/acl-overview.html#canned-acl). 85 | 86 | ## Content Type 87 | 88 | The appropriate Content Type for each file will attempt to be determined using 89 | ``mime-types``. If one can't be determined, a default fallback of 90 | 'application/octet-stream' will be used. 91 | 92 | You can override this fallback per-source by setting ``defaultContentType``. 93 | 94 | ``` 95 | custom: 96 | assets: 97 | targets: 98 | - bucket: my-bucket 99 | files: 100 | - source: html/ 101 | defaultContentType: text/html 102 | ... 103 | ``` 104 | 105 | ## Other Headers 106 | 107 | Additional headers can be included per target by providing a ``headers`` object. 108 | 109 | See http://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectPUT.html for more 110 | details. 111 | 112 | ``` 113 | custom: 114 | assets: 115 | targets: 116 | - bucket: my-bucket 117 | files: 118 | - source: html/ 119 | headers: 120 | CacheControl: max-age=31104000 # 1 year 121 | ``` 122 | 123 | ## Resolving References 124 | 125 | A common use case is to create the S3 buckets in the `resources` section of 126 | your serverless configuration and then reference it in your S3 plugin 127 | settings: 128 | 129 | ``` 130 | custom: 131 | assets: 132 | targets: 133 | - bucket: 134 | Ref: MyBucket 135 | files: 136 | - source: html/ 137 | 138 | resources: 139 | # AWS CloudFormation Template 140 | Resources: 141 | MyBucket: 142 | Type: AWS::S3::Bucket 143 | Properties: 144 | AccessControl: PublicRead 145 | WebsiteConfiguration: 146 | IndexDocument: index.html 147 | ErrorDocument: index.html 148 | ``` 149 | 150 | You can disable the resolving with the following flag: 151 | ``` 152 | custom: 153 | assets: 154 | resolveReferences: false 155 | ``` 156 | 157 | ## Auto-deploy 158 | 159 | If you want s3deploy to run automatically after a deploy, set the `auto` flag: 160 | 161 | ``` 162 | custom: 163 | assets: 164 | auto: true 165 | ``` 166 | 167 | ## IAM Configuration 168 | 169 | You're going to need an IAM policy that supports this deployment. This might be 170 | a good starting point: 171 | 172 | ``` 173 | { 174 | "Version": "2012-10-17", 175 | "Statement": [ 176 | { 177 | "Effect": "Allow", 178 | "Action": [ 179 | "s3:ListBucket" 180 | ], 181 | "Resource": [ 182 | "arn:aws:s3:::${bucket}" 183 | ] 184 | }, 185 | { 186 | "Effect": "Allow", 187 | "Action": [ 188 | "s3:PutObject", 189 | "s3:PutObjectAcl", 190 | "s3:GetObject", 191 | "s3:DeleteObject" 192 | ], 193 | "Resource": [ 194 | "arn:aws:s3:::${bucket}/*" 195 | ] 196 | } 197 | ] 198 | } 199 | ``` 200 | 201 | ## Upload concurrency 202 | 203 | If you want to tweak the upload concurrency, change `uploadConcurrency` config: 204 | 205 | ``` 206 | config: 207 | assets: 208 | # defaults to 3 209 | uploadConcurrency: 1 210 | ``` 211 | 212 | ## Verbosity 213 | 214 | Verbosity cloud be enabled using either of these methods: 215 | 216 | Configuration: 217 | 218 | ``` 219 | custom: 220 | assets: 221 | verbose: true 222 | ``` 223 | 224 | Cli: 225 | 226 | ``` 227 | sls s3deploy -v 228 | ``` 229 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const glob = require('glob-all'); 4 | const fs = require('fs'); 5 | const path = require('path'); 6 | const mime = require('mime-types'); 7 | const BbPromise = require('bluebird'); 8 | 9 | const globOpts = { 10 | nodir: true 11 | }; 12 | 13 | 14 | class Assets { 15 | constructor (serverless, options) { 16 | this.serverless = serverless; 17 | this.options = options; 18 | this.provider = this.serverless.getProvider('aws'); 19 | 20 | let config = this.serverless.service.custom.assets; 21 | if(Array.isArray(config)) { 22 | config = {targets: config}; 23 | } 24 | 25 | this.config = Object.assign({}, { 26 | auto: false, 27 | verbose: false, 28 | resolveReferences: true, 29 | targets: [], 30 | uploadConcurrency: 3, 31 | }, config); 32 | 33 | this.commands = { 34 | s3deploy: { 35 | usage: 'Deploy assets to S3 bucket', 36 | lifecycleEvents: [ 37 | 'deploy' 38 | ], 39 | options: { 40 | verbose: { 41 | usage: 'Increase verbosity', 42 | shortcut: 'v', 43 | type: 'boolean' 44 | }, 45 | bucket: { 46 | usage: 'Limit the deploy to a specific bucket', 47 | shortcut: 'b', 48 | type: 'string' 49 | } 50 | } 51 | } 52 | }; 53 | 54 | this.hooks = { 55 | 's3deploy:deploy': () => Promise.resolve().then(this.deployS3.bind(this)), 56 | 'after:deploy:finalize': () => Promise.resolve().then(this.afterDeploy.bind(this)) 57 | }; 58 | } 59 | 60 | /* 61 | * Handy method for logging (when `verbose` is set) 62 | * Also log on the default serverless SLS_DEBUG env 63 | */ 64 | log(message) { 65 | if(this.options.verbose || process.env.SLS_DEBUG || this.config.verbose) { 66 | this.serverless.cli.log(message); 67 | } 68 | } 69 | 70 | afterDeploy() { 71 | if(this.config.auto) { 72 | return this.deployS3(); 73 | } 74 | } 75 | 76 | listStackResources(resources, nextToken) { 77 | resources = resources || []; 78 | if (!this.config.resolveReferences) { 79 | return BbPromise.resolve(resources); 80 | } 81 | return this.provider.request('CloudFormation', 'listStackResources', { StackName: this.provider.naming.getStackName(), NextToken: nextToken }) 82 | .then(response => { 83 | resources.push.apply(resources, response.StackResourceSummaries); 84 | if (response.NextToken) { 85 | // Query next page 86 | return this.listStackResources(resources, response.NextToken); 87 | } 88 | }) 89 | .then(() => { 90 | return resources; 91 | }); 92 | } 93 | 94 | resolveBucket(resources, value) { 95 | if (typeof value === 'string') { 96 | return BbPromise.resolve(value); 97 | } 98 | else if (value && value.Ref) { 99 | let resolved; 100 | resources.forEach(resource => { 101 | if (resource && resource.LogicalResourceId === value.Ref) { 102 | resolved = resource.PhysicalResourceId; 103 | } 104 | }); 105 | 106 | if (!resolved) { 107 | this.serverless.cli.log(`WARNING: Failed to resolve reference ${value.Ref}`); 108 | } 109 | return BbPromise.resolve(resolved); 110 | } 111 | else { 112 | return BbPromise.reject(new Error(`Invalid bucket name ${value}`)); 113 | } 114 | } 115 | 116 | emptyBucket(bucket, dir) { 117 | const listParams = { 118 | Bucket: bucket, 119 | Prefix: dir 120 | }; 121 | 122 | return this.provider.request('S3', 'listObjectsV2', listParams) 123 | .then((listedObjects) => { 124 | if (listedObjects.Contents.length === 0) return; 125 | 126 | const deleteParams = { 127 | Bucket: bucket, 128 | Delete: { Objects: [] } 129 | }; 130 | 131 | listedObjects.Contents.forEach(({ Key }) => { 132 | deleteParams.Delete.Objects.push({ Key }); 133 | }); 134 | return this.provider.request('S3', 'deleteObjects', deleteParams) 135 | .then(() => { 136 | if (listedObjects.Contents.IsTruncated) { 137 | this.log('Is not finished. Rerun emptyBucket'); 138 | return this.emptyBucket(bucket, dir); 139 | } 140 | }); 141 | }); 142 | } 143 | 144 | deployS3() { 145 | let assetSets = this.config.targets; 146 | let uploadConcurrency = this.config.uploadConcurrency; 147 | 148 | // Read existing stack resources so we can resolve references if necessary 149 | return this.listStackResources() 150 | .then(resources => { 151 | // Process asset sets in parallel (up to 3) 152 | return BbPromise.map(assetSets, assets => { 153 | const prefix = assets.prefix || ''; 154 | // Try to resolve the bucket name 155 | return this.resolveBucket(resources, assets.bucket) 156 | .then((bucket) => { 157 | if (this.options.bucket && this.options.bucket !== bucket) { 158 | this.log(`Skipping bucket: ${bucket}`); 159 | return Promise.resolve(''); 160 | } 161 | 162 | if(assets.empty) { 163 | this.log(`Emptying bucket: ${bucket}`); 164 | return this.emptyBucket(bucket, prefix) 165 | .then(() => bucket); 166 | } 167 | return Promise.resolve(bucket); 168 | }).then(bucket => { 169 | if (!bucket) { 170 | return; 171 | } 172 | 173 | // Process files serially to not overload the network 174 | return BbPromise.each(assets.files, (opt) => { 175 | this.log(`Sync bucket: ${bucket}:${prefix}`); 176 | this.log(`Path: ${opt.source}`); 177 | 178 | const cfg = Object.assign({}, globOpts, { cwd: opt.source }); 179 | const filenames = glob.sync(opt.globs, cfg); 180 | return BbPromise.each(filenames, (filename) => { 181 | const body = fs.readFileSync(path.join(opt.source, filename)); 182 | const type = mime.lookup(filename) || opt.defaultContentType || 'application/octet-stream'; 183 | 184 | this.log(`\tFile: ${filename} (${type})`); 185 | 186 | // when using windows path join resolves to backslashes, but s3 is expecting a slash 187 | // therefore replace all backslashes with slashes 188 | const key = path 189 | .join(prefix, filename) 190 | .replace(/\\/g, '/'); 191 | 192 | const details = Object.assign( 193 | { 194 | ACL: assets.acl || 'private', 195 | Body: body, 196 | Bucket: bucket, 197 | Key: key, 198 | ContentType: type 199 | }, 200 | opt.headers || {} 201 | ); 202 | 203 | return this.provider.request('S3', 'putObject', details); 204 | }); 205 | }); 206 | }); 207 | }, 208 | { concurrency: uploadConcurrency } 209 | ); 210 | }); 211 | } 212 | } 213 | 214 | module.exports = Assets; 215 | --------------------------------------------------------------------------------