{"componentChunkName":"component---src-templates-lesson-js","path":"/phoenix-chat/ssl-and-cloudfront","result":{"data":{"mdx":{"body":"function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }\n\nfunction _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }\n\nfunction _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }\n\n/* @jsx mdx */\nvar _frontmatter = {\n  \"title\": \"SSL and CloudFront\",\n  \"order\": 53,\n  \"free\": false,\n  \"active\": true,\n  \"tech\": [\"cloudfront\"]\n};\n\nvar makeShortcode = function makeShortcode(name) {\n  return function MDXDefaultShortcode(props) {\n    console.warn(\"Component \" + name + \" was not imported, exported, or provided by MDXProvider as global scope\");\n    return mdx(\"div\", props);\n  };\n};\n\nvar layoutProps = {\n  _frontmatter: _frontmatter\n};\nvar MDXLayout = \"wrapper\";\nreturn function MDXContent(_ref) {\n  var components = _ref.components,\n      props = _objectWithoutProperties(_ref, [\"components\"]);\n\n  return mdx(MDXLayout, _extends({}, layoutProps, props, {\n    components: components,\n    mdxType: \"MDXLayout\"\n  }), mdx(\"h1\", null, \"SSL and CloudFront\"), mdx(\"ul\", null, mdx(\"li\", {\n    \"parentName\": \"ul\"\n  }, \"Getting an SSL certificate\"), mdx(\"li\", {\n    \"parentName\": \"ul\"\n  }, \"Setting up CloudFront\"), mdx(\"li\", {\n    \"parentName\": \"ul\"\n  }, \"Cache Invalidation\"), mdx(\"li\", {\n    \"parentName\": \"ul\"\n  }, \"Domain names with Route 53\")), mdx(\"p\", null, \"While Heroku automatically gives us an encrypted connection to our API via \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"HTTPS\"), \" (which is just a normal \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"HTTP\"), \" connection plus an \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"SSL\"), \" security layer--people will use the two terms interchangeably), S3 does not. That's because there's nothing sitting between the user and the files.\"), mdx(\"p\", null, \"The only way to solve this problem is to 1) set up a Node server to handle requests and run this on EC2, or 2) run our requests through a proxy like CloudFront. Since we're going to need to communicate to our backend by a secure route, we're going to need to set up CloudFront and route our connections through it.\"), mdx(\"p\", null, \"We will also need an SSL certificate. This used to be a long, tedious, and painful process that often cost hundreds of dollars a year (Godaddy still charges \\\\$299/yr for it), but thanks to AWS, you get it for free and it's super easy to set up!\"), mdx(\"h2\", null, \"Getting an SSL certificate\"), mdx(\"p\", null, \"As mentioned above, this used to be complicated and expensive, but now it's really easy. Log in to \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"aws.amazon.com\"), \" and open up the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Certificate Manager\"), \". Click the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Request a certificate\"), \" button in the top left.\"), mdx(\"p\", null, \"Then you want to request what is called a \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"wildcard\"), \" certificate and a naked domain certificate. That gives you the ability to protect all the single-level sub-domains of a particular site, so it would cover \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"www.phoenixchat.io\"), \", \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"api.phoenixchat.io\"), \", etc. Assuming the domain is \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"phoenixchat.io\"), \", the requested domains would be \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"*.phoenixchat.io\"), \" and \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"phoenixchat.io\"), \". Click \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Review and request\"), \" and \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Confirm and request\"), \".\"), mdx(\"p\", null, \"It often takes a few minutes to validate, but that's it. You're done.\"), mdx(\"h2\", null, \"Setting up CloudFront\"), mdx(\"p\", null, \"CloudFront is something you can put in front of your app to make load times faster and enable HTTPS. It does aggressive caching in certain locations around the world so your app loads faster since your app won't have to request assets every time the page loads.\"), mdx(\"p\", null, \"Within \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"CloudFront\"), \", click on \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Create Distribution\"), \" followed by \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Create\"), \" under the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Web\"), \" section. From \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Origin Domain Name\"), \" you can select the S3 bucket you just made from the dropdown. Make sure you add domain you would like to reference in the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Alternate Domain Names\"), \" section (e.g. phoenixchat.io). Change \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Viewer Protocol Policy\"), \" to \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Redirect HTTP to HTTPS\"), \" and add \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"index.html\"), \" as the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Default Root Object\"), \". Then under \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"SSL Certificate\"), \", choose \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Custom SSL Certificate\"), \" and select the certificate you just created. Then create the distribution.\"), mdx(\"p\", null, \"This will take a while to provision (often minutes to hours). While we're waiting, we can explain how cache invalidation works.\"), mdx(\"h2\", null, \"Cache Invalidation\"), mdx(\"p\", null, \"Cache invalidation is a big subject and an especially complicated one. Fortunately for us, our use case is pretty simple. Because CloudFront caches files based on their names, if we make a change to \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"bundle.js\"), \", CloudFront doesn't know that it needs to clear the cache and bring in the new version of the app. The act of clearing this cache is called an \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"invalidation\"), \".\"), mdx(\"p\", null, \"Since we're injecting our HTML file with hashed bundle and style files, all we have to do is tell CloudFront not to cache our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"index.html\"), \" file, since every time we update the app, our bundle and style files will have a new hashed suffix.\"), mdx(\"p\", null, \"The way to tell CloudFront to never cache the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"index.html\"), \" file is by setting a new behavior. Select the distribution by clicking on the ID. Then go to the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Behaviors\"), \" and create a new behavior. Add \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"index.html\"), \" to the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Path Pattern\"), \". Then change \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Object Caching\"), \" to \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Customize\"), \", and set the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Maximum\"), \" and \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Default\"), \" TTL (Time To Live--which is how long the object is cached) to \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"0\"), \".\"), mdx(\"h2\", null, \"Domain names with Route 53\"), mdx(\"p\", null, \"If you've purchased your domain from another provider, your setup is going to be different. Assuming you used Amazon's \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Route 53\"), \", this is how you would point your domain to the CloudFront distribution you just created.\"), mdx(\"p\", null, \"After purchasing a domain, go to \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Hosted zones\"), \" in the left panel and select \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Create hosted zone\"), \" for the domain you would like to change.\"), mdx(\"p\", null, \"Now choose \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Create record set\"), \". Then in the panel on the right, toggle the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Alias\"), \" to \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Yes\"), \" and select the CloudFront distribution you just created. Keep in mind that the distribution might not be provisioned yet, in which case you'll have to wait (if you go to CloudFront and it says \\\"in progress\\\", it isn't done). Then click \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Create\"), \" and you're done. Now if you go to the domain you purchased (e.g. \", mdx(\"a\", {\n    \"href\": \"https://phoenixchat.io\",\n    \"parentName\": \"p\"\n  }, \"https://phoenixchat.io\"), \") it should look like you expect with HTTPS enabled.\"), mdx(\"p\", null, mdx(\"em\", {\n    \"parentName\": \"p\"\n  }, \"Keep in mind, these changes can often take hours to propagate.\"), \" If your changes don't immediately appear, don't automatically assume there is an error. Unfortunately there is no way around this and it makes your code really difficult to debug.\"));\n}\n;\nMDXContent.isMDXComponent = true;","excerpt":"SSL and CloudFront Getting an SSL certificate Setting up CloudFront Cache Invalidation Domain names with Route 53 While Heroku automatically…","frontmatter":{"title":"SSL and CloudFront","order":53,"active":true,"free":false,"tech":["cloudfront"]}}},"pageContext":{"slug":"ssl-and-cloudfront","prevNode":{"frontmatter":{"title":"ESLint and Airbnb Style Guide","active":true,"free":false,"order":52,"tech":["react","eslint"]},"fields":{"slug":"eslint-and-airbnb-style-guide"}},"nextNode":{"frontmatter":{"title":"Refactoring Redux","active":true,"free":false,"order":54,"tech":["react","redux"]},"fields":{"slug":"refactoring-redux"}}}},"staticQueryHashes":["1723017916"]}