LoginSignup
1
0

More than 3 years have passed since last update.

VPC Lambda から AWS API を使う

Last updated at Posted at 2020-03-25

VPC に置いた Lambda Function から AWS API を使おうとしたらタイムアウトしてしまって困った。

公式ドキュメント を読むと、

Connect your function to private subnets to access private resources. If your function needs internet access, use NAT. Connecting a function to a public subnet does not give it internet access or a public IP address.

Several services offer VPC endpoints. You can use VPC endpoints to connect to AWS services from within a VPC without internet access.

  • Public Subnet に置いてもランタイムのインスタンスに Public IPv4 アドレスは割り当てられない
  • インターネットへのアクセスが必要な場合は Private Subnet に置いて NAT しろ
  • いくつかの AWS Service は VPC エンドポイントでアクセスできるので、それらを使うだけならインターネットアクセスは必要ない

とのこと。

つまり VPC Lambda から AWS API を使いたければ「NAT する」か「VPC エンドポイントを使う」の2択になる。

いずれの場合も Lambda Function は Private Subnet に配置する必要がある。

VPC エンドポイント

使いたい AWS API が VPC エンドポイントに対応しているならこっちの方が簡単と思われる。

ただし今回触っていたのが趣味 AWS 環境であり、VPC エンドポイント1つに約$10/月かかってしまうのがちょっと厳しいのでやめた。

NAT

お金がある人は NAT Gateway を使うと一瞬でできて便利。(約$45/月)

今回はお金がないので NAT インスタンスを自分で立てた。

参考: Ubuntu の ufw で NAT サーバを作る - Qiita

所感

Lambda で Serverless したかったのに何故ワイは NAT インスタンスを構築しているんや・・・?

1
0
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
1
0