LoginSignup
37
32

More than 3 years have passed since last update.

Amazon Linux2でyumできない時に疑うべきこと

Posted at

結論

VPCエンドポイントのポリシーの設定を疑いましょう。

事象

EC2(Amazon Linux2)で、yum update が通らなかった。

$ sudo yum update
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
Could not retrieve mirrorlist http://amazonlinux.us-east-1.amazonaws.com/2/core/latest/x86_64/mirror.list error was
14: HTTP Error 403 - Forbidden


 One of the configured repositories failed (Unknown),
 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=<repoid> ...

     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 <repoid>
        or
            subscription-manager repos --disable=<repoid>

     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=<repoid>.skip_if_unavailable=true

Cannot find a valid baseurl for repo: amzn2-core/2/x86_64

原因と対処法

Amazon Linux2のyumリポジトリにアクセスできていないためエラーが起きています。

Amazon LinuxおよびAmazon Linux2のyumリポジトリは、S3に存在します。
EC2インスタンスが所属しているVPCのエンドポイントが、
S3のyumリポジトリにアクセスできるようにポリシーを設定します。

VPCエンドポイントのポリシーの変更方法

[EC2] -> [インスタンス]とアクセスし、yumできないインスタンスを選択します。
[説明]タブの中に「VPC ID」があるので、クリックします。

image.png

遷移先で左の一覧から[エンドポイント]を選びます。
EC2が所属するVPCをクリックし、下の欄の[ポリシー]を選択します。
[ポリシーの編集]ボタンからポリシーを変更できます。

image.png

Amazon Linux2の場合

大事なのは、Resources の中に "arn:aws:s3:::amazonlinux.*.amazonaws.com/*" が入っていることです。
以下に設定例を示しますが、おそらくこの例通りにはなっていないと思います。
Resources の中に "arn:aws:s3:::amazonlinux.*.amazonaws.com/*" を追記して保存することをオススメします。

追記した行がResourceの中で最終行でないときは、末尾に,(コンマ)をつけ忘れないようにしましょう。

{
  "Statement": [
    {
      "Sid": "AmazonLinux2AMIRepositoryAccess",
      "Principal": "*",
      "Action": [
        "s3:GetObject"
      ],
      "Effect": "Allow",
      "Resource": [
        "arn:aws:s3:::amazonlinux.*.amazonaws.com/*"
      ]
    }
  ]
}

Amazon Linux の場合(参考)

こちらは、以下の2つのリソースへアクセスできる必要があります。

  • "arn:aws:s3:::packages.*.amazonaws.com/*"
  • "arn:aws:s3:::repo.*.amazonaws.com/*"

こちらもAmazon Linux2の場合と同様、Resourceの中に以上の2つを追記しましょう。
以下は設定例です。

{
  "Statement": [
    {
      "Sid": "AmazonLinuxAMIRepositoryAccess",
      "Principal": "*",
      "Action": [
        "s3:GetObject"
      ],
      "Effect": "Allow",
      "Resource": [
        "arn:aws:s3:::packages.*.amazonaws.com/*",
        "arn:aws:s3:::repo.*.amazonaws.com/*"
      ]
    }
  ]
}

結果

無事にyumが通りました。

$ sudo yum update
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
amzn2-core                                                                                    | 2.4 kB  00:00:00     
Resolving Dependencies
--> Running transaction check
---> Package binutils.x86_64 0:2.29.1-29.amzn2 will be updated
---> Package binutils.x86_64 0:2.29.1-30.amzn2 will be an update
---> Package dyninst.x86_64 0:9.3.1-1.amzn2.0.2 will be updated
---> Package dyninst.x86_64 0:9.3.1-3.amzn2 will be an update
(以下略)

参考

Endpoints for Amazon S3

37
32
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
37
32