0Pricing
Serverless AWS Lambda Development · 课时

NAT 网关与 VPC 的互联网访问

了解为什么私有 VPC 子网中的 Lambda 会失去互联网访问,以及 NAT 网关和 VPC 终端节点如何安全地恢复连接。

NAT 网关与 VPC 的互联网访问 是 CoddyKit 上的免费 Serverless AWS Lambda Development 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Serverless AWS Lambda Development 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Serverless AWS Lambda Development 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

The Surprise

A Lambda in a VPC can reach private resources, but it loses default internet access. Calls to public APIs suddenly time out.

Why It Happens

VPC Lambdas have no public IP. Without a route to the internet, outbound calls to external endpoints have nowhere to go.

Public vs Private Subnets

A public subnet routes to an Internet Gateway; a private subnet does not. Always place Lambdas in private subnets for security.

Enter the NAT Gateway

A NAT gateway lives in a public subnet and lets private subnets make outbound internet calls while staying unreachable from the internet.

The Route Table

The private subnet route table sends internet-bound traffic to the NAT gateway, which forwards it through the Internet Gateway.

Destination     Target
10.0.0.0/16     local
0.0.0.0/0       nat-0abc123

NAT Costs Money

NAT gateways charge per hour plus per GB processed. For heavy traffic this adds up, so consider whether you truly need public internet access.

VPC Endpoints Instead

To reach AWS services like S3 or DynamoDB, use a VPC endpoint. Traffic stays on the AWS network with no NAT needed and no per-GB internet cost.

Gateway vs Interface Endpoints

S3 and DynamoDB use free gateway endpoints; most other services use interface endpoints (powered by PrivateLink) which are billed hourly.

Calling S3 Through an Endpoint

No code change is needed. With a gateway endpoint and the right route, this S3 call never leaves the AWS backbone.

import boto3
s3 = boto3.client('s3')
s3.put_object(Bucket='my-bucket', Key='log.txt', Body=b'hi')

Choosing the Right Path

Need a public third-party API? Use a NAT gateway. Need only AWS services? Use VPC endpoints. Often you use endpoints to avoid NAT entirely.

Multi-AZ for Resilience

Deploy a NAT gateway per Availability Zone and put the Lambda in multiple subnets, so one AZ outage does not break connectivity.

Quick Check

Test your VPC networking knowledge.

Recap

You learned why VPC Lambdas lose internet access and how NAT gateways restore outbound access, while VPC endpoints reach AWS services privately and cheaply.

常见问题解答

「NAT 网关与 VPC 的互联网访问」课时是免费的吗?

是的 — 「NAT 网关与 VPC 的互联网访问」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Serverless AWS Lambda Development 课程的其余内容,请升级到 CoddyKit PRO。 Serverless AWS Lambda Development 课程共包含 4 节课。

「NAT 网关与 VPC 的互联网访问」这节课中我会学到什么?

了解为什么私有 VPC 子网中的 Lambda 会失去互联网访问,以及 NAT 网关和 VPC 终端节点如何安全地恢复连接。 你通过在浏览器中直接运行的动手代码来练习 Serverless AWS Lambda Development,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Serverless AWS Lambda Development 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Serverless AWS Lambda Development 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「NAT 网关与 VPC 的互联网访问」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Serverless AWS Lambda Development 课中编写并运行代码吗?

能。每节 Serverless AWS Lambda Development 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 在 VPC 中使用 Lambda 访问私有资源
  2. 访问 VPC 中的数据库
  3. 网络安全最佳实践
  4. NAT 网关与 VPC 的互联网访问
← 返回 Serverless AWS Lambda Development