官术网_书友最值得收藏!

Setting up static assets

Setting up an S3 bucket and CloudFront distribution to host static media isn't complicated and, in theory, we could add this to the Resources section of our serverless.yml file. The ability of Serverless to manage so many resources via CloudFormation is a slippery slope, since setting up systems can quickly become an exercise in learning and debugging CloudFormation. Another downside of a growing Resources section in the serverless.yml file is that deployments will take longer and longer. It's possible to only deploy application code during development, which results in single-digit second deployments; but when some system resource is updated, including environment variables, the entire CloudFormation stack needs to updated.

Rather than creating the S3 bucket and CloudFront distribution via serverless.yml, we can use a separate CloudFormation template designed just for this purpose. Another reason for splitting this out into a separate step is that this layer rarely changes. Once the CloudFront distribution is set up, there is a good chance you won't need to change anything for a very long time, if ever.

The following repository contains a CloudFormation template, a helper script, and documentation to set up a single - page web application on AWS:

https://github.com/verypossible/cloudfront-single-page-app

Again, you may read the details of this stack creation in the GitHub repository. After we choose one this stack with the necessary variables, we will end up with the following:

  • An S3 bucket, which will host all of our static content
  • A CloudFront distribution, which will pull and cache content from S3
  • A free TLS certificate for *.cupperslog.com
  • A Route53 record, which does the following:
    • Points https://cupperslog.com to the CloudFront distribution
    • Redirects any http:// traffic to https://
    • Caches static assets for 10 minutes:
brianz@gold(master=)$ AWS_DEFAULT_REGION=us-east-1 ./manage-stack.sh create --domain cupperslog.com --zone-id ZNGYF5FXIUL0Z --name cupperslog 
{
"StackId": "arn:aws:cloudformation:us-east-1:875406499000:stack/cupperslog/e7f15a50-b03c-11e7-97b0-5001ff34b4a6"
}

CloudFront distributions can take from several minutes to a couple of hours to be created, which is another good reason that we're doing this separate from our application code and database. Once finished, all that is required is uploading static assets to the S3 bucket that CloudFormation created for you. Ensure that the access control policy is set to public-read since this is a publicly accessible website. Uploading is accomplished via many tools, the AWS CLI tool being one of them:

brianz@gold$ aws s3 cp \
build/ \
--acl public-read \
--recurisve \
s3://cupperslog-s3bucketforstaticcontent-nswgo5ega4r1
Other tools I've used for S3 file management are Cyberduck for OS X, S3 Organizer for Firefox, and the regular AWS web interface. They all do more or less the same thing, so pick what works for you.

For our example application, the frontend will consist of a simple React application that allows users to do the following:

  • List coffee cupping sessions
  • View coffee cupping session details
  • Create a new coffee cupping session
  • Delete a coffee cupping session

It should be clear that there is no authentication and no notion of a user in these examples. This application was built to demonstrate a serverless pattern, and even critical details such as user authentication and authorization wouldn't fit in this single chapter.

主站蜘蛛池模板: 历史| 凤翔县| 宣化县| 海宁市| 越西县| 民勤县| 清水县| 安仁县| 文昌市| 仪陇县| 宁乡县| 沐川县| 新昌县| 嘉善县| 台中市| 扶风县| 潜江市| 永春县| 承德县| 柳州市| 林西县| 江达县| 定兴县| 永川市| 涡阳县| 临夏市| 南充市| 肥乡县| 天等县| 阿尔山市| 浑源县| 兰考县| 景东| 南陵县| 马尔康县| 昭苏县| 砀山县| 伊吾县| 龙南县| 元阳县| 阳山县|