AWS · Advanced Networking Specialty

网络课程笔记

Lecture 172 · 12 张幻灯片

幻灯片、双语总结及 Transcript · 图片已内嵌,可离线阅读。

原文默认收起
lecture-172.md

Lecture 172: CloudFront Functions and Lambda@Edge

  • 课程:[NEW] AWS Certified Advanced Networking Specialty 2026
  • 视频时长:09:58
  • 来源:Udemy Lecture 28556165
  • Slide 数量:12
  • 画面切换:00:08、01:14、01:42、02:44、03:43、04:18、04:44、06:18、07:23、07:58、09:21

扫描说明

逐条读取了 Udemy 页面中的 266 条英文 Transcript cue,并按时间轴扫描全长约 09:58 的视频。依据边缘定制概览、部署位置、CloudFront Functions、Lambda@Edge、组合/单独使用、能力对比和四类典型用例划分为 12 个小节;同一幻灯片中的动画构建合并,并在信息完整时截图。所有截图均关闭 Transcript 侧栏、隐藏播放器控件,统一为 1364×770。

Transcript 来自 Udemy 页面自带的英文自动字幕,没有使用 ASR。下面保留页面原文,包括其中的语法或识别错误。

1. CloudFront Edge Customization

时间范围:00:00–00:08

CloudFront customization at the edge title

总结 / Summary

中文

CloudFront 不只是内容分发网络(Content Delivery Network, CDN),还可以在边缘运行自定义代码,对请求和响应进行低延迟处理。AWS 提供 CloudFront Functions 与 Lambda@Edge 两种 Edge Function 机制。

English

CloudFront is more than a Content Delivery Network. It can run custom code at the edge to process requests and responses with low latency. AWS provides two edge-function mechanisms: CloudFront Functions and Lambda@Edge.

Transcript

00:00 So CloudFront has more capability

00:02 than just a CDN.

00:03 It is allowing you to run custom code at the edge

00:06 to do some customization.

2. Edge Function 的目的与用例

时间范围:00:08–01:14

CloudFront edge function concepts and use cases

总结 / Summary

中文

Edge Function 是附加到 CloudFront Distribution 的代码,在接近用户的位置执行,用于修改 HTTP Request/Response,而不是改变 Cache Behavior。典型用途包括 Request Filtering、Authentication/Authorization、在边缘直接生成 Response、A/B Testing、Bot Mitigation 和 Cache Key Normalization。两种服务都无需管理服务器,并由 CloudFront 全球部署。

English

An edge function is code attached to a CloudFront distribution and executed close to users. It modifies HTTP requests and responses rather than changing cache behavior. Typical uses include request filtering, authentication and authorization, generating responses at the edge, A/B testing, bot mitigation, and cache-key normalization. Both mechanisms are serverless and deployed globally by CloudFront.

Transcript

00:08 So the idea is that you want to execute edge functions

00:11 as close as possible to users to make sure

00:14 that you're verifying some early logic, some early behavior,

00:17 or normalizing some cache keys.

00:19 So an edge function is a code that you write

00:19 and that you attach to your CloudFront distribution,

00:19 and it will run very, very close to your users

00:19 in order to minimize the latency

00:27 of whatever you're trying to do.

00:29 It doesn't have any cache behavior.

00:30 It is just here to change the request

00:32 and the response sent by the users and back to the users.

00:36 Now, CloudFront has two different logic for edge functions.

00:40 It has the CloudFront functions and Lambda@Edge.

00:43 We'll see them both in this lecture.

00:45 So the use cases for customization at the end is to,

00:48 at the edge is to manipulate HTTP request and responses.

00:52 Is to implement request filtering

00:53 before reaching your application.

00:54 Is to verify user authentication and authorization.

00:58 Is to also maybe generate HTTP responses

01:00 directly at the edge

01:02 or do A/B testing by separating users into two groups,

