CloudFormation はカスタムリソースのドリフトを検出しますか?

CloudFormation はカスタムリソースのドリフトを検出しますか?

2025.08.07

困っていた内容

CloudFormation でカスタムリソースを構築後にカスタムリソース側に変更を加えましたが、CloudFormation のドリフトで検出されませんでした。
CloudFormation はカスタムリソースのドリフトを検出しますか?

どう対応すればいいの?

いいえ、CloudFormation はカスタムリソースのドリフトを検出しません。
AWS CloudFormation custom resource - AWS ParallelCluster

CloudFormation doesn't detect custom resource drift.

やってみた

まずはカスタムリソースとなる Lambda 関数を作成します。

  • 関数名: custom-resource-handler
  • ランタイム: Python 3.13
  • 実行ロール: AdministratorAccess 権限を付与した IAM ロール
  • コード: 以下の通り
lambda_function.py
import json
import urllib3

def lambda_handler(event, context):
    urllib3.PoolManager().request('PUT', event['ResponseURL'],
        body=json.dumps({
            'Status': 'SUCCESS',
            'PhysicalResourceId': 'custom-resource-001',
            'StackId': event['StackId'],
            'RequestId': event['RequestId'],
            'LogicalResourceId': event['LogicalResourceId'],
            'Data': {}
        }),
        headers={'Content-Type': 'application/json'})

上記コードをデプロイします。

続いて以下のテンプレートで CloudFormation スタックを作成します。

AWSTemplateFormatVersion: "2010-09-09"
Description: "Custom Resource Test Template"

Resources:
  MyCustomResource:
    Type: "AWS::CloudFormation::CustomResource"
    Properties:
      ServiceToken: !Sub "arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:custom-resource-handler"

スタック作成時のオプションはすべてデフォルト値です。
スタックの作成が完了すればカスタムリソースの作成も完了です。
2025-08-07_09h25_08

この状態でドリフトを検出してもデプロイ直後なのでステータスは IN_SYNC です。
2025-08-07_09h26_45

ここで Lambda のタイムアウト値を変更します。
デフォルトの 3 秒から 5 秒に変えてみました。
2025-08-07_09h28_23

Lambda のタイムアウト変更後、再度 CloudFormation でドリフトの検出を実行してもステータスは IN_SYNC でした。
2025-08-07_09h30_44

以上より、カスタムリソースではドリフトを検出できないことを確認できました。

なお、カスタムリソース以外でも CloudFormation でドリフトの検出がサポートされていないリソースタイプもあります。
サポート状況については以下のドキュメントをご参照ください。
Resource type support - AWS CloudFormation

参考資料

この記事をシェアする

facebookのロゴhatenaのロゴtwitterのロゴ

© Classmethod, Inc. All rights reserved.