[Mackerel]MackerelAgentのインストールエラー時に確認すること(S3エンドポイント編)

みなさま、AWSの監視に何を使っていますでしょうか?弊社ではMackerelを利用した監視オプションサービスを提供しています。今回、ハマる訳ないと思っていたEC2へのMackerelAgentのインストールでプチハマりしたので備忘でまとめておきたいと思います。
2020.02.14

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

はじめに

こんにちは。大阪オフィスの林です。

みなさま、AWSの監視に何を使っていますでしょうか?弊社ではMackerelを利用した監視オプションサービスを提供しています。今回、ハマる訳ないと思っていたEC2へのMackerelAgentのインストールでプチハマりしたので備忘でまとめておきたいと思います。

結論

早速結論ですがMackerelAgentのインストールに失敗した時は、S3エンドポイント使ってないかを確認の1つに含めてください。S3エンドポイント使っていてカスタムポリシーできっちりアクセス制御している環境だとMackerelAgentのインストールに失敗することがあります。(インストールに失敗するケースはこれが全てではないですが確認する箇所の1つとして頭の片隅に入れて頂ければと思います。)

どんなエラーが出るの?

こんなエラーでMackerelAgentのインストールに失敗します。

$ curl -fsSL https://mackerel.io/file/script/amznlinux/setup-all-yum-v2.sh | MACKEREL_API
KEY='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' sh
This script requires superuser authority to setup Mackerel agent:
++ mktemp
+ gpgkey_path=/tmp/tmp.6JoolNphdL
+ curl -fsS -o /tmp/tmp.6JoolNphdL https://mackerel.io/file/cert/GPG-KEY-mackerel-v2
+ rpm --import /tmp/tmp.6JoolNphdL
+ rm /tmp/tmp.6JoolNphdL
+ cat
+ yum install -y mackerel-agent
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
amzn2-core                                                                                    | 2.4 kB  00:00:00
amzn2extra-docker                                                                             | 1.3 kB  00:00:00
amzn2extra-epel                                                                               | 1.3 kB  00:00:00
amzn2extra-php7.3                                                                             | 1.3 kB  00:00:00
epel/x86_64/metalink                                                                          | 9.0 kB  00:00:00
http://yum.mackerel.io/amznlinux/v2/2/x86_64/repodata/repomd.xml: [Errno 14] HTTP Error 403 - Forbidden
Trying other mirror.
 One of the configured repositories failed (mackerel-agent),
 and yum doesn't have enough cached data to continue. At this point the only
 safe thing yum can do is fail. There are a few ways to work "fix" this:
     1. Contact the upstream for the repository and get them to fix the problem.
     2. Reconfigure the baseurl/etc. for the repository, to point to a working
        upstream. This is most often useful if you are using a newer
        distribution release than is supported by the repository (and the
        packages for the previous distribution release still work).
     3. Run the command with the repository temporarily disabled
            yum --disablerepo=mackerel ...
     4. Disable the repository permanently, so yum won't use it by default. Yum
        will then just ignore the repository until you permanently enable it
        again or use --enablerepo for temporary usage:
            yum-config-manager --disable mackerel
        or
            subscription-manager repos --disable=mackerel
     5. Configure the failing repository to be skipped, if it is unavailable.
        Note that yum will try to contact the repo. when it runs most commands,
        so will have to try and fail each time (and thus. yum will be be much
        slower). If it is a very temporary problem though, this is often a nice
        compromise:
            yum-config-manager --save --setopt=mackerel.skip_if_unavailable=true
failure: repodata/repomd.xml from mackerel: [Errno 256] No more mirrors to try.
http://yum.mackerel.io/amznlinux/v2/2/x86_64/repodata/repomd.xml: [Errno 14] HTTP Error 403 - Forbidden
+ mackerel-agent init -apikey=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
sh: line 21: mackerel-agent: command not found

特徴としては、yum.mackerel.ioに行こうとしてHTTP Error 403 - Forbiddenになってる部分です。

どうすればいいの?

例えばS3エンドポイントを使っていてカスタムポリシーでアクセス制御している場合、こんなポリシーになっているかと思います。

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "Access-to-S3",
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:*",
            "Resource": [
                "arn:aws:s3:::hogehoge.hogehoge.com",
                "arn:aws:s3:::hogehoge.hogehoge.com/*",
                "arn:aws:s3:::amazonlinux.ap-northeast-1.amazonaws.com",
                "arn:aws:s3:::amazonlinux.ap-northeast-1.amazonaws.com/*"
            ]
        }
    ]
}

ここにarn:aws:s3:::yum.mackerel.io/arn:aws:s3:::yum.mackerel.io/*を加えてあげてください。

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "Access-to-S3",
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:*",
            "Resource": [
                "arn:aws:s3:::hogehoge.hogehoge.com",
                "arn:aws:s3:::hogehoge.hogehoge.com/*",
                "arn:aws:s3:::amazonlinux.ap-northeast-1.amazonaws.com",
                "arn:aws:s3:::amazonlinux.ap-northeast-1.amazonaws.com/*",
                "arn:aws:s3:::yum.mackerel.io/",
                "arn:aws:s3:::yum.mackerel.io/*"
            ]
        }
    ]
}

なぜ???

AmazonLinuxではデフォルトのリポジトリとしてS3が登録されています。(なのでS3エンドポイントがあればインターネットに出られなくてもyumのアップデートが出来ます。)

デフォルトのリポジトリがS3になっているのでS3エンドポイントを使っている場合は、そのエンドポイント経由でyumを実行しようとします。例えば今回のケースでいうとMackerelAgentをインストールしようとS3のリポジトリにアクセスします。もちろんAWSのデフォルトのリポジトリではMackerelAgentのパッケージはもっていないので別のリポジトリを探すことになるのですが、S3エンドポイントのカスタムポリシーできっちりアクセス制御している場合、MackerelAgentのリポジトリを探しに行く部分の通信も制限されてMackerelAgentのインストールが出来ないという動作になっていると考えられます。

まとめ

蓋を開けてみれば単純な原因ですが想定していないエラーに遭遇すると結構あわてるものです。今後MackerelAgentのインストールのトラブルに遭遇した方の助けになれば幸いです。

以上、大阪オフィスの林がお送りしました!