01:05 or for example, doing bot mitigation at the edge.

01:08 The idea is that using edge functions,

01:10 so either CloudFront functions or Lambda@Edge,

01:12 you do not have to manage any servers.

3. 部署位置:Edge Location 与 Regional Edge Cache

时间范围:01:14–01:42

CloudFront Functions and Lambda at Edge deployment locations

总结 / Summary

中文

CloudFront Functions 部署并运行在 Edge Location,最靠近 Viewer;Lambda@Edge 运行在 Regional Edge Cache。不同部署层级会影响延迟、规模、可用 Trigger 和能力选择。

English

CloudFront Functions run at edge locations, closest to viewers, while Lambda@Edge runs at regional edge caches. The deployment layer affects latency, scale, available triggers, and the appropriate service choice.

Transcript

01:14 Everything is deployed globally and it is serverless.

01:14 So if we look at a traditional architecture of CloudFront,

01:21 we have our clients talking to our edge locations,

01:23 talking to original edge cache, talking to our origin.

01:26 Now, Lambda@Edge functions are going to be deployed

01:29 at the regional edge cache level,

01:31 whereas CloudFront functions are going to be deployed

01:31 on the edge location.

01:34 So as you can see, two different level for deployment

01:37 of these functions, and that will impact your choice

01:37 between CloudFront functions and Lambda@Edge functions.

4. CloudFront Functions 特性与 Trigger

时间范围:01:42–02:44

CloudFront Functions characteristics and viewer triggers

总结 / Summary

中文

CloudFront Functions 是轻量级 JavaScript 函数,只处理 Viewer Request 和 Viewer Response,不处理 Origin Request/Response。它为高规模、延迟敏感的 CDN 定制设计,启动极快,可扩展到每秒数百万请求,并通过 CloudFront 原生界面管理代码。

English

CloudFront Functions are lightweight JavaScript functions that handle only viewer request and viewer response events, not origin events. They target high-scale, latency-sensitive CDN customization, start quickly, scale to millions of requests per second, and are managed natively in CloudFront.

Transcript

01:42 So if we look at CloudFront functions,

01:44 they will interact with the viewer request

01:47 and the viewer response.

01:48 These are the request and response

01:50 that happen between the client and CloudFront.

01:52 It does not modify origin request or origin response.

01:55 So these are lightweight functions returning JavaScript,

01:58 and they're meant to do high scale latency sensitive

02:00 CDN customizations.

02:02 So it is a sub-millisecond startup time,

02:04 so it's very, very quick to start and it can handle

02:06 millions of requests per second.

02:09 So we run at edge locations

02:10 and it's process-based isolation.

02:13 So you want to change the viewer request and responses

02:15 as I said.

02:16 So what is the viewer request?

02:17 Well, it's after CloudFront receives a request

02:19 from a viewer, and your response is before CloudFront

02:22 forwards the response to the viewer.

02:24 So the idea is that it's a native feature of CloudFront.

02:26 You manage the code directly from within CloudFront,

02:29 and this allows you to do some very quick

02:32 time sensitive transformations in CloudFront.

02:34 Okay?

02:35 The idea is that you cannot call external services

02:38 from the CloudFront functions.

02:39 You just want to perform some transformations,

02:41 some computations or some normalizations

5. Lambda@Edge 特性与四类 Trigger

时间范围:02:44–03:43

Lambda at Edge characteristics and trigger points

总结 / Summary

中文

Lambda@Edge 支持 Node.js 或 Python,运行在 Regional Edge Cache,规模通常低于 CloudFront Functions,但具备更强能力。它可以挂载到 Viewer Request、Viewer Response、Origin Request 和 Origin Response 四类事件。函数在 us-east-1 编写后由 CloudFront 复制到全球 Regional Edge Caches。

English

