はじめに CloudFront + S3 で静的な Web サイトをホスティングしている。 ステージング環境として同様のサイトを作ったが、超簡単なアクセス制限をかけるために BASIC 認証をかけたい。 こういったケースでは Lambda@Edge を使うのが主流のようだが、最近リリースされた CloudFront Functions を使ってやってみた。 手順 CloudFront Functions のサイドバーに Functions メニューがあるので、そこから以下の内容で Functions を登録する。 function handler(event) { var request = event.request; var headers = request.headers; // echo -n user:pass | base64 var authString = "Basic
![CloudFront + S3 + CloudFront Functions で BASIC 認証をかける](https://cdn-ak-scissors.b.st-hatena.com/image/square/c2d33507a2bff5fdcdad8d344baf6ad18f20309f/height=288;version=1;width=512/https%3A%2F%2Fres.cloudinary.com%2Fzenn%2Fimage%2Fupload%2Fs--Y9mPeQj0--%2Fc_fit%252Cg_north_west%252Cl_text%3Anotosansjp-medium.otf_55%3ACloudFront%252520%25252B%252520S3%252520%25252B%252520CloudFront%252520Functions%252520%2525E3%252581%2525A7%252520BASIC%252520%2525E8%2525AA%25258D%2525E8%2525A8%2525BC%2525E3%252582%252592%2525E3%252581%25258B%2525E3%252581%252591%2525E3%252582%25258B%252Cw_1010%252Cx_90%252Cy_100%2Fg_south_west%252Cl_text%3Anotosansjp-medium.otf_37%3Amallowlabs%252Cx_203%252Cy_121%2Fg_south_west%252Ch_90%252Cl_fetch%3AaHR0cHM6Ly9saDMuZ29vZ2xldXNlcmNvbnRlbnQuY29tL2EtL0FPaDE0R2dZNmRmWXRUcVloa0FyaXVZdWlELVlMNklPdFM0dlpXV21taUVOPXMyNTAtYw%3D%3D%252Cr_max%252Cw_90%252Cx_87%252Cy_95%2Fv1627283836%2Fdefault%2Fog-base-w1200-v2.png)