1
1

More than 3 years have passed since last update.

Cloudfront+LambdaEdgeでリダイレクトを行う。

Posted at

こちらに書いてあるサンプルをそのまま載せてます…

HTTP リダイレクトの生成 (生成されたレスポンス)

lambdaはバージニア北部で作ります。
Pythonは3.8です(3.9で作ったらCloudfrontが対応してないって言われました。)

ロールは新たに作ります。
[基本エッジ Lambda のアクセス許可]を指定。

import json

def lambda_handler(event, context):
    print("event:{}".format(event))
    print("context:{}".format(context))
     # Generate HTTP redirect response with 302 status code and Location header.

    response = {
     'status': '302',
     'statusDescription': 'Found',
     'headers': {
         'location': [{
             'key': 'Location',
             'value': 'http://docs.aws.amazon.com/lambda/latest/dg/lambda-edge.html'
         }]
     }
    }

    return response

こちらをDeployし、バージョンを発行します。
Cloudfrontのビヘイビアに設定します。

スクリーンショット 2021-09-03 15.21.12.png

lambdaのコンソールでARNをコピーしましょう
arn:aws:lambda:us-east-1:99999999:function:EdgeTest:1
※バージョンも指定しないと駄目みたいです。

これでCloudfrontにアクセスするとリダイレクトされます。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1