Lambda@Edge supports Node.js or Python and runs at regional edge caches. It scales less than CloudFront Functions but offers greater capability. It can attach to viewer request, viewer response, origin request, and origin response events. Functions are authored in us-east-1 and replicated by CloudFront to regional edge caches worldwide.

Transcript

02:44 in a very, very quick fashion.

02:46 Now, Lambda@Edge is a different mindset.

02:49 So these were first what CloudFront was offering

02:52 as a feature.

02:53 CloudFront functions are new,

02:53 but Lambda@Edge has been existing for a long time,

02:56 and the idea was this is that you have Lambda functions

02:58 that are in either NodeJS or Python,

02:58 and you can scale to thousands of requests per second.

03:04 So less scale than your CloudFront functions for sure.

03:04 And they run on the nearest regional edge cache.

03:12 They're VM-based isolation,

03:14 and they're used to change both

03:14 CloudFront requests and responses.

03:18 So we can change the viewer request, the origin request,

03:20 which is the request that CloudFront sends to your origin.

03:24 You can change the origin response, so the response

03:26 that CloudFront receives from the origin

03:29 and as well as the viewer response.

03:31 Okay?

03:32 Now, when you want to deploy them,

03:33 that edge functions on CloudFront,

03:35 you need to author your functions in one region,

03:36 which is US East one,

03:38 and then CloudFront will replicate this function

03:40 to all its regional edge caches.

6. 组合使用 CloudFront Functions 与 Lambda@Edge

时间范围:03:43–04:18

CloudFront Functions used together with Lambda at Edge

总结 / Summary

中文

两者可在同一请求路径中组合使用:CloudFront Functions 在 Edge Location 处理 Viewer Request/Response,Lambda@Edge 在 Regional Edge Cache 处理 Origin Request/Response。这形成清晰的职责分离,并允许轻量逻辑尽量靠近用户、复杂逻辑靠近 Origin 路径。

English

Both services can participate in one request path. CloudFront Functions handles viewer request and response events at the edge location, while Lambda@Edge handles origin request and response events at the regional edge cache. This creates a clear separation of duties between lightweight viewer logic and more capable origin-path logic.

Transcript

03:43 Now, if we have a look at

03:45 how these things can be used together.

03:46 So we have edge locations and regional edge caches,

03:49 and so the view request goes all the way like this,

03:52 then it goes through the origin,

03:53 and then the origin response goes all the way like this

03:55 to the viewer response.

03:57 Now your CloudFront functions

03:58 are going to run on the edge locations at the viewer request

04:02 and viewer response level,

04:03 whereas your Lambda@Edge functions are going to run

04:06 at the origin request and origin response level.

04:08 This is if you want to use both CloudFront functions

04:11 and Lambda@Edge functions together.

04:13 Okay, so this is one option

04:14 and we can see a clear separation of duties.

7. 仅使用 Lambda@Edge

时间范围:04:18–04:44

Using Lambda at Edge only across viewer and origin events

总结 / Summary

中文

如果需要更长执行时间、Network Access、第三方 API 调用等 CloudFront Functions 不具备的能力,可只使用 Lambda@Edge,并让它在 Regional Edge Cache 处理 Viewer 与 Origin 两侧事件。同一个 Viewer Event 不能同时关联 CloudFront Function 和 Lambda@Edge。

English

When the workload needs longer execution, network access, third-party API calls, or other capabilities unavailable in CloudFront Functions, Lambda@Edge can handle both viewer and origin events at the regional edge cache. The same viewer event cannot use both a CloudFront Function and Lambda@Edge.

Transcript

04:18 But if you want to just have Lambda@Edge functions,

04:22 then in that case, what's going to happen

04:23 is that the Lambda@Edge functions are going to operate

04:26 on both the viewer requests response

04:28 and the origin request and response,

04:29 but only at the regional edge cache level.

04:32 In this case, we cannot use CloudFront functions

04:34 because we cannot use both Lambda@Edge

04:36 and CloudFront functions

04:37 on the viewer request and responses.

