EFS マウント時エラー「ERROR – Cannot retrieve region from instance_metadata」の回避方法について

2023.03.08

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

困っていること

EC2 インスタンスから EFS にマウントする際マウント処理が失敗しました。
ログを確認したところ、以下のエラーを確認しています。

2023-01-01 09:39:11,609 - WARNING - Region not found in config file and metadata service call failed, falling back to legacy "dns_name_format" check
2023-01-01 09:39:11,609 - WARNING - Legacy check for region in "dns_name_format" failed
2023-01-01 09:39:11,609 - ERROR - Cannot retrieve region from instance_metadata

当該エラーの回避方法を教えてください。

どう対応すればいいの?

原因

エラーメッセージを調査したころ、下記 GitHub より同様のエラーを確認してます。

  • WARNING について

リージョン情報を "dns_name_format" から取得しており、"instance_metadata" からリージョン情報を取得されていないため出力されています。

  • ERROR について

ファイルシステムと当該 EC2 インスタンスが異なるリージョンで利用しているため、当該エラーが発生している可能性が見受けられます。

def get_target_region(config):
    def _fatal_error(message):
        fatal_error(
            'Error retrieving region. Please set the "region" parameter '
            "in the efs-utils configuration file.",
            message,
        )

    try:
        return config.get(CONFIG_SECTION, "region")
    except NoOptionError:
        pass

    try:
        return get_region_from_instance_metadata(config)
    except Exception as e:
        metadata_exception = e
        logging.warning(
            "Region not found in config file and metadata service call failed, falling back "
            'to legacy "dns_name_format" check'
        )

    try:
        region = get_region_from_legacy_dns_format(config)
        sys.stdout.write(
            'Warning: region obtained from "dns_name_format" field. Please set the "region" '
            "parameter in the efs-utils configuration file."
        )
        return region
    except Exception:
        logging.warning('Legacy check for region in "dns_name_format" failed')

    _fatal_error(metadata_exception)
def get_region_from_instance_metadata(config):
    instance_identity = get_instance_identity_info_from_instance_metadata(
        config, "region"
    )

    if not instance_identity:
        raise Exception(
            "Cannot retrieve region from instance_metadata. "
            "Please set the 'region' parameter in the efs-utils configuration file."
        )

    return instance_identity

回避方法

ドキュメント を参考に efs-utils.conf ファイル に 'region' パラメーターを設定することで、事象が解消されるかご確認ください。

If you are mounting your EFS file system from an Amazon EC2 instance that is in a different AWS Region than the file system, you will need to edit the region property value in the efs-utils.conf file.

記述例

次の行を見つけて、行のコメント(#)を外し対象リージョンを記述してください。
*変更前
#region = us-east-1

*変更後
region = ap-northeast-1

参考資料