6
3

More than 1 year has passed since last update.

[AWS]プライベートネットワークのEC2からタダでyum install/updateを行う

Last updated at Posted at 2022-12-07

インフラ構築していくうえで、外界に触れられたくないサーバ群はNWから切り離した環境に置くのが一般的かと思います(例えばDBサーバ)。
その際にNATを利用することになるかと思いますが、NATGWはまあまあいい値段がするので何とか削れないかと考えました。。

結論

いきなり結論から入ります。
VPCEndpoint S3のGatewayTypeを使いましょう!

Note: To avoid the NAT Gateway Data Processing charge in this example, you could set up a gateway Type VPC endpoint and route the traffic to/from S3 through the VPC endpoint instead of going through the NAT Gateway. There are no data processing or hourly charges for using Gateway Type VPC endpoints. For details on how to use VPC endpoints, please visit VPC Endpoints Documentation.

ゲートウェイタイプの VPC エンドポイントの使用に対するデータ処理料金や時間単位料金は発生しません。

つまり設定してしまえばタダで使えてしまいます。

設定方法

VPC > エンドポイント > エンドポイントを作成 に遷移して以下のように対象にしたいVPCとルートテーブルを選択してあげればOKです。

image.png

これで設定したNW内に属するEC2はタダでyumが使えるようになります!
※ただし、EC2のOSがAmazonLinux系に限ります。

構成パターン

プライベートNWから実現しようとすると以下のパターンになります。

  • EC2でNAT構築(NATインスタンス)
    • 自前で構築するのだるい~
  • NATGW
    • ちょっと料金が、、、
  • パブリックNWに置いちゃう
    • ( ˘ω˘ )

料金

EC2料金

最安で 0.0042 USD × 24 × 30 = 3.024/月 = 412.63円/月(2022/12/06現在)

NATGW料金

0.062USD/h

単純計算 0.062USD/h × 24 × 30 = 44.64/月 = 6,091.22円/月(2022/12/06現在)

※料金計算はあえて起動時間のみにしています。通信に対しても使った分だけ料金がかかります

仮にyumだけのためにNATを立てるなんてもったいない。。

次なる作戦は、必要な時だけNATGWを起動させる。運用コストかかるし、、めんどうですよね。。
そこで、VPCEndpoint S3のGatewayTypeを使うって感じなります。

種明かし

有効なリポジトリリストを見てみます。

$ yum repolist enabled -v
Loading "extras_suggestions" plugin
Loading "langpacks" plugin
Loading "priorities" plugin
Loading "update-motd" plugin
Adding en_US to language list
Config time: 0.451
Yum version: 3.4.3
Setting up Package Sacks
pkgsack time: 0.005
Repo-id      : amzn2-core/2/aarch64
Repo-name    : Amazon Linux 2 core repository
Repo-revision: 1667440190
Repo-updated : Thu Nov  3 01:49:50 2022
Repo-pkgs    : 20,542
Repo-size    : 30 G
Repo-mirrors : https://amazonlinux-2-repos-ap-northeast-1.s3.dualstack.ap-northeast-1.amazonaws.com/2/core/latest/aarch64/mirror.list
Repo-baseurl : https://amazonlinux-2-repos-ap-northeast-1.s3.dualstack.ap-northeast-1.amazonaws.com/2/core/2.0/aarch64/0cac95da63306270fbedb235008cfaf4f04477723dc1966e2abadb274a0edd44/
Repo-expire  : 300 second(s) (last: Tue Dec  6 12:04:43 2022)
  Filter     : read-only:present
Repo-filename: /etc/yum.repos.d/amzn2-core.repo

Repo-id      : amzn2extra-docker/2/aarch64
Repo-name    : Amazon Extras repo for docker
Repo-revision: 1665591570
Repo-updated : Wed Oct 12 16:19:30 2022
Repo-pkgs    : 66
Repo-size    : 886 M
Repo-mirrors : https://amazonlinux-2-repos-ap-northeast-1.s3.dualstack.ap-northeast-1.amazonaws.com/2/extras/docker/latest/aarch64/mirror.list
Repo-baseurl : https://amazonlinux-2-repos-ap-northeast-1.s3.dualstack.ap-northeast-1.amazonaws.com/2/extras/docker/stable/aarch64/1ee1c87f6d4f9b19701d3c5f5258cc8951a7d277ce5e41ffa2c94f7438abe976/
Repo-expire  : 21,600 second(s) (last: Tue Dec  6 12:04:44 2022)
  Filter     : read-only:present
Repo-filename: /etc/yum.repos.d/amzn2-extras.repo

Repo-id      : amzn2extra-kernel-5.10/2/aarch64
Repo-name    : Amazon Extras repo for kernel-5.10
Repo-revision: 1667861467
Repo-updated : Mon Nov  7 22:51:07 2022
Repo-pkgs    : 213
Repo-size    : 1.3 G
Repo-mirrors : https://amazonlinux-2-repos-ap-northeast-1.s3.dualstack.ap-northeast-1.amazonaws.com/2/extras/kernel-5.10/latest/aarch64/mirror.list
Repo-baseurl : https://amazonlinux-2-repos-ap-northeast-1.s3.dualstack.ap-northeast-1.amazonaws.com/2/extras/kernel-5.10/stable/aarch64/c41f40d2b8397590e7a012f72b31e2fe137624aa386daaea8be514b8f854dbc3/
Repo-expire  : 21,600 second(s) (last: Tue Dec  6 12:04:44 2022)
  Filter     : read-only:present
Repo-filename: /etc/yum.repos.d/amzn2-extras.repo

repolist: 20,821

Repo-baseurlとRepo-mirrorsがS3になってますね。
つまり、このS3にアクセスさえできればyumは使えるってことになります。

よって、VPCEndpoint S3のGatewayTypeを有効にすることでタダでyumができるようになりました。

おわりに

VPCEndpointはS3だけでなく、いろいろなサービスをサポートしています(Lambda, ECRなど)。
ここで注意なのは、あくまでGatewayTypeのVPCEndpointのみがタダであるということです。
VPCEndpointはInterfaceTypeは有料なので注意です。

各 AZ の VPC エンドポイント 1 つあたりの料金 (USD/時間)
0.014USD

とはいえ、NATGWより安価なので、構築時には検討してみるのをお勧めします。
通信対象サービスが少ないのであればVPCEndpointがおすすめです。

以下のように損益分岐点を計算されているページもあるので参考にしてみてください。

6
3
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
6
3