04:40 So this is when you need capabilities of Lambda@Edge

04:43 that are not available with CloudFront functions.

8. CloudFront Functions 与 Lambda@Edge 能力对比

时间范围:04:44–06:18

CloudFront Functions versus Lambda at Edge comparison table

总结 / Summary

中文

CloudFront Functions 仅支持 JavaScript、在 Edge Location 执行、只支持 Viewer Events、最大约 2 MB Memory、代码大小约 10 KB,不能访问 Network 或 File System,但延迟低、规模高、包含 Free Tier,单价约为 Lambda@Edge 的六分之一。Lambda@Edge 支持 Node.js/Python、四类 Events、更长 Runtime、更多 Memory 与代码空间,并可访问 Network、File System 和 Request Body,但价格更高且没有 Free Tier。能下沉到 CloudFront Functions 的轻量逻辑通常更经济。

English

CloudFront Functions supports JavaScript only, runs at edge locations, handles viewer events only, offers about 2 MB of memory and roughly 10 KB of code, and has no network or file-system access. In return, it delivers very low latency, massive scale, a free tier, and pricing around one-sixth of Lambda@Edge. Lambda@Edge supports Node.js and Python, all four event types, longer runtime, more memory and code space, network and file-system access, and request-body access, but costs more and has no free tier. Lightweight logic should move to CloudFront Functions when possible.

Transcript

04:44 For example, if you need a longer execution time,

04:44 network access, third party API calls, these kind of things.

04:44 So if we want to compare, you know, on the table,

04:54 the CloudFront functions and Lambda@Edge,

04:56 while the runtime support is different,

04:58 so one is JavaScript, pure JavaScript code,

05:00 the other one is NodeJS and Python,

05:02 where it gets executed is on edge locations

05:05 for CloudFront functions, whereas it's original edge cache

05:05 is for Lambda@Edge.

05:09 The CloudFront triggers is going to be the view request

05:11 and response for functions

05:12 and all of them, for them that edge.

05:15 The isolation is process-based versus VM-based.

05:17 Now, the max execution time

05:19 is what I think is the most important thing.

05:21 You only get one millisecond

05:22 to execute your CloudFront functions

05:24 and therefore they must be very, very quick

05:26 and very, very simple.

05:27 Whereas Lambda@Edge, you get five seconds of runtime

05:31 for viewers triggered and 30 seconds of runtime

05:34 for origin triggers.

05:35 The memory of your CloudFront functions

05:37 is low, two megabytes.

05:38 Whereas for Lambda@Edge, you have more memory,

05:41 128 megabytes or 10 gigabytes.

05:43 The total package size is very, very small

05:45 for CloudFront functions, 10 kilobytes.

05:46 Whereas Lambda@Edge has more space.

05:49 There's no network access, no file system access

05:51 for CloudFront functions,

05:52 whereas you have some for Lambda@Edge.

05:54 You do not have access to the request body

05:57 in CloudFront functions,

05:58 you have access to it in Lambda@Edge.

06:00 So CloudFront functions is really about

06:02 reading headers and so on.

06:04 And the pricing is that there's a free tier

06:06 for CloudFront functions,

06:07 and it's 1/6 of the price of @Edge.

06:09 So if you can move your logic from Lambda@Edge

06:12 to CloudFront functions, you're better off.

06:14 And Lambda@Edge has no free tier

06:15 and you get charged per request and duration.

9. 选择服务的典型用例

时间范围:06:18–07:23

CloudFront Functions and Lambda at Edge use cases

总结 / Summary

中文

CloudFront Functions 适合 Cache Key Normalization、修改 Headers/Cookies/Query Strings/URL、Redirect/Rewrite,以及轻量 Authentication/Authorization。Lambda@Edge 适合需要更长执行时间、可调 CPU/Memory、第三方 Library 或 AWS SDK、外部 Network Call,以及访问 File System 或 HTTP Request Body 的逻辑。

