Skip to content

Instantly share code, notes, and snippets.

@tkuchiki
Last active August 2, 2017 01:48
Show Gist options
  • Save tkuchiki/88efd01f4e1d9f5c43f9a5b8dfe0c93e to your computer and use it in GitHub Desktop.
Save tkuchiki/88efd01f4e1d9f5c43f9a5b8dfe0c93e to your computer and use it in GitHub Desktop.
CloudFront + S3 で static website hosting のリダイレクトルールを使用するときの注意点 & API Gateway
  • CloudFront の origin を static website hosting のドメインにする(foobar.s3-website-ap-northeast-1.amazonaws.com みたいなやつ)
    • S3 の bucket を設定すると static website hosting ではないのでリダイレクトルールが適応されない
  • S3 の policy の Principal は "AWS": "*" でないと 403
  • redirect_rule.xml は 404 だったら your.domain にリダイレクト
    • path を維持したい場合は <ReplaceKeyWith/> を削除
    • https にしたければ <Redirect></Redirect><Protocol>https</Protocol> を追加
  • POST を受けたい場合は API Gateway + Lambda を使う
    • Origin を API Gateway の endpoint にする
    • (当然だが)Host ヘッダを透過させると動かない
<RoutingRules>
<RoutingRule>
<Condition>
<HttpErrorCodeReturnedEquals>404</HttpErrorCodeReturnedEquals>
</Condition>
<Redirect>
<HostName>your.domain</HostName>
<ReplaceKeyWith/>
</Redirect>
</RoutingRule>
</RoutingRules>
{
"Version": "2008-10-17",
"Id": "PolicyForCloudFrontStaticWebsiteHosting",
"Statement": [
{
"Sid": "1",
"Effect": "Allow",
"Principal": {
"AWS": "*"
},
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::your_bucket/*"
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment