├── frontend ├── public │ ├── robots.txt │ ├── favicon.ico │ ├── logo192.png │ ├── logo512.png │ ├── manifest.json │ └── index.html ├── amplify │ ├── backend │ │ ├── hosting │ │ │ └── S3AndCloudFront │ │ │ │ ├── parameters.json │ │ │ │ └── template.json │ │ └── backend-config.json │ ├── .config │ │ └── project-config.json │ └── team-provider-info.json ├── src │ ├── App.test.js │ ├── index.css │ ├── index.js │ ├── App.css │ ├── App.js │ ├── logo.svg │ └── serviceWorker.js ├── config │ └── buildspec.dev.yml ├── .gitignore ├── package.json └── README.md ├── backend ├── .gitignore ├── handler.js ├── serverless.yml └── config │ └── buildspec.dev.yml ├── README.md └── LICENSE /frontend/public/robots.txt: -------------------------------------------------------------------------------- 1 | # https://www.robotstxt.org/robotstxt.html 2 | User-agent: * 3 | -------------------------------------------------------------------------------- /backend/.gitignore: -------------------------------------------------------------------------------- 1 | # package directories 2 | node_modules 3 | jspm_packages 4 | 5 | # Serverless directories 6 | .serverless -------------------------------------------------------------------------------- /frontend/amplify/backend/hosting/S3AndCloudFront/parameters.json: -------------------------------------------------------------------------------- 1 | { 2 | "bucketName": "frontend-20200730172413-hostingbucket" 3 | } -------------------------------------------------------------------------------- /frontend/public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AditModi/Building-CI-CD-pipeline-for-Serverless-Application/HEAD/frontend/public/favicon.ico -------------------------------------------------------------------------------- /frontend/public/logo192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AditModi/Building-CI-CD-pipeline-for-Serverless-Application/HEAD/frontend/public/logo192.png -------------------------------------------------------------------------------- /frontend/public/logo512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AditModi/Building-CI-CD-pipeline-for-Serverless-Application/HEAD/frontend/public/logo512.png -------------------------------------------------------------------------------- /frontend/amplify/backend/backend-config.json: -------------------------------------------------------------------------------- 1 | { 2 | "hosting": { 3 | "S3AndCloudFront": { 4 | "service": "S3AndCloudFront", 5 | "providerPlugin": "awscloudformation" 6 | } 7 | } 8 | } -------------------------------------------------------------------------------- /frontend/src/App.test.js: -------------------------------------------------------------------------------- 1 | test("fake one", () => { 2 | expect(true).toBeTruthy(); 3 | }); 4 | 5 | test("fake two", () => { 6 | expect(true).toBeTruthy(); 7 | }); 8 | 9 | test("fake three", () => { 10 | expect(true).toBeTruthy(); 11 | }); 12 | -------------------------------------------------------------------------------- /backend/handler.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | module.exports.listTodos = async event => { 3 | const todos = ["Wake up at 4AM", "Workout", "Eat breakfast","Attend Lec","Sleep"]; 4 | return { 5 | statusCode: 200, 6 | body: JSON.stringify({ todos }, null, 2) 7 | }; 8 | }; 9 | -------------------------------------------------------------------------------- /backend/serverless.yml: -------------------------------------------------------------------------------- 1 | service: todoApi 2 | provider: 3 | name: aws 4 | runtime: nodejs10.x 5 | 6 | functions: 7 | listTodos: 8 | handler: handler.listTodos 9 | events: 10 | - http: 11 | path: todo/list 12 | method: get 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Building-CI-CD-pipeline-for-Serverless-Application 2 | 3 | 4 | Todoapp is a simple Web Application designed to use AWS for CI/CD Pipeline Functionality using AWS CodePipeline, it also does UI testing using Ghost Inspector with the help of CodePipeline build stage. 5 | 6 | ![2020-07-30](https://user-images.githubusercontent.com/48589838/88926603-d8f23980-d293-11ea-80bd-3848df8eb19c.png) 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /frontend/src/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen", 4 | "Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue", 5 | sans-serif; 6 | -webkit-font-smoothing: antialiased; 7 | -moz-osx-font-smoothing: grayscale; 8 | } 9 | 10 | code { 11 | font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New", 12 | monospace; 13 | } 14 | -------------------------------------------------------------------------------- /frontend/src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import './index.css'; 4 | import App from './App'; 5 | import * as serviceWorker from './serviceWorker'; 6 | 7 | ReactDOM.render(, document.getElementById('root')); 8 | 9 | // If you want your app to work offline and load faster, you can change 10 | // unregister() to register() below. Note this comes with some pitfalls. 11 | // Learn more about service workers: https://bit.ly/CRA-PWA 12 | serviceWorker.unregister(); 13 | -------------------------------------------------------------------------------- /frontend/amplify/.config/project-config.json: -------------------------------------------------------------------------------- 1 | { 2 | "projectName": "frontend", 3 | "version": "3.0", 4 | "frontend": "javascript", 5 | "javascript": { 6 | "framework": "react", 7 | "config": { 8 | "SourceDir": "src", 9 | "DistributionDir": "build", 10 | "BuildCommand": "npm.cmd run-script build", 11 | "StartCommand": "npm.cmd run-script start" 12 | } 13 | }, 14 | 15 | "providers": [ 16 | "awscloudformation" 17 | ] 18 | } 19 | -------------------------------------------------------------------------------- /backend/config/buildspec.dev.yml: -------------------------------------------------------------------------------- 1 | version: 0.2 2 | 3 | phases: 4 | install: 5 | runtime-versions: 6 | nodejs: 10 7 | commands: 8 | - echo Installing serverless 9 | - npm install -g serverless 10 | - echo Installing todo API dependencies 11 | - cd backend 12 | - npm install --silent 13 | - echo Finished installing todo API dependencies 14 | build: 15 | commands: 16 | - echo Deploying todo API 17 | - serverless deploy 18 | - echo Finished deploying todo API 19 | 20 | cache: 21 | paths: 22 | - "backend/node_modules/**/*" 23 | -------------------------------------------------------------------------------- /frontend/public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "React App", 3 | "name": "Create React App Sample", 4 | "icons": [ 5 | { 6 | "src": "favicon.ico", 7 | "sizes": "64x64 32x32 24x24 16x16", 8 | "type": "image/x-icon" 9 | }, 10 | { 11 | "src": "logo192.png", 12 | "type": "image/png", 13 | "sizes": "192x192" 14 | }, 15 | { 16 | "src": "logo512.png", 17 | "type": "image/png", 18 | "sizes": "512x512" 19 | } 20 | ], 21 | "start_url": ".", 22 | "display": "standalone", 23 | "theme_color": "#000000", 24 | "background_color": "#ffffff" 25 | } 26 | -------------------------------------------------------------------------------- /frontend/src/App.css: -------------------------------------------------------------------------------- 1 | .App { 2 | text-align: center; 3 | } 4 | 5 | .App-logo { 6 | animation: App-logo-spin infinite 20s linear; 7 | height: 40vmin; 8 | pointer-events: none; 9 | } 10 | 11 | .App-header { 12 | background-color: #282c34; 13 | min-height: 100vh; 14 | display: flex; 15 | flex-direction: column; 16 | align-items: center; 17 | justify-content: center; 18 | font-size: calc(10px + 2vmin); 19 | color: white; 20 | } 21 | 22 | .App-link { 23 | color: #61dafb; 24 | } 25 | 26 | @keyframes App-logo-spin { 27 | from { 28 | transform: rotate(0deg); 29 | } 30 | to { 31 | transform: rotate(360deg); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /frontend/src/App.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from "react"; 2 | import axios from "axios"; 3 | import "./App.css"; 4 | 5 | class App extends Component { 6 | state = { 7 | todos: [] 8 | }; 9 | 10 | componentDidMount = async () => { 11 | const response = await axios.get("/todo/list"); 12 | this.setState({ 13 | todos: response.data.todos 14 | }); 15 | }; 16 | 17 | render() { 18 | return ( 19 |
20 |
21 | {this.state.todos.map(todo => ( 22 |

{todo}

23 | ))} 24 |
25 |
26 | ); 27 | } 28 | } 29 | 30 | export default App; 31 | -------------------------------------------------------------------------------- /frontend/config/buildspec.dev.yml: -------------------------------------------------------------------------------- 1 | version: 0.2 2 | 3 | phases: 4 | install: 5 | runtime-versions: 6 | nodejs: 10 7 | commands: 8 | - echo Installing Web dependencies 9 | - cd frontend 10 | - npm install --silent 11 | - echo Finished installing Web dependencies 12 | build: 13 | commands: 14 | - echo Running Unit Tests 15 | - npm run test 16 | - echo Building artifacts 17 | - npm run build 18 | - echo Deploying artifacts 19 | - npm run deploy-dev 20 | - echo Invalidating cloudfront cache 21 | - npm run cache-bust-dev 22 | 23 | cache: 24 | paths: 25 | - "frontend/node_modules/**/*" 26 | 27 | artifacts: 28 | files: 29 | - "**/*" 30 | base-directory: "frontend/build*" 31 | discard-paths: no 32 | -------------------------------------------------------------------------------- /frontend/amplify/team-provider-info.json: -------------------------------------------------------------------------------- 1 | { 2 | "dev": { 3 | "awscloudformation": { 4 | "AuthRoleName": "amplify-frontend-dev-172309-authRole", 5 | "UnauthRoleArn": "arn:aws:iam::780655064925:role/amplify-frontend-dev-172309-unauthRole", 6 | "AuthRoleArn": "arn:aws:iam::780655064925:role/amplify-frontend-dev-172309-authRole", 7 | "Region": "us-east-1", 8 | "DeploymentBucketName": "amplify-frontend-dev-172309-deployment", 9 | "UnauthRoleName": "amplify-frontend-dev-172309-unauthRole", 10 | "StackName": "amplify-frontend-dev-172309", 11 | "StackId": "arn:aws:cloudformation:us-east-1:780655064925:stack/amplify-frontend-dev-172309/40816460-d25b-11ea-8218-12e346dd5d0c", 12 | "AmplifyAppId": "d377x90brk7cq9" 13 | } 14 | } 15 | } -------------------------------------------------------------------------------- /frontend/.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # production 12 | /build 13 | 14 | # misc 15 | .DS_Store 16 | .env.local 17 | .env.development.local 18 | .env.test.local 19 | .env.production.local 20 | 21 | npm-debug.log* 22 | yarn-debug.log* 23 | yarn-error.log* 24 | 25 | #amplify 26 | amplify/\#current-cloud-backend 27 | amplify/.config/local-* 28 | amplify/backend/amplify-meta.json 29 | amplify/backend/awscloudformation 30 | build/ 31 | dist/ 32 | node_modules/ 33 | aws-exports.js 34 | awsconfiguration.json 35 | 36 | #amplify 37 | amplify/\#current-cloud-backend 38 | amplify/.config/local-* 39 | amplify/mock-data 40 | amplify/backend/amplify-meta.json 41 | amplify/backend/awscloudformation 42 | build/ 43 | dist/ 44 | node_modules/ 45 | aws-exports.js 46 | awsconfiguration.json 47 | amplifyconfiguration.json 48 | amplify-gradle-config.json 49 | amplifyxc.config -------------------------------------------------------------------------------- /frontend/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "frontend", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "@babel/runtime": "^7.6.0", 7 | "axios": "^0.21.2", 8 | "react": "^16.9.0", 9 | "react-dom": "^16.9.0", 10 | "react-scripts": "3.1.1" 11 | }, 12 | "scripts": { 13 | "start": "react-scripts start", 14 | "build": "react-scripts build", 15 | "deploy-dev": "aws s3 sync ./build/ s3://todoapi-dev-serverlessdeploymentbucket-a0a40ga4zgq5 --region us-east-1", 16 | "cache-bust-dev": "aws cloudfront create-invalidation --distribution-id E16WA9VBIAJIYZ --paths '/'", 17 | "test": "jest", 18 | "eject": "react-scripts eject" 19 | }, 20 | "eslintConfig": { 21 | "extends": "react-app" 22 | }, 23 | "browserslist": { 24 | "production": [ 25 | ">0.2%", 26 | "not dead", 27 | "not op_mini all" 28 | ], 29 | "development": [ 30 | "last 1 chrome version", 31 | "last 1 firefox version", 32 | "last 1 safari version" 33 | ] 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | 4 | Copyright (c) 2020 Adit Modi 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in all 14 | copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | SOFTWARE. 23 | -------------------------------------------------------------------------------- /frontend/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 12 | 13 | 17 | 18 | 27 | React App 28 | 29 | 30 | 31 |
32 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /frontend/src/logo.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /frontend/README.md: -------------------------------------------------------------------------------- 1 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). 2 | 3 | 4 | ## Available Scripts 5 | 6 | In the project directory, you can run: 7 | 8 | ### `npm start` 9 | 10 | Runs the app in the development mode.
11 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser. 12 | 13 | The page will reload if you make edits.
14 | You will also see any lint errors in the console. 15 | 16 | ### `npm test` 17 | 18 | Launches the test runner in the interactive watch mode.
19 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. 20 | 21 | ### `npm run build` 22 | 23 | Builds the app for production to the `build` folder.
24 | It correctly bundles React in production mode and optimizes the build for the best performance. 25 | 26 | The build is minified and the filenames include the hashes.
27 | Your app is ready to be deployed! 28 | 29 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. 30 | 31 | ### `npm run eject` 32 | 33 | **Note: this is a one-way operation. Once you `eject`, you can’t go back!** 34 | 35 | If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. 36 | 37 | Instead, it will copy all the configuration files and the transitive dependencies (Webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own. 38 | 39 | You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it. 40 | 41 | ## Learn More 42 | 43 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). 44 | 45 | To learn React, check out the [React documentation](https://reactjs.org/). 46 | 47 | ### Code Splitting 48 | 49 | This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting 50 | 51 | ### Analyzing the Bundle Size 52 | 53 | This section has moved here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size 54 | 55 | ### Making a Progressive Web App 56 | 57 | This section has moved here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app 58 | 59 | ### Advanced Configuration 60 | 61 | This section has moved here: https://facebook.github.io/create-react-app/docs/advanced-configuration 62 | 63 | ### Deployment 64 | 65 | This section has moved here: https://facebook.github.io/create-react-app/docs/deployment 66 | 67 | ### `npm run build` fails to minify 68 | 69 | This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify 70 | -------------------------------------------------------------------------------- /frontend/src/serviceWorker.js: -------------------------------------------------------------------------------- 1 | // This optional code is used to register a service worker. 2 | // register() is not called by default. 3 | 4 | // This lets the app load faster on subsequent visits in production, and gives 5 | // it offline capabilities. However, it also means that developers (and users) 6 | // will only see deployed updates on subsequent visits to a page, after all the 7 | // existing tabs open on the page have been closed, since previously cached 8 | // resources are updated in the background. 9 | 10 | // To learn more about the benefits of this model and instructions on how to 11 | // opt-in, read https://bit.ly/CRA-PWA 12 | 13 | const isLocalhost = Boolean( 14 | window.location.hostname === 'localhost' || 15 | // [::1] is the IPv6 localhost address. 16 | window.location.hostname === '[::1]' || 17 | // 127.0.0.1/8 is considered localhost for IPv4. 18 | window.location.hostname.match( 19 | /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/ 20 | ) 21 | ); 22 | 23 | export function register(config) { 24 | if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) { 25 | // The URL constructor is available in all browsers that support SW. 26 | const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href); 27 | if (publicUrl.origin !== window.location.origin) { 28 | // Our service worker won't work if PUBLIC_URL is on a different origin 29 | // from what our page is served on. This might happen if a CDN is used to 30 | // serve assets; see https://github.com/facebook/create-react-app/issues/2374 31 | return; 32 | } 33 | 34 | window.addEventListener('load', () => { 35 | const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`; 36 | 37 | if (isLocalhost) { 38 | // This is running on localhost. Let's check if a service worker still exists or not. 39 | checkValidServiceWorker(swUrl, config); 40 | 41 | // Add some additional logging to localhost, pointing developers to the 42 | // service worker/PWA documentation. 43 | navigator.serviceWorker.ready.then(() => { 44 | console.log( 45 | 'This web app is being served cache-first by a service ' + 46 | 'worker. To learn more, visit https://bit.ly/CRA-PWA' 47 | ); 48 | }); 49 | } else { 50 | // Is not localhost. Just register service worker 51 | registerValidSW(swUrl, config); 52 | } 53 | }); 54 | } 55 | } 56 | 57 | function registerValidSW(swUrl, config) { 58 | navigator.serviceWorker 59 | .register(swUrl) 60 | .then(registration => { 61 | registration.onupdatefound = () => { 62 | const installingWorker = registration.installing; 63 | if (installingWorker == null) { 64 | return; 65 | } 66 | installingWorker.onstatechange = () => { 67 | if (installingWorker.state === 'installed') { 68 | if (navigator.serviceWorker.controller) { 69 | // At this point, the updated precached content has been fetched, 70 | // but the previous service worker will still serve the older 71 | // content until all client tabs are closed. 72 | console.log( 73 | 'New content is available and will be used when all ' + 74 | 'tabs for this page are closed. See https://bit.ly/CRA-PWA.' 75 | ); 76 | 77 | // Execute callback 78 | if (config && config.onUpdate) { 79 | config.onUpdate(registration); 80 | } 81 | } else { 82 | // At this point, everything has been precached. 83 | // It's the perfect time to display a 84 | // "Content is cached for offline use." message. 85 | console.log('Content is cached for offline use.'); 86 | 87 | // Execute callback 88 | if (config && config.onSuccess) { 89 | config.onSuccess(registration); 90 | } 91 | } 92 | } 93 | }; 94 | }; 95 | }) 96 | .catch(error => { 97 | console.error('Error during service worker registration:', error); 98 | }); 99 | } 100 | 101 | function checkValidServiceWorker(swUrl, config) { 102 | // Check if the service worker can be found. If it can't reload the page. 103 | fetch(swUrl) 104 | .then(response => { 105 | // Ensure service worker exists, and that we really are getting a JS file. 106 | const contentType = response.headers.get('content-type'); 107 | if ( 108 | response.status === 404 || 109 | (contentType != null && contentType.indexOf('javascript') === -1) 110 | ) { 111 | // No service worker found. Probably a different app. Reload the page. 112 | navigator.serviceWorker.ready.then(registration => { 113 | registration.unregister().then(() => { 114 | window.location.reload(); 115 | }); 116 | }); 117 | } else { 118 | // Service worker found. Proceed as normal. 119 | registerValidSW(swUrl, config); 120 | } 121 | }) 122 | .catch(() => { 123 | console.log( 124 | 'No internet connection found. App is running in offline mode.' 125 | ); 126 | }); 127 | } 128 | 129 | export function unregister() { 130 | if ('serviceWorker' in navigator) { 131 | navigator.serviceWorker.ready.then(registration => { 132 | registration.unregister(); 133 | }); 134 | } 135 | } 136 | -------------------------------------------------------------------------------- /frontend/amplify/backend/hosting/S3AndCloudFront/template.json: -------------------------------------------------------------------------------- 1 | { 2 | "AWSTemplateFormatVersion": "2010-09-09", 3 | "Description": "Hosting resource stack creation using Amplify CLI", 4 | "Parameters": { 5 | "env": { 6 | "Type": "String" 7 | }, 8 | "bucketName": { 9 | "Type": "String" 10 | } 11 | }, 12 | "Conditions": { 13 | "ShouldNotCreateEnvResources": { 14 | "Fn::Equals": [ 15 | { 16 | "Ref": "env" 17 | }, 18 | "NONE" 19 | ] 20 | } 21 | }, 22 | "Resources": { 23 | "S3Bucket": { 24 | "Type": "AWS::S3::Bucket", 25 | "DeletionPolicy": "Retain", 26 | "Properties": { 27 | "BucketName": { 28 | "Fn::If": [ 29 | "ShouldNotCreateEnvResources", 30 | { 31 | "Ref": "bucketName" 32 | }, 33 | { 34 | "Fn::Join": [ 35 | "", 36 | [ 37 | { 38 | "Ref": "bucketName" 39 | }, 40 | "-", 41 | { 42 | "Ref": "env" 43 | } 44 | ] 45 | ] 46 | } 47 | ] 48 | }, 49 | "WebsiteConfiguration": { 50 | "IndexDocument": "index.html", 51 | "ErrorDocument": "index.html" 52 | }, 53 | "CorsConfiguration": { 54 | "CorsRules": [ 55 | { 56 | "AllowedHeaders": [ 57 | "Authorization", 58 | "Content-Length" 59 | ], 60 | "AllowedMethods": [ 61 | "GET" 62 | ], 63 | "AllowedOrigins": [ 64 | "*" 65 | ], 66 | "MaxAge": 3000 67 | } 68 | ] 69 | } 70 | } 71 | }, 72 | "PrivateBucketPolicy": { 73 | "Type": "AWS::S3::BucketPolicy", 74 | "DependsOn": "OriginAccessIdentity", 75 | "Properties": { 76 | "PolicyDocument": { 77 | "Id": "MyPolicy", 78 | "Version": "2012-10-17", 79 | "Statement": [ 80 | { 81 | "Sid": "APIReadForGetBucketObjects", 82 | "Effect": "Allow", 83 | "Principal": { 84 | "CanonicalUser": { 85 | "Fn::GetAtt": [ 86 | "OriginAccessIdentity", 87 | "S3CanonicalUserId" 88 | ] 89 | } 90 | }, 91 | "Action": "s3:GetObject", 92 | "Resource": { 93 | "Fn::Join": [ 94 | "", 95 | [ 96 | "arn:aws:s3:::", 97 | { 98 | "Ref": "S3Bucket" 99 | }, 100 | "/*" 101 | ] 102 | ] 103 | } 104 | } 105 | ] 106 | }, 107 | "Bucket": { 108 | "Ref": "S3Bucket" 109 | } 110 | } 111 | }, 112 | "OriginAccessIdentity": { 113 | "Type": "AWS::CloudFront::CloudFrontOriginAccessIdentity", 114 | "Properties": { 115 | "CloudFrontOriginAccessIdentityConfig": { 116 | "Comment": "CloudFrontOriginAccessIdentityConfig" 117 | } 118 | } 119 | }, 120 | "CloudFrontDistribution": { 121 | "Type": "AWS::CloudFront::Distribution", 122 | "DependsOn": [ 123 | "S3Bucket", 124 | "OriginAccessIdentity" 125 | ], 126 | "Properties": { 127 | "DistributionConfig": { 128 | "Origins": [ 129 | { 130 | "DomainName": { 131 | "Fn::GetAtt": [ 132 | "S3Bucket", 133 | "DomainName" 134 | ] 135 | }, 136 | "Id": "hostingS3Bucket", 137 | "S3OriginConfig": { 138 | "OriginAccessIdentity": { 139 | "Fn::Join": [ 140 | "", 141 | [ 142 | "origin-access-identity/cloudfront/", 143 | { 144 | "Ref": "OriginAccessIdentity" 145 | } 146 | ] 147 | ] 148 | } 149 | } 150 | } 151 | ], 152 | "Enabled": "true", 153 | "DefaultCacheBehavior": { 154 | "AllowedMethods": [ 155 | "DELETE", 156 | "GET", 157 | "HEAD", 158 | "OPTIONS", 159 | "PATCH", 160 | "POST", 161 | "PUT" 162 | ], 163 | "TargetOriginId": "hostingS3Bucket", 164 | "ForwardedValues": { 165 | "QueryString": "false" 166 | }, 167 | "ViewerProtocolPolicy": "redirect-to-https", 168 | "DefaultTTL": 86400, 169 | "MaxTTL": 31536000, 170 | "MinTTL": 60, 171 | "Compress": true 172 | }, 173 | "DefaultRootObject": "index.html", 174 | "CustomErrorResponses": [ 175 | { 176 | "ErrorCachingMinTTL": 300, 177 | "ErrorCode": 400, 178 | "ResponseCode": 200, 179 | "ResponsePagePath": "/" 180 | }, 181 | { 182 | "ErrorCachingMinTTL": 300, 183 | "ErrorCode": 403, 184 | "ResponseCode": 200, 185 | "ResponsePagePath": "/" 186 | }, 187 | { 188 | "ErrorCachingMinTTL": 300, 189 | "ErrorCode": 404, 190 | "ResponseCode": 200, 191 | "ResponsePagePath": "/" 192 | } 193 | ] 194 | } 195 | } 196 | } 197 | }, 198 | "Outputs": { 199 | "Region": { 200 | "Value": { 201 | "Ref": "AWS::Region" 202 | } 203 | }, 204 | "HostingBucketName": { 205 | "Description": "Hosting bucket name", 206 | "Value": { 207 | "Ref": "S3Bucket" 208 | } 209 | }, 210 | "WebsiteURL": { 211 | "Value": { 212 | "Fn::GetAtt": [ 213 | "S3Bucket", 214 | "WebsiteURL" 215 | ] 216 | }, 217 | "Description": "URL for website hosted on S3" 218 | }, 219 | "S3BucketSecureURL": { 220 | "Value": { 221 | "Fn::Join": [ 222 | "", 223 | [ 224 | "https://", 225 | { 226 | "Fn::GetAtt": [ 227 | "S3Bucket", 228 | "DomainName" 229 | ] 230 | } 231 | ] 232 | ] 233 | }, 234 | "Description": "Name of S3 bucket to hold website content" 235 | }, 236 | "CloudFrontDistributionID": { 237 | "Value": { 238 | "Ref": "CloudFrontDistribution" 239 | } 240 | }, 241 | "CloudFrontDomainName": { 242 | "Value": { 243 | "Fn::GetAtt": [ 244 | "CloudFrontDistribution", 245 | "DomainName" 246 | ] 247 | } 248 | }, 249 | "CloudFrontSecureURL": { 250 | "Value": { 251 | "Fn::Join": [ 252 | "", 253 | [ 254 | "https://", 255 | { 256 | "Fn::GetAtt": [ 257 | "CloudFrontDistribution", 258 | "DomainName" 259 | ] 260 | } 261 | ] 262 | ] 263 | } 264 | }, 265 | "CloudFrontOriginAccessIdentity": { 266 | "Value": { 267 | "Ref": "OriginAccessIdentity" 268 | } 269 | } 270 | } 271 | } --------------------------------------------------------------------------------