English

CloudFront Functions fits cache-key normalization, header/cookie/query-string/URL manipulation, redirects or rewrites, and lightweight authentication or authorization. Lambda@Edge fits longer execution, adjustable CPU or memory, third-party libraries or the AWS SDK, external network calls, file-system access, and HTTP request-body processing.

Transcript

06:18 So when do you use a CloudFront function

06:20 versus a Lambda@Edge function?

06:22 Well, CloudFront functions are going to be used

06:24 for cache key normalization.

06:25 So you want to transform the request attributes

06:27 such as the headers, cookies, the query string, the URL

06:30 to create an optimal cache key.

06:32 You also want to manipulate your headers,

06:34 so you want to change them.

06:34 So you want to insert, modify, or delete HTTP headers

06:37 in the request or response,

06:39 or you want to do your rewrites or redirects

06:41 or to request authentication

06:43 and authorization directly at the edge.

06:45 So in which you can create

06:46 and validate user generated tokens, for example, JWT,

06:50 to allow or deny request very, very close to your users.

06:54 Whereas Lambda@Edge is going to be giving you

06:56 a much longer execution time, okay?

06:57 Up to five seconds or 30 seconds.

07:00 You get adjustable CPU or memory.

07:01 You get to have code that can depend on

07:04 third party libraries.

07:04 For example, you can use the SDK

07:06 to access other AWS services.

07:08 You can use the network access to use external services

07:11 for processing, and you have file system access

07:13 if you wanted to cache some stuff locally,

07:15 or you can even access the body of the HTTP request

07:18 to manipulate the body itself.

07:20 So hopefully the use cases are clear and well-defined.

10. Authentication 与 Authorization

时间范围:07:23–07:58

Authentication and authorization with CloudFront Functions and Lambda at Edge

总结 / Summary

中文

CloudFront Functions 可在 Edge Location 验证客户端携带的 JWT 等 Token,并在无效时直接拒绝请求,避免流量到达 Origin。Lambda@Edge 也能执行验证,并可借助第三方 Library 或 API Call 完成更复杂的 Authentication/Authorization。

English

A CloudFront Function can validate a client token such as a JWT at the edge and reject an invalid request before it reaches the origin. Lambda@Edge can also perform validation and can use third-party libraries or API calls for more complex authentication and authorization.

Transcript

07:23 Now, if you wanted to do authorization

07:25 and authentication with CloudFront functions,

07:27 the idea is that at the Edge location

07:29 you would intercept the content

07:30 and you would check the token, the digital JWT token,

07:34 which lives in your headers, and then you can check

07:36 whether or not it is correct.

07:37 And then if it's not correct,

07:38 then the client will get an error message directly

07:41 without having your request go all the way to your origin.

07:44 Whereas your Lambda@Edge function

07:46 will operate on the regional edge cache level.

07:48 It could be intercepted by a Lambda function,

07:50 which can do some custom logic

07:51 and even issue third party API calls

07:54 to check whether or not the authorization

07:56 and authentication is properly well set up.

11. 根据 User-Agent 加载不同内容

时间范围:07:58–09:21

CloudFront Function loading content based on user agent

总结 / Summary

中文

CloudFront Function 可检查 User-Agent Header,把来自不同设备的同一 URL 重写为不同的 Cache Key 或对象路径。例如 Desktop 获取大图,Mobile 获取小图;CloudFront 随后从 Cache 或 S3 Origin 返回适合设备的版本,从而减少移动端传输量。

English

A CloudFront Function can inspect the User-Agent header and rewrite the same URL to a different cache key or object path. A desktop client can receive a large image while a mobile client receives a smaller image. CloudFront then serves the device-appropriate version from cache or the S3 origin, reducing mobile transfer size.

Transcript

07:58 Another very common use case for CloudFront functions

07:58 is to load content based on the user agents.

