0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

CloudFrontのx-edge-request-idをAPI側に渡す方法

Posted at

概要

データを分析するにあたってエッジロケーションからのリクエスト単位で集計をしたく、CloudFrontのログに出力されているx-edge-request-idを使用するという事になった

x-edge-request-idはレスポンスヘッダではx-amz-cf-idとして出力されるのでてっきりOriginにもx-amz-cf-idに同じ値が格納されていると思っていた
しかし検証した結果、OriginでCloudFrontからのリクエストを受け取った際にx-amz-cf-idはあるにはあるが
ヘッダに格納されているx-amz-cf-idの値がx-edge-request-idとは異なる値になっていた

これを解消するために調査した結果を記しておく

結論

Lambda@EdgeをOrigin Requestに設置し、リクエストを中継する
Originに渡されるx-amz-cf-idがx-edge-request-idと同じになる

Lambda@Edgeのサンプルコード

event.py
import json

def lambda_handler(event, context):
    # TODO implement
    print('####lambda edge start###')
    print('####event###')
    print(event)
    print('####lambda edge end###')
    print('####context###')
    print(context)
    cf_request = event['Records'][0]['cf']['request']    
    return cf_request

その他

他の方法としてはOrigin RequestのLambda@Edgeで別のヘッダにrequestId(x-edge-request-idのLambda@Edgeのkey)をコピーする方法がある

なんでこんな挙動なのかは諸事情によりAWSの技術サポートに聞くことができず、知ってる人いたら教えてください

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?