aws s3 静态网站_如何使用AWS S3,CloudFront和命令行管理静态网站

aws s3 静态网站

by Ben Cheng


如何使用AWS S3,CloudFront和命令行管理静态网站 (How to manage your static websites with AWS S3, CloudFront, and a command line)

Here’s a short list of things you shouldn’t need to worry about when setting up a static website:


  • scaling up your servers for surges in traffic

  • logging into the AWS Management Console just to upload a new index.html file

  • spending US$5 to host a single static website


I myself was sick of worrying about these things. So I learned some Go, developed a small command line tool over the weekend, and open-sourced it.

我自己已经担心这些事情了。 因此,我学习了一些Go语言,在周末开发了一个小型命令行工具,并将其开源。

AWS S3 is an affordable option for for hosting (and free for first time users), and AWS CloudFront is good for CDN. But setting up the two is a pain. The checklist is pretty long:

AWS S3是负担得起的主机托管选项(对于首次使用的用户免费),AWS CloudFront非常适合CDN。 但是设置两者是很痛苦的。 清单很长:

  1. set up S3 correctly

  2. configure CloudFront

  3. make sure you’ve followed best practices such as HTTP -> HTTPS redirection

    确保您已遵循最佳实践,例如HTTP-> HTTPS重定向
  4. sync files

  5. invalidate CloudFront for updates.


Luckily, my open source AWS-site-manager makes hosting and updating a static site as simple as a single command line.


AWS Site Manager is a simple command-line tool that makes it easy to host a static website with AWS S3 and CloudFront, without running afoul of best practices.

AWS Site Manager是一个简单的命令行工具,可轻松使用AWS S3和CloudFront托管静态网站,而无需遵循最佳实践。

If this is your first time hosting a site, create an AWS account and register your domain name with a service such as Namecheap (make sure you use their monthly discount coupon).


为什么将S3和CloudFront用于静态站点? (Why use S3 and CloudFront for static sites?)

So given all the trouble, why bother with S3 and CloudFront? The two main reasons are:

因此,考虑到所有麻烦,为什么还要麻烦S3和CloudFront? 两个主要原因是:

  1. Affordability for features (compared to free Heroku dyno — which shuts down when you reach their inactivity limit — or — which doesn’t support HTTPS with custom domains)

    功能的可承受性(与免费的Heroku dyno(当您达到其不活动限制时会关闭)或不支持带有自定义域的HTTPS相比)
  2. Speed (CloudFront is a cheap, but acceptable CDN)


Given the advantages, the only barrier was the setup, so we created a command line tool for convenient future usage. Below, I’ll walk you through how to set up this open-source site manager.

有了这些优点,唯一的障碍就是设置,因此我们创建了命令行工具以方便将来使用。 下面,我将引导您完成如何设置此开源站点管理器的操作。

AWS-site-manager做两件事: (AWS-site-manager does 2 things:)

1.使用确定的配置设置S3和CloudFront: (1. Setup S3 and CloudFront with an opinionated configuration:)

  • Create S3 buckets

  • Configure CloudFront Distribution and set up CNAME

  • Upload and set custom HTTPS certs

  • Redirect www to naked domains for better SEO

  • Redirect HTTP to HTTPS for better SEO (if HTTPS is enabled)

  • Set up IAM / let’s encrypt cert automatically (coming soon)

    设置IAM /让我们自动加密证书(即将推出)

2.与S3同步本地文件夹 (2. Sync a local folder with S3)

  • Gzip files for better site speed (will replace with CloudFront Gzip Setting)

    Gzip文件可提高站点速度(将替换为CloudFront Gzip设置)

  • Sensible default for HTTP Header (correct MIME type, max-age=900, etag etc)

    HTTP标头的明智默认值(正确的MIME类型,max-age = 900,etag等)
  • Invalidate CloudFront distribution for files synced


入门:安装 (Getting started: Install)

Download the binary for Linux / Mac / Windows release here.

此处下载适用于Linux / Mac / Windows 版本的二进制文件。

Or compile from sources: If you have Go 1.6 or above installed, run the following command:

或从源代码进行编译:如果已安装Go 1.6或更高版本 ,请运行以下命令:

如何使用AWS Site Manager (How to Use AWS Site Manager)

1.设置AWS凭证和配置。 (1. Set up AWS Credentials and config.)

If you haven’t set up AWS credentials on your environment before, you can set it up by putting the following lines in ~/.aws/credentials.


And in ~/.aws/config


You should also set the environment variable of AWS_SDK_LOAD_CONFIG If you’re on Linux / Mac using bash put the following line in ~/.bashrc

您还应该设置AWS_SDK_LOAD_CONFIG的环境变量。如果您使用bash在Linux / Mac上,请将以下行放入~/.bashrc

You can read more about AWS CLI set up on its official documentation.

您可以在其官方文档中阅读有关AWS CLI设置的更多信息。

2.使用它! (2. Use it!)

Assuming you’re going to set up a website and, you can:


The commands above will:


If you want to use https, and have the cert in PEM format ready, run the following command lines instead to setup HTTPS. (if your SSL registry sent you .key / .crt, read this)

如果要使用https,并准备好PEM格式的证书,请运行以下命令行来设置HTTPS。 (如果您的SSL注册表向您发送了.key / .crt请阅读此内容 )

Finally, you need to set up DNS mapping to your CloudFront distribution endpoint. Consider using AWS Route-53 for that.

最后,您需要设置到CloudFront分发端点的DNS映射。 考虑为此使用AWS Route-53

You need to setup a CNAME record to point your domain name to the Cloud Front Distribution. You can get your CloudFront Domain Name from AWS Management Console, then set your domain’s CNAME to the Domain Name.

您需要设置一个CNAME记录,以将您的域名指向Cloud Front Distribution。 您可以从AWS管理控制台获取CloudFront域名,然后将域的CNAME设置为域名。

If you need to update the site, just run aws-site-manager sync –domain In the folder and the command will compare the file changes, upload new files to S3, and invalidate the CloudFront cache for you.

如果您需要更新站点,只需运行aws-site-manager sync –domain example.com在文件夹中,该命令将比较文件更改,将新文件上传到S3,并为您使CloudFront缓存无效。

继续发射 (Go forth and launch)

This project solved a recurring problem for me and my team at Oursky. In addition to building mobile and web apps, we also often create one-off static sites that we want to be able to conveniently update every once in a while.

这个项目为我和我的Oursky团队解决了一个反复出现的问题。 除了构建移动和Web应用程序之外,我们还经常创建一个一次性的静态站点,我们希望该站点能够方便地每隔一段时间进行一次更新。

AWS-site-manager is open source and in its preliminary stages. If you want to contribute, you can create issues or submit a pull request.

AWS-site-manager是开源的,并且处于初始阶段。 如果您想做出贡献,则可以创建问题或提交请求请求。

If you have any questions about this project or serverless solutions in general, you can contact me here or on Twitter. Or, if you’re ever dropping by Hong Kong, come visit my office for a chat!

如果您对此项目或无服务器解决方案总体上有任何疑问,可以在此处或在Twitter上与我联系。 或者,如果您曾经来过香港,请来我的办公室聊天!



PS: My company’s also working on another open-sourced project called, which is a free BaaS that includes features such as chat, social networks, bots, social logins, real-time and offline data sync, CMS for mobile, user management, etc.



aws s3 静态网站