08:04 We have an S3 bucket

08:05 and Amazon CloudFront with original Edge cache

08:07 and we have users accessing it from desktop

08:10 and others just accessing it from mobile.

08:12 Now they both wanna access a cat picture,

08:14 so they do a request for cat picture,

08:16 and the resolution is super high.

08:17 So 1920-1080.

08:23 So it's 1080p and they request it from CloudFront.

08:25 So if the request goes well,

08:27 well the request is passed all the way through

08:29 to the S3 bucket to get that specific image.

08:32 But imagine that the same request is done

08:35 from our mobile device, and this time the user agent

08:37 is not Chrome, it is the iPhone OS Safari.

08:41 Now what we can do is instead of sending this big image

08:45 back to our mobile, which may be very inefficient,

08:48 we can have a CloudFront function

08:50 that will inspect the user agent HTTP header

08:50 and then redirect based on the device type.

08:56 And so there, therefore,

08:58 because our device is a mobile device,

09:00 then the image is going to be asked into our S3 bucket

09:03 is going to be cat 640-320,

09:06 which is a lower resolution for our cat picture.

09:10 And this is the picture that will be returned

09:10 by our S3 bucket into our mobile device.

09:14 So the function was able to modify the request

09:14 to change the target of these requests.

12. Lambda@Edge 支持 Global Application

时间范围:09:21–09:58

Lambda at Edge global application querying DynamoDB

总结 / Summary

中文

Lambda@Edge 可把动态逻辑部署到全球 Regional Edge Caches,为 Static Website 增加 API 能力,并通过 Network Access 查询 DynamoDB 等外部服务。它适合在靠近用户的位置执行需要 AWS SDK 或后端数据访问的动态请求处理。

English

Lambda@Edge can deploy dynamic logic across global regional edge caches, add API behavior to a static website, and use network access to query services such as DynamoDB. It is suitable for dynamic request processing near users when the logic needs the AWS SDK or backend data access.

Transcript

09:21 So the other way you can use Lambda@Edge

09:23 is for a global application, for example,

09:24 your client gets the website from an S3 bucket,

09:24 and then for dynamic API request,

09:29 it sends it into CloudFront to leverage the cache.

09:32 But your Lambda function can also be used, for example,

09:34 to query data into DynamoDB table,

09:36 which gives you a fully global

09:36 and serverless way to do an API in AWS.

09:44 So that's it.

09:44 At the exam level what you need to know

09:47 is understand when you need to use Lambda@Edge

09:47 or CloudFront functions, and that should be good to go.

09:52 But I want to give you a bit more detail

09:53 so you can really understand the use cases behind it.

09:55 So I hope you liked this lecture

09:56 and I will see you in the next lecture.

整讲总结 / Overall Summary

中文

CloudFront Functions 和 Lambda@Edge 都能在 CloudFront 边缘修改请求与响应,但定位不同。CloudFront Functions 更靠近用户,专注 Viewer Events,以极低延迟、高规模和低成本执行轻量 JavaScript;Lambda@Edge 位于 Regional Edge Cache,支持四类 Events、更长执行、更大资源、Network/File System/Request Body 访问和更丰富 Runtime。选择时应优先把可实现的轻量逻辑放到 CloudFront Functions,仅在需要高级能力时使用 Lambda@Edge;两者也可按 Viewer 与 Origin 路径组合。

English

CloudFront Functions and Lambda@Edge both customize CloudFront requests and responses, but they serve different roles. CloudFront Functions runs closer to viewers and focuses on viewer events with extremely low latency, massive scale, and low-cost JavaScript execution. Lambda@Edge runs at regional edge caches and supports all four event types, longer execution, larger resources, network/file-system/request-body access, and richer runtimes. Prefer CloudFront Functions for lightweight logic and use Lambda@Edge when advanced capabilities are required; they can also be combined across viewer and origin paths.

返回顶部 ↑