LoginSignup
0
0

More than 1 year has passed since last update.

CDKでCloudFront Functionが見つからないエラーの対処

Posted at

問題

CDKでCloudFront Functionを作成した状態で、別のリソースを変更した際に下記のエラーが出た。

12:02:49 | UPDATE_FAILED        | AWS::CloudFront::Function                       | mycloudfrontfunction911B181C
Resource handler returned message: "Resource of type 'AWS::CloudFront::Function' with identifier 'ap-northeast-1MyCloudFrontFunction6AA8AF07' was not found." (RequestToken: 74d6dbad-01af-c5ca-985b-4091c1eddd4a, HandlerErrorCode: NotFound)

NotFound?CloudFront Functionの変更は行っていないのに何故だろうと思って調べると下記のissueを見つけた。

(aws-cloudfront): Unexpected diffs caused by cloudfront.Function #15523

どうやらCDKのバグで、CloudFront FunctionのID生成に問題があることが分かった。

回避策

回避策としては、CloudFront FunctionのID生成はCDKに任せず明示的に指定してやれば良い。

issueに記載されていたjsの実装方法を参考にしながらPythonで実装した。

from aws_cdk import (
    Duration,
    Stack,
    aws_cloudfront as cloudfront,
)

class MyPythonCDKStack(Stack):
    def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)

cf_function_origin_redirect_id = f"originredirect{self.node.addr}"[:55] # CDK で生成するIDの長さが55文字だったため同様に指定した
cf_function_origin_redirect = cloudfront.Function(
    self,
    cf_function_origin_redirect_id, # 明示的に作成したIDを渡す
    code=cloudfront.FunctionCode.from_file(
        file_path="cloudfront_functions/origin_redirect/index.js"
    ),
    function_name=cf_function_origin_redirect_id,
)

生成するIDの文字数が長すぎると下記のエラーが発生する。上記の55文字がオススメ。

Resource handler returned message: "Invalid request provided: 2 validation errors detected: Value 'origin_redirect_func_dev_xxxxxxxx
xxx_c8f3291eab8ea63782c49ef250e1f4f76e4c61ea94' at 'name' failed to satisfy constraint: Member must have length less than or equal t
o 64; Value 'origin_redirect_func_dev_xxxxxxxxxxx_c8f3291eab8ea63782c49ef250e1f4f76e4c61ea94' at 'name' failed to satisfy constraint
: Member must satisfy regular expression pattern: ^[a-zA-Z0-9-_]{1,64}$ (Service: CloudFront, Status Code: 400, Request ID: 01d2d320
-abb6-425e-bba8-78d77d3f9700, Extended Request ID: null)" (RequestToken: 3e13c565-4c8f-55e8-d4e2-086ff2791650, HandlerErrorCode: Inv
alidRequest)
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