├── .gitignore ├── LICENSE ├── README.md ├── docs └── architecture.png ├── modules ├── output.js └── sanitizeStackName.js ├── package-lock.json ├── package.json ├── resources ├── certificate.yml ├── cf-distribution.yml ├── cloudfront-origin-access-identity.yml ├── custom-acm-certificate-lambda-role.yml ├── custom-acm-certificate-lambda.yml ├── dns-records.yml ├── hosted-zone.yml ├── outputs.yml ├── s3-bucket.yml └── s3-policies.yml ├── serverless.yml └── src └── index.html /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | .serverless 3 | .DS_Store 4 | 5 | # Logs 6 | logs 7 | *.log 8 | npm-debug.log* 9 | yarn-debug.log* 10 | yarn-error.log* 11 | 12 | # Runtime data 13 | pids 14 | *.pid 15 | *.seed 16 | *.pid.lock 17 | 18 | # Directory for instrumented libs generated by jscoverage/JSCover 19 | lib-cov 20 | 21 | # Coverage directory used by tools like istanbul 22 | coverage 23 | 24 | # nyc test coverage 25 | .nyc_output 26 | 27 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 28 | .grunt 29 | 30 | # Bower dependency directory (https://bower.io/) 31 | bower_components 32 | 33 | # node-waf configuration 34 | .lock-wscript 35 | 36 | # Compiled binary addons (https://nodejs.org/api/addons.html) 37 | build/Release 38 | 39 | # Dependency directories 40 | node_modules/ 41 | jspm_packages/ 42 | 43 | # TypeScript v1 declaration files 44 | typings/ 45 | 46 | # Optional npm cache directory 47 | .npm 48 | 49 | # Optional eslint cache 50 | .eslintcache 51 | 52 | # Optional REPL history 53 | .node_repl_history 54 | 55 | # Output of 'npm pack' 56 | *.tgz 57 | 58 | # Yarn Integrity file 59 | .yarn-integrity 60 | 61 | # dotenv environment variables file 62 | .env 63 | 64 | # next.js build output 65 | .next 66 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Tobi 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-aws-static-websites 2 | Host your static website on AWS via S3, with global CDN via CloudFront, using SSL certificates provided by ACM using your own domain name! 3 | 4 | All set up via one [Serverless](https://www.serverless.com) command and minimum manual configuration! 5 | 6 | ## Architecture 7 | 8 | ![Serverless static websites on AWS](docs/architecture.png)[]() 9 | 10 | ### What is provisioned in your AWS account? 11 | * A S3 bucket containing your static website 12 | * A CloudFront distribution for global hosting via CDN 13 | * A HostedZone on Route53 with A records for your domain name 14 | * A Lambda function for automatic SSL certificate generation via ACM for your domain name (run once upon deployment) 15 | 16 | ## Preconditions 17 | This guide assumes that you have a pre-existing domain which you want to use for hosting your static website. Furthermore, you need to have access to the domain's DNS configuration. 18 | 19 | Also, you need to have an install of [Serverless](https://www.serverless.com) on your machine. 20 | 21 | ## How-to 22 | To use this blueprint with your own static websites, you can follow the steps below to get started. 23 | 24 | ### Set up a Serverless project for your static website 25 | There are basically two ways to get started, either use [Serverless](https://www.serverless.com) to generate a basic project structure, or use the "traditional" fork and clone mechanisms. 26 | 27 | #### Use Serverless templates 28 | The following command will create a local project structure you can use to deploy your static website in the `mystaticwebsite` folder relative to your current working directory: 29 | 30 | ```bash 31 | $ sls create --template-url https://github.com/tobilg/serverless-aws-static-websites --path mystaticwebsite 32 | Serverless: Generating boilerplate... 33 | Serverless: Downloading and installing "serverless-aws-static-websites"... 34 | Serverless: Successfully installed "serverless-aws-static-websites" 35 | ``` 36 | 37 | **Hint** 38 | When using this method, Serverless is replacing the `service.name` in the `serverless.yml` file automatically with `mystaticwebiste`. If you want to use a different stack name, you have to replace it manually. You also need to take care of that the stack name is using only allowed characters. When using the "Fork and clone" method below, the stack name is automatically derived from the domain name and sanitized regarding the allowed characters. 39 | 40 | #### Fork and clone 41 | Once you forked the repo on GitHub, you can clone it locally via 42 | 43 | ```bash 44 | $ git clone git@github.com:youraccount/yourrepo.git 45 | ``` 46 | 47 | where `youraccount/yourrepo` needs to be replaced with the actual repository name of your forked repo. 48 | 49 | ### Install dependencies 50 | To install the dependencies, do a 51 | 52 | ```bash 53 | $ npm i 54 | ``` 55 | 56 | After that, the project is usable. 57 | 58 | ### Create your static website 59 | You can now create your static website in the `src` folder of your cloned repo. 60 | 61 | ### Deploy 62 | You can deploy your static website with the following command: 63 | 64 | ```bash 65 | $ sls deploy --domain yourdomain.yourtld 66 | ``` 67 | 68 | where `yourdomain.yourtld` needs to be replaced with your actual domain name. You can also specify a AWS region via the `--region` flag, otherwise `us-east-1` will be used. 69 | 70 | #### Manual update of DNS records on first deploy 71 | On the first deploy, it is necessary to update the DNS setting for the domain manually, otherwise the hosted zone will not be able to be established. 72 | 73 | Therefore, once you triggered the `sls deploy` command, you need to log into the AWS console, go to the [Hosted Zones](https://console.aws.amazon.com/route53/home?region=eu-central-1#hosted-zones:) menu and select the corresponding domain name you used for the deployment. 74 | 75 | The nameservers you have to configure your domain DNS to can be found under the `NS` record and will look similar to this: 76 | 77 | ```bash 78 | ns-1807.awsdns-33.co.uk. 79 | ns-977.awsdns-58.net. 80 | ns-1351.awsdns-40.org. 81 | ns-32.awsdns-04.com. 82 | ``` 83 | 84 | You should then update your DNS settings for your domain with those values, **otherwise the stack creation process will fail**. 85 | 86 | This is a bit misfortunate, but to the best of knowledge there's currently no other way possible if you use AWS external (non-Route53) domains. During my tests with [namecheap.com](https://www.namecheap.com) domains the DNS records were always updated fast enough, so that the stack creation didn't fail. 87 | 88 | #### Deployment process duration 89 | As a new CloudFront distribution is created (which is pretty slow), it can take **up to 45min** for the initial deploy to finish. This is normal and expected behavior. 90 | 91 | ### Post-deploy 92 | If the deployment finished successfully, you will be able to access your domain via `https://www.yourdomain.yourtld` and `https://yourdomain.yourtld`. 93 | 94 | This setup should give you some good [PageSpeed Insights](https://developers.google.com/speed/pagespeed/insights/?hl=en) results. 95 | 96 | ### Updates 97 | For every update of your website, you can trigger a deploy as stated above. This will effectively just do s S3 sync of the `src` folder. 98 | 99 | To do a manual sync, your can also use `sls s3sync`. There's also the possibility to customize the caching behavior for individual files or file types via the `serverless.yml`, see the [s3sync plugin's documentation](https://www.npmjs.com/package/serverless-s3-sync#setup). 100 | 101 | As **CloudFront caches the contents of the website**, a [Serverless plugin](https://github.com/aghadiry/serverless-cloudfront-invalidate) is used to invalidate files. This [may incur costs](https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/Invalidation.html#PayingForInvalidation), see the [docs](https://aws.amazon.com/de/premiumsupport/knowledge-center/cloudfront-serving-outdated-content-s3/) for more info. 102 | 103 | You can run `sls cloudfrontInvalidate` to do a standalone invalidation of the defined files in the `serverless.yml`. 104 | 105 | ## Removal 106 | If you want to remove the created stack, you will have to delete all records of the Hosted Zone of the respective domain except the `SOA` and `NS` records, otherwise the stack deletion via 107 | 108 | ```bash 109 | $ sls remove --domain yourdomain.yourtld 110 | ``` 111 | 112 | will fail. 113 | -------------------------------------------------------------------------------- /docs/architecture.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tobilg/serverless-aws-static-websites/12db702c3a8b6e63acdd7d284b40f0841b5a6636/docs/architecture.png -------------------------------------------------------------------------------- /modules/output.js: -------------------------------------------------------------------------------- 1 | module.exports.handler = (data, serverless, options) => { 2 | serverless.cli.log(`DNS nameservers to be configured with your '${serverless.providers.aws.options.domain}' domain: ${data.HostedZoneNameservers}`); 3 | }; 4 | -------------------------------------------------------------------------------- /modules/sanitizeStackName.js: -------------------------------------------------------------------------------- 1 | module.exports.sanitize = (serverless) => { 2 | if (!serverless.providers.aws.options.hasOwnProperty('domain')) { 3 | serverless.cli.log('No domain flag found, exiting! Please specify --domain '); 4 | process.exit(1); 5 | } else { 6 | const sanitizedStackName = `website-${serverless.providers.aws.options.domain.replace(/\./g, '-')}`; 7 | serverless.cli.log(`Stack name is ${sanitizedStackName}`); 8 | return sanitizedStackName; 9 | } 10 | }; 11 | -------------------------------------------------------------------------------- /package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "serverless-aws-static-websites", 3 | "version": "0.1.0", 4 | "lockfileVersion": 1, 5 | "requires": true, 6 | "dependencies": { 7 | "@auth0/s3": { 8 | "version": "1.0.0", 9 | "resolved": "https://registry.npmjs.org/@auth0/s3/-/s3-1.0.0.tgz", 10 | "integrity": "sha512-O8PTXJnA7n8ONBSwqlWa+aZ/vlOdZYnSCGQt25h87ALWNItY/Yij79TOnzIkMTJZ8aCpGXQPuIRziLmBliV++Q==", 11 | "dev": true, 12 | "requires": { 13 | "aws-sdk": "^2.346.0", 14 | "fd-slicer": "~1.0.0", 15 | "findit2": "~2.2.3", 16 | "graceful-fs": "~4.1.4", 17 | "mime": "^2.3.1", 18 | "mkdirp": "~0.5.0", 19 | "pend": "~1.2.0", 20 | "rimraf": "~2.2.8", 21 | "streamsink": "~1.2.0" 22 | } 23 | }, 24 | "ansi-styles": { 25 | "version": "3.2.1", 26 | "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-3.2.1.tgz", 27 | "integrity": "sha512-VT0ZI6kZRdTh8YyJw3SMbYm/u+NqfsAxEpWO0Pf9sq8/e94WxxOpPKx9FR1FlyCtOVDNOQ+8ntlqFxiRc+r5qA==", 28 | "dev": true, 29 | "requires": { 30 | "color-convert": "^1.9.0" 31 | } 32 | }, 33 | "argparse": { 34 | "version": "1.0.10", 35 | "resolved": "https://registry.npmjs.org/argparse/-/argparse-1.0.10.tgz", 36 | "integrity": "sha512-o5Roy6tNG4SL/FOkCAN6RzjiakZS25RLYFrcMttJqbdd8BWrnA+fGz57iN5Pb06pvBGvl5gQ0B48dJlslXvoTg==", 37 | "dev": true, 38 | "requires": { 39 | "sprintf-js": "~1.0.2" 40 | } 41 | }, 42 | "array-uniq": { 43 | "version": "1.0.2", 44 | "resolved": "https://registry.npmjs.org/array-uniq/-/array-uniq-1.0.2.tgz", 45 | "integrity": "sha1-X8w3OSB3VyPP1k1lxkvvU7+eum0=", 46 | "dev": true 47 | }, 48 | "aws-sdk": { 49 | "version": "2.444.0", 50 | "resolved": "https://registry.npmjs.org/aws-sdk/-/aws-sdk-2.444.0.tgz", 51 | "integrity": "sha512-3vdC7l5BJ3zHzVNgtIxD+TDviti/sAA/1T8zAXAm2XhZ7AePR5lYIMNAwqu+J44Nm6PFSK1QNSzRQ6A4/6b9eA==", 52 | "dev": true, 53 | "requires": { 54 | "buffer": "4.9.1", 55 | "events": "1.1.1", 56 | "ieee754": "1.1.8", 57 | "jmespath": "0.15.0", 58 | "querystring": "0.2.0", 59 | "sax": "1.2.1", 60 | "url": "0.10.3", 61 | "uuid": "3.3.2", 62 | "xml2js": "0.4.19" 63 | }, 64 | "dependencies": { 65 | "ieee754": { 66 | "version": "1.1.8", 67 | "resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.1.8.tgz", 68 | "integrity": "sha1-vjPUCsEO8ZJnAfbwii2G+/0a0+Q=", 69 | "dev": true 70 | } 71 | } 72 | }, 73 | "balanced-match": { 74 | "version": "1.0.0", 75 | "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", 76 | "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=", 77 | "dev": true 78 | }, 79 | "base64-js": { 80 | "version": "1.3.0", 81 | "resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.3.0.tgz", 82 | "integrity": "sha512-ccav/yGvoa80BQDljCxsmmQ3Xvx60/UpBIij5QN21W3wBi/hhIC9OoO+KLpu9IJTS9j4DRVJ3aDDF9cMSoa2lw==", 83 | "dev": true 84 | }, 85 | "bluebird": { 86 | "version": "3.5.4", 87 | "resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.5.4.tgz", 88 | "integrity": "sha512-FG+nFEZChJrbQ9tIccIfZJBz3J7mLrAhxakAbnrJWn8d7aKOC+LWifa0G+p4ZqKp4y13T7juYvdhq9NzKdsrjw==", 89 | "dev": true 90 | }, 91 | "brace-expansion": { 92 | "version": "1.1.11", 93 | "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", 94 | "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", 95 | "dev": true, 96 | "requires": { 97 | "balanced-match": "^1.0.0", 98 | "concat-map": "0.0.1" 99 | } 100 | }, 101 | "buffer": { 102 | "version": "4.9.1", 103 | "resolved": "https://registry.npmjs.org/buffer/-/buffer-4.9.1.tgz", 104 | "integrity": "sha1-bRu2AbB6TvztlwlBMgkwJ8lbwpg=", 105 | "dev": true, 106 | "requires": { 107 | "base64-js": "^1.0.2", 108 | "ieee754": "^1.1.4", 109 | "isarray": "^1.0.0" 110 | } 111 | }, 112 | "chalk": { 113 | "version": "2.4.2", 114 | "resolved": "https://registry.npmjs.org/chalk/-/chalk-2.4.2.tgz", 115 | "integrity": "sha512-Mti+f9lpJNcwF4tWV8/OrTTtF1gZi+f8FqlyAdouralcFWFQWF2+NgCHShjkCb+IFBLq9buZwE1xckQU4peSuQ==", 116 | "dev": true, 117 | "requires": { 118 | "ansi-styles": "^3.2.1", 119 | "escape-string-regexp": "^1.0.5", 120 | "supports-color": "^5.3.0" 121 | } 122 | }, 123 | "color-convert": { 124 | "version": "1.9.3", 125 | "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-1.9.3.tgz", 126 | "integrity": "sha512-QfAUtd+vFdAtFQcC8CCyYt1fYWxSqAiK2cSD6zDB8N3cpsEBAvRxp9zOGg6G/SHHJYAT88/az/IuDGALsNVbGg==", 127 | "dev": true, 128 | "requires": { 129 | "color-name": "1.1.3" 130 | } 131 | }, 132 | "color-name": { 133 | "version": "1.1.3", 134 | "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.3.tgz", 135 | "integrity": "sha1-p9BVi9icQveV3UIyj3QIMcpTvCU=", 136 | "dev": true 137 | }, 138 | "concat-map": { 139 | "version": "0.0.1", 140 | "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", 141 | "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=", 142 | "dev": true 143 | }, 144 | "escape-string-regexp": { 145 | "version": "1.0.5", 146 | "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz", 147 | "integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=", 148 | "dev": true 149 | }, 150 | "events": { 151 | "version": "1.1.1", 152 | "resolved": "https://registry.npmjs.org/events/-/events-1.1.1.tgz", 153 | "integrity": "sha1-nr23Y1rQmccNzEwqH1AEKI6L2SQ=", 154 | "dev": true 155 | }, 156 | "fd-slicer": { 157 | "version": "1.0.1", 158 | "resolved": "https://registry.npmjs.org/fd-slicer/-/fd-slicer-1.0.1.tgz", 159 | "integrity": "sha1-i1vL2ewyfFBBv5qwI/1nUPEXfmU=", 160 | "dev": true, 161 | "requires": { 162 | "pend": "~1.2.0" 163 | } 164 | }, 165 | "findit2": { 166 | "version": "2.2.3", 167 | "resolved": "https://registry.npmjs.org/findit2/-/findit2-2.2.3.tgz", 168 | "integrity": "sha1-WKRmaX34piBc39vzlVNri9d3pfY=", 169 | "dev": true 170 | }, 171 | "fs.realpath": { 172 | "version": "1.0.0", 173 | "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", 174 | "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=", 175 | "dev": true 176 | }, 177 | "glob": { 178 | "version": "7.1.3", 179 | "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.3.tgz", 180 | "integrity": "sha512-vcfuiIxogLV4DlGBHIUOwI0IbrJ8HWPc4MU7HzviGeNho/UJDfi6B5p3sHeWIQ0KGIU0Jpxi5ZHxemQfLkkAwQ==", 181 | "dev": true, 182 | "requires": { 183 | "fs.realpath": "^1.0.0", 184 | "inflight": "^1.0.4", 185 | "inherits": "2", 186 | "minimatch": "^3.0.4", 187 | "once": "^1.3.0", 188 | "path-is-absolute": "^1.0.0" 189 | } 190 | }, 191 | "graceful-fs": { 192 | "version": "4.1.15", 193 | "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.1.15.tgz", 194 | "integrity": "sha512-6uHUhOPEBgQ24HM+r6b/QwWfZq+yiFcipKFrOFiBEnWdy5sdzYoi+pJeQaPI5qOLRFqWmAXUPQNsielzdLoecA==", 195 | "dev": true 196 | }, 197 | "has-flag": { 198 | "version": "3.0.0", 199 | "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-3.0.0.tgz", 200 | "integrity": "sha1-tdRU3CGZriJWmfNGfloH87lVuv0=", 201 | "dev": true 202 | }, 203 | "ieee754": { 204 | "version": "1.1.12", 205 | "resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.1.12.tgz", 206 | "integrity": "sha512-GguP+DRY+pJ3soyIiGPTvdiVXjZ+DbXOxGpXn3eMvNW4x4irjqXm4wHKscC+TfxSJ0yw/S1F24tqdMNsMZTiLA==", 207 | "dev": true 208 | }, 209 | "inflight": { 210 | "version": "1.0.6", 211 | "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", 212 | "integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=", 213 | "dev": true, 214 | "requires": { 215 | "once": "^1.3.0", 216 | "wrappy": "1" 217 | } 218 | }, 219 | "inherits": { 220 | "version": "2.0.3", 221 | "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz", 222 | "integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=", 223 | "dev": true 224 | }, 225 | "isarray": { 226 | "version": "1.0.0", 227 | "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", 228 | "integrity": "sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE=", 229 | "dev": true 230 | }, 231 | "jmespath": { 232 | "version": "0.15.0", 233 | "resolved": "https://registry.npmjs.org/jmespath/-/jmespath-0.15.0.tgz", 234 | "integrity": "sha1-o/Iiqarp+Wb10nx5ZRDigJF2Qhc=", 235 | "dev": true 236 | }, 237 | "mime": { 238 | "version": "2.4.2", 239 | "resolved": "https://registry.npmjs.org/mime/-/mime-2.4.2.tgz", 240 | "integrity": "sha512-zJBfZDkwRu+j3Pdd2aHsR5GfH2jIWhmL1ZzBoc+X+3JEti2hbArWcyJ+1laC1D2/U/W1a/+Cegj0/OnEU2ybjg==", 241 | "dev": true 242 | }, 243 | "minimatch": { 244 | "version": "3.0.4", 245 | "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", 246 | "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", 247 | "dev": true, 248 | "requires": { 249 | "brace-expansion": "^1.1.7" 250 | } 251 | }, 252 | "minimist": { 253 | "version": "0.0.8", 254 | "resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz", 255 | "integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0=", 256 | "dev": true 257 | }, 258 | "mkdirp": { 259 | "version": "0.5.1", 260 | "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz", 261 | "integrity": "sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM=", 262 | "dev": true, 263 | "requires": { 264 | "minimist": "0.0.8" 265 | } 266 | }, 267 | "once": { 268 | "version": "1.4.0", 269 | "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", 270 | "integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=", 271 | "dev": true, 272 | "requires": { 273 | "wrappy": "1" 274 | } 275 | }, 276 | "path-is-absolute": { 277 | "version": "1.0.1", 278 | "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", 279 | "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=", 280 | "dev": true 281 | }, 282 | "pend": { 283 | "version": "1.2.0", 284 | "resolved": "https://registry.npmjs.org/pend/-/pend-1.2.0.tgz", 285 | "integrity": "sha1-elfrVQpng/kRUzH89GY9XI4AelA=", 286 | "dev": true 287 | }, 288 | "punycode": { 289 | "version": "1.3.2", 290 | "resolved": "https://registry.npmjs.org/punycode/-/punycode-1.3.2.tgz", 291 | "integrity": "sha1-llOgNvt8HuQjQvIyXM7v6jkmxI0=", 292 | "dev": true 293 | }, 294 | "querystring": { 295 | "version": "0.2.0", 296 | "resolved": "https://registry.npmjs.org/querystring/-/querystring-0.2.0.tgz", 297 | "integrity": "sha1-sgmEkgO7Jd+CDadW50cAWHhSFiA=", 298 | "dev": true 299 | }, 300 | "randomstring": { 301 | "version": "1.1.5", 302 | "resolved": "https://registry.npmjs.org/randomstring/-/randomstring-1.1.5.tgz", 303 | "integrity": "sha1-bfBij3XL1ZMpMNn+OrTpVqGFGMM=", 304 | "dev": true, 305 | "requires": { 306 | "array-uniq": "1.0.2" 307 | } 308 | }, 309 | "rimraf": { 310 | "version": "2.2.8", 311 | "resolved": "https://registry.npmjs.org/rimraf/-/rimraf-2.2.8.tgz", 312 | "integrity": "sha1-5Dm+Kq7jJzIZUnMPmaiSnk/FBYI=", 313 | "dev": true 314 | }, 315 | "sax": { 316 | "version": "1.2.1", 317 | "resolved": "https://registry.npmjs.org/sax/-/sax-1.2.1.tgz", 318 | "integrity": "sha1-e45lYZCyKOgaZq6nSEgNgozS03o=", 319 | "dev": true 320 | }, 321 | "serverless-cloudfront-invalidate": { 322 | "version": "1.3.0", 323 | "resolved": "https://registry.npmjs.org/serverless-cloudfront-invalidate/-/serverless-cloudfront-invalidate-1.3.0.tgz", 324 | "integrity": "sha512-AX/3aFd+U71cUK0ANL8afe8h5E7lBhma7coJMSfiUlvUvdsGVMWIbiDmiqSBqO4MXwRQKeegLPuOJ9NPHZc52A==", 325 | "dev": true, 326 | "requires": { 327 | "aws-sdk": "^2.224.1", 328 | "chalk": "^2.3.1", 329 | "randomstring": "^1.1.5" 330 | }, 331 | "dependencies": { 332 | "aws-sdk": { 333 | "version": "2.441.0", 334 | "resolved": "https://registry.npmjs.org/aws-sdk/-/aws-sdk-2.441.0.tgz", 335 | "integrity": "sha512-PnS2lih7p6sPJYUeUSxab7VNsldcHEsCJddHXnnAZRxd2nVa8pAAbPSAauJIN9E6Z4DBhvX3nuQjj+roP/KBTg==", 336 | "dev": true, 337 | "requires": { 338 | "buffer": "4.9.1", 339 | "events": "1.1.1", 340 | "ieee754": "1.1.8", 341 | "jmespath": "0.15.0", 342 | "querystring": "0.2.0", 343 | "sax": "1.2.1", 344 | "url": "0.10.3", 345 | "uuid": "3.3.2", 346 | "xml2js": "0.4.19" 347 | } 348 | }, 349 | "ieee754": { 350 | "version": "1.1.8", 351 | "resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.1.8.tgz", 352 | "integrity": "sha1-vjPUCsEO8ZJnAfbwii2G+/0a0+Q=", 353 | "dev": true 354 | }, 355 | "uuid": { 356 | "version": "3.3.2", 357 | "resolved": "https://registry.npmjs.org/uuid/-/uuid-3.3.2.tgz", 358 | "integrity": "sha512-yXJmeNaw3DnnKAOKJE51sL/ZaYfWJRl1pK9dr19YFCu0ObS231AB1/LbqTKRAQ5kw8A90rA6fr4riOUpTZvQZA==", 359 | "dev": true 360 | }, 361 | "xml2js": { 362 | "version": "0.4.19", 363 | "resolved": "https://registry.npmjs.org/xml2js/-/xml2js-0.4.19.tgz", 364 | "integrity": "sha512-esZnJZJOiJR9wWKMyuvSE1y6Dq5LCuJanqhxslH2bxM6duahNZ+HMpCLhBQGZkbX6xRf8x1Y2eJlgt2q3qo49Q==", 365 | "dev": true, 366 | "requires": { 367 | "sax": ">=0.6.0", 368 | "xmlbuilder": "~9.0.1" 369 | } 370 | }, 371 | "xmlbuilder": { 372 | "version": "9.0.7", 373 | "resolved": "https://registry.npmjs.org/xmlbuilder/-/xmlbuilder-9.0.7.tgz", 374 | "integrity": "sha1-Ey7mPS7FVlxVfiD0wi35rKaGsQ0=", 375 | "dev": true 376 | } 377 | } 378 | }, 379 | "serverless-pseudo-parameters": { 380 | "version": "2.4.0", 381 | "resolved": "https://registry.npmjs.org/serverless-pseudo-parameters/-/serverless-pseudo-parameters-2.4.0.tgz", 382 | "integrity": "sha512-lb9R62PUFdEAbbYH7pe1wzR7vtIpa8YI8OVcQ5LlLyE0+AxWG4bwEw33X5LE8+5oLwTy57Y/EevnxKnMeyiXxw==", 383 | "dev": true 384 | }, 385 | "serverless-s3-sync": { 386 | "version": "1.8.0", 387 | "resolved": "https://registry.npmjs.org/serverless-s3-sync/-/serverless-s3-sync-1.8.0.tgz", 388 | "integrity": "sha512-WD937VPDITWsXpGpvLKHBJf7/4vAwbHPEhK1NJxsAhPq0I5ZA6dNw28qtBJLri9bwM1Zx3zZ7dsrhKHkPjDMgg==", 389 | "dev": true, 390 | "requires": { 391 | "@auth0/s3": "^1.0.0", 392 | "bluebird": "^3.5.4", 393 | "chalk": "^2.4.2", 394 | "minimatch": "^3.0.4" 395 | } 396 | }, 397 | "serverless-stack-output": { 398 | "version": "0.2.3", 399 | "resolved": "https://registry.npmjs.org/serverless-stack-output/-/serverless-stack-output-0.2.3.tgz", 400 | "integrity": "sha512-bJUUXJ+bc0E+IBlWz+FyUpdNjEmSM0dYd2UpDX0h2zeW+luPOJeP//rbIamBmteJjbed9S3FtRWnLLRAJbzM5g==", 401 | "dev": true, 402 | "requires": { 403 | "tomlify-j0.4": "^2.0.0", 404 | "yamljs": "^0.3.0" 405 | } 406 | }, 407 | "sprintf-js": { 408 | "version": "1.0.3", 409 | "resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz", 410 | "integrity": "sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw=", 411 | "dev": true 412 | }, 413 | "streamsink": { 414 | "version": "1.2.0", 415 | "resolved": "https://registry.npmjs.org/streamsink/-/streamsink-1.2.0.tgz", 416 | "integrity": "sha1-76/unx4i01ke1949yqlcP1559zw=", 417 | "dev": true 418 | }, 419 | "supports-color": { 420 | "version": "5.5.0", 421 | "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.5.0.tgz", 422 | "integrity": "sha512-QjVjwdXIt408MIiAqCX4oUKsgU2EqAGzs2Ppkm4aQYbjm+ZEWEcW4SfFNTr4uMNZma0ey4f5lgLrkB0aX0QMow==", 423 | "dev": true, 424 | "requires": { 425 | "has-flag": "^3.0.0" 426 | } 427 | }, 428 | "tomlify-j0.4": { 429 | "version": "2.2.1", 430 | "resolved": "https://registry.npmjs.org/tomlify-j0.4/-/tomlify-j0.4-2.2.1.tgz", 431 | "integrity": "sha512-0kCocYX8ujnbK6jQ9e+g9GLiCIfVkFaCB3DCTQDP7J79gPVZVmZgQZ/KUNe1a6hUfrmHHaErVGUjedfpaX5EZw==", 432 | "dev": true 433 | }, 434 | "url": { 435 | "version": "0.10.3", 436 | "resolved": "https://registry.npmjs.org/url/-/url-0.10.3.tgz", 437 | "integrity": "sha1-Ah5NnHcF8hu/N9A861h2dAJ3TGQ=", 438 | "dev": true, 439 | "requires": { 440 | "punycode": "1.3.2", 441 | "querystring": "0.2.0" 442 | } 443 | }, 444 | "uuid": { 445 | "version": "3.3.2", 446 | "resolved": "https://registry.npmjs.org/uuid/-/uuid-3.3.2.tgz", 447 | "integrity": "sha512-yXJmeNaw3DnnKAOKJE51sL/ZaYfWJRl1pK9dr19YFCu0ObS231AB1/LbqTKRAQ5kw8A90rA6fr4riOUpTZvQZA==", 448 | "dev": true 449 | }, 450 | "wrappy": { 451 | "version": "1.0.2", 452 | "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", 453 | "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=", 454 | "dev": true 455 | }, 456 | "xml2js": { 457 | "version": "0.4.19", 458 | "resolved": "https://registry.npmjs.org/xml2js/-/xml2js-0.4.19.tgz", 459 | "integrity": "sha512-esZnJZJOiJR9wWKMyuvSE1y6Dq5LCuJanqhxslH2bxM6duahNZ+HMpCLhBQGZkbX6xRf8x1Y2eJlgt2q3qo49Q==", 460 | "dev": true, 461 | "requires": { 462 | "sax": ">=0.6.0", 463 | "xmlbuilder": "~9.0.1" 464 | } 465 | }, 466 | "xmlbuilder": { 467 | "version": "9.0.7", 468 | "resolved": "https://registry.npmjs.org/xmlbuilder/-/xmlbuilder-9.0.7.tgz", 469 | "integrity": "sha1-Ey7mPS7FVlxVfiD0wi35rKaGsQ0=", 470 | "dev": true 471 | }, 472 | "yamljs": { 473 | "version": "0.3.0", 474 | "resolved": "https://registry.npmjs.org/yamljs/-/yamljs-0.3.0.tgz", 475 | "integrity": "sha512-C/FsVVhht4iPQYXOInoxUM/1ELSf9EsgKH34FofQOp6hwCPrW4vG4w5++TED3xRUo8gD7l0P1J1dLlDYzODsTQ==", 476 | "dev": true, 477 | "requires": { 478 | "argparse": "^1.0.7", 479 | "glob": "^7.0.5" 480 | } 481 | } 482 | } 483 | } 484 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "serverless-aws-static-websites", 3 | "version": "0.1.0", 4 | "description": "Deploy your static websites without all the hassle on AWS with CloudFront, S3, ACM and Route53 via Serverless", 5 | "scripts": { 6 | "test": "echo \"Error: no test specified\" && exit 1" 7 | }, 8 | "repository": { 9 | "type": "git", 10 | "url": "git@github.com:tobilg/serverless-aws-static-websites.git" 11 | }, 12 | "author": "", 13 | "license": "UNLICENSED", 14 | "bugs": { 15 | "url": "https://github.com/tobilg/serverless-aws-static-websites/issues" 16 | }, 17 | "homepage": "https://github.com/tobilg/serverless-aws-static-websites#readme", 18 | "devDependencies": { 19 | "serverless-cloudfront-invalidate": "^1.3.0", 20 | "serverless-pseudo-parameters": "^2.4.0", 21 | "serverless-s3-sync": "^1.8.0", 22 | "serverless-stack-output": "^0.2.3" 23 | }, 24 | "dependencies": {} 25 | } 26 | -------------------------------------------------------------------------------- /resources/certificate.yml: -------------------------------------------------------------------------------- 1 | Resources: 2 | SSLCertificate: 3 | Type: 'Custom::DNSCertificate' 4 | DependsOn: 5 | - HostedZone 6 | Properties: 7 | DomainName: '${opt:domain}' 8 | SubjectAlternativeNames: 9 | - 'www.${opt:domain}' 10 | ValidationMethod: DNS 11 | # Needs to be in us-east-1 because of CloudFront limitations 12 | Region: us-east-1 13 | DomainValidationOptions: 14 | - DomainName: '${opt:domain}' 15 | HostedZoneId: '#{HostedZone}' 16 | ServiceToken: '#{CustomAcmCertificateLambda.Arn}' 17 | -------------------------------------------------------------------------------- /resources/cf-distribution.yml: -------------------------------------------------------------------------------- 1 | # See https://blog.m-taylor.co.uk/2018/01/cloudformation-template-for-a-cloudfront-enabled-s3-website.html 2 | Resources: 3 | CFDistribution: 4 | Type: 'AWS::CloudFront::Distribution' 5 | DependsOn: 6 | - WebsiteBucket 7 | - HostedZone 8 | - SSLCertificate 9 | - CloudFrontOriginAccessIdentity 10 | Properties: 11 | DistributionConfig: 12 | Aliases: 13 | - '${opt:domain}' 14 | - 'www.${opt:domain}' 15 | Origins: 16 | - DomainName: '#{WebsiteBucket.DomainName}' 17 | OriginPath: '' 18 | Id: S3BucketOrigin 19 | S3OriginConfig: 20 | OriginAccessIdentity: 21 | Fn::Join: 22 | - '' 23 | - - 'origin-access-identity/cloudfront/' 24 | - '#{CloudFrontOriginAccessIdentity}' 25 | Comment: 'CloudFront origin for ${opt:domain}' 26 | DefaultCacheBehavior: 27 | AllowedMethods: 28 | - GET 29 | - HEAD 30 | - OPTIONS 31 | TargetOriginId: S3BucketOrigin 32 | Compress: true 33 | ForwardedValues: 34 | QueryString: 'false' 35 | Cookies: 36 | Forward: none 37 | ViewerProtocolPolicy: redirect-to-https 38 | DefaultRootObject: index.html 39 | Enabled: 'true' 40 | HttpVersion: 'http2' 41 | PriceClass: 'PriceClass_100' 42 | ViewerCertificate: 43 | AcmCertificateArn: '#{SSLCertificate}' 44 | SslSupportMethod: sni-only 45 | -------------------------------------------------------------------------------- /resources/cloudfront-origin-access-identity.yml: -------------------------------------------------------------------------------- 1 | Resources: 2 | CloudFrontOriginAccessIdentity: 3 | Type: 'AWS::CloudFront::CloudFrontOriginAccessIdentity' 4 | Properties: 5 | CloudFrontOriginAccessIdentityConfig: 6 | Comment: '${self:service.name}-oai' -------------------------------------------------------------------------------- /resources/custom-acm-certificate-lambda-role.yml: -------------------------------------------------------------------------------- 1 | Resources: 2 | CustomAcmCertificateLambdaExecutionRole: 3 | Type: 'AWS::IAM::Role' 4 | Properties: 5 | AssumeRolePolicyDocument: 6 | Statement: 7 | - Action: 8 | - sts:AssumeRole 9 | Effect: Allow 10 | Principal: 11 | Service: lambda.amazonaws.com 12 | Version: '2012-10-17' 13 | ManagedPolicyArns: 14 | - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole 15 | - arn:aws:iam::aws:policy/service-role/AWSLambdaRole 16 | Policies: 17 | - PolicyDocument: 18 | Statement: 19 | - Action: 20 | - acm:AddTagsToCertificate 21 | - acm:DeleteCertificate 22 | - acm:DescribeCertificate 23 | - acm:RemoveTagsFromCertificate 24 | Effect: Allow 25 | Resource: 26 | - 'arn:aws:acm:*:#{AWS::AccountId}:certificate/*' 27 | - Action: 28 | - acm:RequestCertificate 29 | - acm:ListTagsForCertificate 30 | - acm:ListCertificates 31 | Effect: Allow 32 | Resource: 33 | - '*' 34 | - Action: 35 | - route53:ChangeResourceRecordSets 36 | Effect: Allow 37 | Resource: 38 | - arn:aws:route53:::hostedzone/* 39 | Version: '2012-10-17' 40 | PolicyName: 'CustomAcmCertificateLambdaExecutionPolicy-${self:service.name}' 41 | -------------------------------------------------------------------------------- /resources/custom-acm-certificate-lambda.yml: -------------------------------------------------------------------------------- 1 | Resources: 2 | CustomAcmCertificateLambda: 3 | Type: 'AWS::Lambda::Function' 4 | Metadata: 5 | Source: https://github.com/dflook/cloudformation-dns-certificate 6 | Version: 1.7.1 7 | Properties: 8 | Description: Cloudformation custom resource for DNS validated certificates 9 | Handler: index.handler 10 | Role: '#{CustomAcmCertificateLambdaExecutionRole.Arn}' 11 | Runtime: python3.6 12 | Timeout: 900 13 | Code: 14 | ZipFile: "T=RuntimeError\nimport copy,hashlib as t,json,logging as B,time\ 15 | \ as b\nfrom boto3 import client as K\nfrom botocore.exceptions import ClientError\ 16 | \ as u,ParamValidationError as v\nfrom botocore.vendored import requests\ 17 | \ as w\nA=B.getLogger()\nA.setLevel(B.INFO)\nD=A.info\nS=A.exception\nd=json.dumps\n\ 18 | M=copy.copy\ne=b.sleep\ndef handler(event,c):\n\tA9='OldResourceProperties';A8='Update';A7='Delete';A6='None';A5='acm';A4='FAILED';A3='properties';A2='stack-id';A1='logical-id';A0='DNS';s='Old';r='Certificate';q='LogicalResourceId';p='DomainName';o='ValidationMethod';n='Route53RoleArn';m='Region';a='RequestType';Z='Reinvoked';Y='StackId';X=None;R='Status';Q='Key';P='';O=True;N='DomainValidationOptions';L=False;J='ResourceProperties';I='cloudformation:';H='Value';G='CertificateArn';F='Tags';C='PhysicalResourceId';A=event;f=c.get_remaining_time_in_millis;D(A)\n\ 19 | \tdef g():\n\t\tD=M(B)\n\t\tfor H in ['ServiceToken',m,F,n]:D.pop(H,X)\n\ 20 | \t\tif o in B:\n\t\t\tif B[o]==A0:\n\t\t\t\tfor I in set([B[p]]+B.get('SubjectAlternativeNames',[])):k(I)\n\ 21 | \t\t\t\tdel D[N]\n\t\tA[C]=E.request_certificate(IdempotencyToken=y,**D)[G];l()\n\ 22 | \tdef U(a):\n\t\twhile O:\n\t\t\ttry:E.delete_certificate(**{G:a});return\n\ 23 | \t\t\texcept u as B:\n\t\t\t\tS(P);A=B.response['Error']['Code']\n\t\t\t\ 24 | \tif A=='ResourceInUseException':\n\t\t\t\t\tif f()/1000<30:raise\n\t\t\t\ 25 | \t\te(5);continue\n\t\t\t\tif A in['ResourceNotFoundException','ValidationException']:return\n\ 26 | \t\t\t\traise\n\t\t\texcept v:return\n\tdef V(props):\n\t\tfor J in E.get_paginator('list_certificates').paginate():\n\ 27 | \t\t\tfor B in J['CertificateSummaryList']:\n\t\t\t\tD(B);C={A[Q]:A[H]for\ 28 | \ A in E.list_tags_for_certificate(**{G:B[G]})[F]}\n\t\t\t\tif C.get(I+A1)==A[q]and\ 29 | \ C.get(I+A2)==A[Y]and C.get(I+A3)==hash(props):return B[G]\n\tdef h():\n\ 30 | \t\tif A.get(Z,L):raise T('Certificate not issued in time')\n\t\tA[Z]=O;D(A);K('lambda').invoke(FunctionName=c.invoked_function_arn,InvocationType='Event',Payload=d(A).encode())\n\ 31 | \tdef i():\n\t\twhile f()/1000>30:\n\t\t\tB=E.describe_certificate(**{G:A[C]})[r];D(B)\n\ 32 | \t\t\tif B[R]=='ISSUED':return O\n\t\t\telif B[R]==A4:raise T(B.get('FailureReason',P))\n\ 33 | \t\t\te(5)\n\t\treturn L\n\tdef x():B=M(A[s+J]);B.pop(F,X);C=M(A[J]);C.pop(F,X);return\ 34 | \ B!=C\n\tdef j():\n\t\tW='Type';V='Name';U='HostedZoneId';T='ValidationStatus';S='PENDING_VALIDATION';L='ResourceRecord'\n\ 35 | \t\tif B.get(o)!=A0:return\n\t\twhile O:\n\t\t\tI=E.describe_certificate(**{G:A[C]})[r];D(I)\n\ 36 | \t\t\tif I[R]!=S:return\n\t\t\tif not[A for A in I.get(N,[{}])if T not in\ 37 | \ A or L not in A]:break\n\t\t\tb.sleep(1)\n\t\tfor F in I[N]:\n\t\t\tif\ 38 | \ F[T]==S:M=k(F[p]);P=M.get(n,B.get(n));J=K('sts').assume_role(RoleArn=P,RoleSessionName=(r+A[q])[:64],DurationSeconds=900)['Credentials']if\ 39 | \ P is not X else{};Q=K('route53',aws_access_key_id=J.get('AccessKeyId'),aws_secret_access_key=J.get('SecretAccessKey'),aws_session_token=J.get('SessionToken')).change_resource_record_sets(**{U:M[U],'ChangeBatch':{'Comment':'Domain\ 40 | \ validation for '+A[C],'Changes':[{'Action':'UPSERT','ResourceRecordSet':{V:F[L][V],W:F[L][W],'TTL':60,'ResourceRecords':[{H:F[L][H]}]}}]}});D(Q)\n\ 41 | \tdef k(n):\n\t\tC='.';n=n.rstrip(C);D={A[p].rstrip(C):A for A in B[N]};A=n.split(C)\n\ 42 | \t\twhile len(A):\n\t\t\tif C.join(A)in D:return D[C.join(A)]\n\t\t\tA=A[1:]\n\ 43 | \t\traise T(N+' missing'+' for '+n)\n\thash=lambda v:t.new('md5',d(v,sort_keys=O).encode()).hexdigest()\n\ 44 | \tdef l():B=M(A[J].get(F,[]));B+=[{Q:I+A1,H:A[q]},{Q:I+A2,H:A[Y]},{Q:I+'stack-name',H:A[Y].split('/')[1]},{Q:I+A3,H:hash(A[J])}];E.add_tags_to_certificate(**{G:A[C],F:B})\n\ 45 | \tdef W():D(A);B=w.put(A['ResponseURL'],json=A,headers={'content-type':P});B.raise_for_status()\n\ 46 | \ttry:\n\t\ty=hash(A['RequestId']+A[Y]);B=A[J];E=K(A5,region_name=B.get(m));A[R]='SUCCESS'\n\ 47 | \t\tif A[a]=='Create':\n\t\t\tif A.get(Z,L)is L:A[C]=A6;g()\n\t\t\tj()\n\ 48 | \t\t\tif not i():return h()\n\t\telif A[a]==A7:\n\t\t\tif A[C]!=A6:\n\t\t\ 49 | \t\tif A[C].startswith('arn:'):U(A[C])\n\t\t\t\telse:U(V(B))\n\t\telif A[a]==A8:\n\ 50 | \t\t\tif x():\n\t\t\t\tD(A8)\n\t\t\t\tif V(B)==A[C]:\n\t\t\t\t\ttry:E=K(A5,region_name=A[A9].get(m));D(A7);U(V(A[A9]))\n\ 51 | \t\t\t\t\texcept:S(P)\n\t\t\t\t\treturn W()\n\t\t\t\tif A.get(Z,L)is L:g()\n\ 52 | \t\t\t\tj()\n\t\t\t\tif not i():return h()\n\t\t\telse:\n\t\t\t\tif F in\ 53 | \ A[s+J]:E.remove_tags_from_certificate(**{G:A[C],F:A[s+J][F]})\n\t\t\t\t\ 54 | l()\n\t\telse:raise T(A[a])\n\t\treturn W()\n\texcept Exception as z:S(P);A[R]=A4;A['Reason']=str(z);return\ 55 | \ W()" 56 | -------------------------------------------------------------------------------- /resources/dns-records.yml: -------------------------------------------------------------------------------- 1 | # See RecordSet: https://docs.aws.amazon.com/de_de/AWSCloudFormation/latest/UserGuide/aws-properties-route53-recordset.html 2 | # See AliasTarget: https://docs.aws.amazon.com/de_de/AWSCloudFormation/latest/UserGuide/aws-properties-route53-aliastarget.html 3 | # See https://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region (below the first table for hosted zone ids / website endpoints of S3) 4 | Resources: 5 | DnsRecord: 6 | Type: 'AWS::Route53::RecordSet' 7 | DependsOn: 8 | - HostedZone 9 | Properties: 10 | Comment: 'Alias CloudFront for ${opt:domain}' 11 | HostedZoneId: '#{HostedZone}' 12 | Type: A 13 | Name: '${opt:domain}' 14 | AliasTarget: 15 | # Generated domain name from CloudFront 16 | DNSName: '#{CFDistribution.DomainName}' 17 | # Default (static) hosted zone for CloudFront 18 | HostedZoneId: 'Z2FDTNDATAQYW2' 19 | WWWDnsRecord: 20 | Type: 'AWS::Route53::RecordSet' 21 | DependsOn: 22 | - HostedZone 23 | Properties: 24 | Comment: 'Alias CloudFront for www.${opt:domain}' 25 | HostedZoneId: '#{HostedZone}' 26 | Type: A 27 | Name: 'www.${opt:domain}' 28 | AliasTarget: 29 | # Generated domain name from CloudFront 30 | DNSName: '#{CFDistribution.DomainName}' 31 | # Default (static) hosted zone for CloudFront 32 | HostedZoneId: 'Z2FDTNDATAQYW2' -------------------------------------------------------------------------------- /resources/hosted-zone.yml: -------------------------------------------------------------------------------- 1 | # See https://docs.aws.amazon.com/de_de/AWSCloudFormation/latest/UserGuide/aws-resource-route53-hostedzone.html 2 | Resources: 3 | HostedZone: 4 | Type: 'AWS::Route53::HostedZone' 5 | Properties: 6 | HostedZoneConfig: 7 | Comment: 'Hosted zone for ${opt:domain}' 8 | Name: '${opt:domain}' -------------------------------------------------------------------------------- /resources/outputs.yml: -------------------------------------------------------------------------------- 1 | Outputs: 2 | CloudFrontDistributionId: 3 | Description: CloudFront distribution id 4 | Value: 5 | Ref: CFDistribution 6 | HostedZoneNameservers: 7 | Description: The nameservers for the Hosted Zone (to be used with your external DNS configuration) 8 | Value: 9 | 'Fn::Join': 10 | - ', ' 11 | - 'Fn::GetAtt': ['HostedZone', 'NameServers'] -------------------------------------------------------------------------------- /resources/s3-bucket.yml: -------------------------------------------------------------------------------- 1 | Resources: 2 | WebsiteBucket: 3 | Type: 'AWS::S3::Bucket' 4 | Properties: 5 | BucketName: ${opt:domain} 6 | -------------------------------------------------------------------------------- /resources/s3-policies.yml: -------------------------------------------------------------------------------- 1 | Resources: 2 | WebsiteBucketPolicy: 3 | Type: AWS::S3::BucketPolicy 4 | DependsOn: 5 | - WebsiteBucket 6 | Properties: 7 | Bucket: 8 | Ref: WebsiteBucket 9 | PolicyDocument: 10 | Statement: 11 | - Sid: PublicReadGetObject 12 | Effect: Allow 13 | Principal: 14 | AWS: 15 | Fn::Join: 16 | - ' ' 17 | - - 'arn:aws:iam::cloudfront:user/CloudFront Origin Access Identity' 18 | - '#{CloudFrontOriginAccessIdentity}' 19 | Action: 20 | - s3:GetObject 21 | Resource: 22 | - Fn::Join: [ 23 | '', [ 24 | 'arn:aws:s3:::', 25 | { 26 | 'Ref': 'WebsiteBucket' 27 | }, 28 | '/*' 29 | ] 30 | ] 31 | -------------------------------------------------------------------------------- /serverless.yml: -------------------------------------------------------------------------------- 1 | service: 2 | name: ${file(./modules/sanitizeStackName.js):sanitize} 3 | 4 | plugins: 5 | - serverless-s3-sync 6 | - serverless-pseudo-parameters 7 | - serverless-stack-output 8 | - serverless-cloudfront-invalidate 9 | 10 | custom: 11 | 12 | # The domain name to be used 13 | domainName: ${opt:domain} 14 | 15 | # Output plugin configuration 16 | output: 17 | handler: modules/output.handler 18 | 19 | # CloudFront invalidation plugin configuration 20 | cloudfrontInvalidate: 21 | distributionIdKey: 'CloudFrontDistributionId' 22 | items: # Add your files to invalidate here: 23 | - '/index.html' 24 | 25 | # S3 sync plugin configuration 26 | s3Sync: 27 | - bucketName: ${opt:domain} 28 | localDir: src 29 | 30 | provider: 31 | name: aws 32 | runtime: nodejs8.10 33 | region: ${opt:region, 'us-east-1'} 34 | 35 | resources: 36 | 37 | - ${file(resources/custom-acm-certificate-lambda.yml)} 38 | - ${file(resources/custom-acm-certificate-lambda-role.yml)} 39 | - ${file(resources/cloudfront-origin-access-identity.yml)} 40 | - ${file(resources/s3-bucket.yml)} 41 | - ${file(resources/s3-policies.yml)} 42 | - ${file(resources/hosted-zone.yml)} 43 | - ${file(resources/dns-records.yml)} 44 | - ${file(resources/certificate.yml)} 45 | - ${file(resources/cf-distribution.yml)} 46 | - ${file(resources/outputs.yml)} 47 | -------------------------------------------------------------------------------- /src/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |

Welcome to my static website!

4 | 5 | --------------------------------------------------------------------------------