- 公開日
- 最終更新日
Lambda@Edge削除をCDKで試みたらDELETE FAILEDした
この記事を共有する

はじめに
パーソル&サーバーワークスの嶋田です。
CloudFrontとLambda@Edgeをセットで使う機会があり、CDKで検証用環境をデプロイしました。
検証を終え、cdk destroy でリソースの後片付けを行った際に、事件は起こりました。
DELETE FAILED の登場です。
本事象に遭遇して調査を行ったので、そのときのまとめを本ブログにて紹介します。
結論
- Lambda@Edgeの Replica削除には数時間程度の時間を要するため、時間をあけて再実行 する
- Replicaとは、エッジロケーションに配備されたLambda@EdgeのLambda関数のこと
DELETE FAILED 時の出力
ApiGatewayThrottlingDemoStack | 26 | 10:13:28 AM | DELETE_IN_PROGRESS | AWS::Lambda::Function | EdgeFunction/Fn (EdgeFunctionFn44308ADF) ApiGatewayThrottlingDemoStack | 26 | 10:13:29 AM | DELETE_FAILED | AWS::Lambda::Function | EdgeFunction/Fn (EdgeFunctionFn44308ADF) Resource handler returned message: "Lambda was unable to delete arn:aws:lambda:us-east-1:012345678910:function:ApiGatewayThrottlingDemoSta-EdgeFunctionFn44308ADF-m3VImAwU5LxI:1 because it is a replicated function. Please see our documentation for Deleting Lambda@Edge Functions and Replicas. (Service: Lambda, Status Code: 400, Request ID: 62994eb7-1e23-4c66-91d3-08992d3c7b15)" (RequestToken: 9e10d5f1-7ab2-d79b-fb07-fdea569bcb16, HandlerErrorCode: InvalidRequest) ApiGatewayThrottlingDemoStack | 26 | 10:13:29 AM | DELETE_FAILED | AWS::CloudFormation::Stack | ApiGatewayThrottlingDemoStack The following resource(s) failed to delete: [EdgeFunctionFn44308ADF]. Failed resources: ApiGatewayThrottlingDemoStack | 10:13:29 AM | DELETE_FAILED | AWS::Lambda::Function | EdgeFunction/Fn (EdgeFunctionFn44308ADF) Resource handler returned message: "Lambda was unable to delete arn:aws:lambda:us-east-1:012345678910:function:ApiGatewayThrottlingDemoSta-EdgeFunctionFn44308ADF-m3VImAwU5LxI:1 because it is a replicated function. Please see our documentation for Deleting Lambda@Edge Functions and Replicas. (Service: Lambda, Status Code: 400, Request ID: 62994eb7-1e23-4c66-91d3-08992d3c7b15)" (RequestToken: 9e10d5f1-7ab2-d79b-fb07-fdea569bcb16, HandlerErrorCode: InvalidRequest) ❌ ApiGatewayThrottlingDemoStack: destroy failed _ToolkitError: The stack named ApiGatewayThrottlingDemoStack is in a failed state. You may need to delete it from the AWS console : DELETE_FAILED (The following resource(s) failed to delete: [EdgeFunctionFn44308ADF]. ) at destroyStack (/home/ec2-user/.anyenv/envs/nodenv/versions/20.17.0/lib/node_modules/aws-cdk/lib/index.js:142573:11) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async CdkToolkit.destroy (/home/ec2-user/.anyenv/envs/nodenv/versions/20.17.0/lib/node_modules/aws-cdk/lib/index.js:338102:13) at async exec3 (/home/ec2-user/.anyenv/envs/nodenv/versions/20.17.0/lib/node_modules/aws-cdk/lib/index.js:345533:12) { type: 'toolkit' } The stack named ApiGatewayThrottlingDemoStack is in a failed state. You may need to delete it from the AWS console : DELETE_FAILED (The following resource(s) failed to delete: [EdgeFunctionFn44308ADF]. )
上記ログより、エラーメッセージを抜粋
Resource handler returned message: "Lambda was unable to delete arn:aws:lambda:us-east-1:012345678910:function:ApiGatewayThrottlingDemoSta-EdgeFunctionFn44308ADF-m3VImAwU5LxI:1 because it is a replicated function. Please see our documentation for Deleting Lambda@Edge Functions and Replicas.
エラーメッセージをそのまま翻訳します。
リソースハンドラがメッセージを返しました: 「arn:aws:lambda:us-east-1:012345678910:function:ApiGatewayThrottlingDemoSta-EdgeFunctionFn44308ADF-m3VImAwU5LxI:1は複製された関数であるため、Lambdaは削除できませんでした。Lambda@Edge 関数とレプリカの削除に関するドキュメントを参照してください。
ドキュメントを参照する
Lambda@Edge 関数とレプリカを削除する
こちらのドキュメントを参照してみると
レプリカは通常、数時間以内に削除されます。Lambda@Edge 関数のレプリカを手動で削除することはできません。これにより、まだ使用中のレプリカが削除されてエラーが発生する状況を防ぐことができます。
と記載があり、仕様のようです。
しばらく経過して
これは長期戦か? と腹をくくって待つこと30分程度、リソースの削除を再度実施してみると、今度は無事に削除できました。
$ cdk destroy --force ApiGatewayThrottlingDemoStack: destroying... [1/1] ApiGatewayThrottlingDemoStack | 0 | 10:58:42 AM | DELETE_IN_PROGRESS | AWS::CloudFormation::Stack | ApiGatewayThrottlingDemoStack User Initiated ApiGatewayThrottlingDemoStack | 0 | 10:58:44 AM | DELETE_IN_PROGRESS | AWS::Lambda::Function | EdgeFunction/Fn (EdgeFunctionFn44308ADF) ApiGatewayThrottlingDemoStack | 1 | 10:58:48 AM | DELETE_COMPLETE | AWS::Lambda::Function | EdgeFunction/Fn (EdgeFunctionFn44308ADF) ApiGatewayThrottlingDemoStack | 1 | 10:58:48 AM | DELETE_IN_PROGRESS | AWS::IAM::Role | EdgeFunction/Fn/ServiceRole (EdgeFunctionFnServiceRoleC7B72E46) ✅ ApiGatewayThrottlingDemoStack: destroyed
めでたしめでたし。お疲れさまでした。
この記事は私が書きました
嶋田 龍登
記事一覧インフラからアプリのことまでなんでもやりたい、フルスタックを目指すエンジニア
