西瓜网站建设,家教辅导培训网站建设,南京seo推广优化,百度网站建设微信封面概述
国内 Amazon Cloudfront 目前不支持 Lambdaedge 功能#xff0c;不能实现基于 CDN 的 A/B 测试、rewrite、redirect、token 认证和产生 response 等功能#xff0c;本文介绍如何利用 API Gateway 和 Lambda 实现 Lambdaedge 的功能。下面实验介绍通过 request header 参…概述
国内 Amazon Cloudfront 目前不支持 Lambdaedge 功能不能实现基于 CDN 的 A/B 测试、rewrite、redirect、token 认证和产生 response 等功能本文介绍如何利用 API Gateway 和 Lambda 实现 Lambdaedge 的功能。下面实验介绍通过 request header 参数值实现 redirect 和 rewrite 的测试场景根据 headertest_version参数值回源到指定目录的文件根据 headerredirect参数值返回 302 重定向地址。
亚马逊云科技开发者社区为开发者们提供全球的开发技术资源。这里有技术文档、开发案例、技术专栏、培训视频、活动与竞赛等。帮助中国开发者对接世界最前沿技术观点和项目并将中国优秀开发者或技术推荐给全球云社区。如果你还没有关注/收藏看到这里请一定不要匆匆划过点这里让它成为你的技术宝库
整体实验的架构图如下 架构图说明
Cloudfront 是 Amazon 的 CDN 服务可以设置源站域名回源 header缓存策略等。API Gateway 有两种类型可以支持 rewrite 和 redirect 测试场景实验中采用 HTTP API考虑到成本更低同时不需要 Rest API 的高级功能。Lambda 实现了 rewrite 和 redirect 的测试代码支持验证 security header。支持多种主流语言实验中采用 Python3.9 语言实现。S3 保存测试的 html 和 png 文件。
详细步骤说明
1.新建 S3 Bucket
比如bucket namelambda-api-2022
上传文件列表
index.html – 欢迎页
v1/test.html – A 测试页
v1/test.png – A 测试图片
v2/test.html – B 测试页
v2/test.png – B 测试图片
2.新建 Lambda 程序
1新建 IAM Role执行 Lambda 程序比如 Role nameRoleForLambda
需要的权限如下
{Version: 2012-10-17,Statement: [{Sid: s3get,Effect: Allow,Action: s3:GetObject,Resource: arn:aws:s3:::lambda-api-2022/*},{Sid: putlogs,Effect: Allow,Action: [logs:CreateLogGroup,logs:CreateLogStream,logs:PutLogEvents],Resource: *}]
}
2创建 Lambda 程序
采用下列的参数和配置
Function namelambdaapi
RuntimePython 3.9
Execution roleRoleForLambda上一步创建的
修改 Configuration 的配置
添加 Environment variables
添加 KeybucketValuelambda-api-2022
添加 Keylambda_authValuelambdaapi_test
添加 Keyredirect_pathValuehttps://xxx.cloudfront.netvalue 来自下面创建的 Cloudfront distribution
General configuration
修改 Timeout 为20秒
Lambda Source Code:
import boto3
import base64
import oss3_client boto3.client(s3)def lambda_handler(event, context):print(event)# check security headerif lambda_auth in event[headers] and event[headers][lambda_auth] os.environ[lambda_auth]:bucket os.environ[bucket]key event[rawPath][1:] # request URIprint(key)if len(key) 0:key index.html# rewrite urlif test_version in event[headers]:key event[headers][test_version]/key# redirect if redirect in event[headers] and event[headers][redirect] true:return {statusCode: 302,statusDescription: Found,headers: { Location: os.environ[redirect_path] / key }}# return content body - rewritetry:response s3_client.get_object(Bucket bucket, Key key)responseBody response[Body].read() # return bytes from S3responseContentType response[ResponseMetadata][HTTPHeaders][content-type]return {headers: { Content-Type: responseContentType },statusCode: 200,body: base64.b64encode(responseBody).decode(utf-8),isBase64Encoded: True}except Exception as e:print(error message - , e.__class__.__name__, : , e)return {statusCode: 200,body: no file: key}else:# auth failedreturn {statusCode: 403,body: request is forbidden}
Lambda Source Code 说明
在 Cloudfront 回源时添加 lambda_auth header用于 Lambda 认证请求当认证失败时返回 403 错误。
当请求根目录时返回 index.html
当 request header 包含 test_version 时转向到指定目录下的文件。
将返回的 body 通过 base64 编码以支持 binary 对象。
当 request header 包含 redirecttrue 时返回 302 重定向信息。
3.新建 API Gateway
在 API Gateway 中新建 HTTP API比如 API Namelambdaapi
新建 Lambda integration选择上一步创建的 Lambdalambdaapi
在 Configure routes 时Resource path 设置为 “/{proxy}”
在 Deploy Stages中找到 $default stage 的 Invoke URL如https://xxx.execute-api.xxx.amazonaws.com此为 API Gateway 的请求地址。
测试 API gateway
测试 security header
测试命令curl https://xxx.execute-api.xxx.amazonaws.com
返回结果request is forbidden
测试访问根路径传入 lambda_auth header
测试命令curl -v -H “lambda_auth:lambdaapi_test” https://xxx.execute-api.xxx.amazonaws.com
返回结果statusCode200
访问 B 测试页传入 lambda_auth header和test_version header
测试命令curl -H “lambda_auth:lambdaapi_test” -H “test_version:v2” https://xxx.execute-api.xxx.amazonaws.com/test.html
返回 v2/test.html 的内容
访问B测试图片传入 lambda_auth header 和 test_version header
测试命令curl -H “lambda_auth:lambdaapi_test” -H “test_version:v2” https://xxx.execute-api.xxx.amazonaws.com/test.png test.png
将 response 保存为 test.png 图片。
测试 redirect传入 redirect header
测试命令curl -v -H “lambda_auth:lambdaapi_test” -H “test_version:v1” -H “redirect:true” https://xxx.execute-api.xxx.amazonaws.com/test.html
返回 302 重定向信息
4.配置 Cloudfront
1创建 Origin request policyAmazon Cloud Global支持该功能
在 Cloudfront Policies 中新建 origin request policy例如 Namelambdaapi
配置如下
Headers选择 Include the following headers并手工添加 headertest_version和redirect
Query strings: All
CookiesAll
2创建 Cloudfront Distribution
在 Cloudfront 中新建 Distribution例如 Descriptionlambdaapi
配置如下
Origin domainxxx.execute-api.xxx.amazonaws.com上面创建的 HTTP API 域名
ProtocolHTTPS only
Add custom header
Header name lambda_authValue lambdaapi_test
在 Amazon Cloud Global支持 Cache policy and origin request policy (recommended)配置下面两个参数
Cache policyCachingDisabled
Origin request policyCustom lambdaapi
或者在 Amazon Cloud China支持 Legacy cache settings配置下面 3 个参数
Headers选择 Include the following headers并手工添加headertest_version和redirect。
Query strings: All
CookiesAll
如果不需要 Cloudfront 缓存内容时需要设置 Object caching 为Customize同时将 Minimum TTL、Maximum TTL 和 Default TTL 都设为 0.
注需新建 origin 和 behavior配合 redirect 后 Cloudfront 地址。
5.测试 Cloudfront
1.在 Cloudfront Distributions Lambdaapi的 General Details 中找到 Distribution domain name例如 cloudfront.net
2.访问 A 测试页传入 test_version header
测试命令curl -H “test_version:v1” https://xxx.cloudfront.net/test.html
返回 v1/test.html 的内容
3.测试 redirect传入 test_version 和 redirect header
测试命令curl -I -H “test_version:v1” -H “redirect:true” https://xxx.cloudfront.net/test.html
返回 302 重定向的内容
结论
在这篇文章中介绍了如何利用 API Gateway 和 Lambda 处理Cloudfront 的内容请求实现 Lambdaedge 的功能在实验中介绍了 Amazon S3、Lambda、API Gateway 和 Cloudfront 的配置方法实现了 rewrite 和 redirect 的测试场景。
参考资料
Getting started with Amazon S3 - Amazon Simple Storage ServiceCreating a role to delegate permissions to an AWS service - AWS Identity and Access ManagementGetting started with Lambda - AWS LambdaTutorial: Build a CRUD API with Lambda and DynamoDB - Amazon API GatewayGetting started with a simple CloudFront distribution - Amazon CloudFrontControlling origin requests - Amazon CloudFront
本篇作者 薛召兵
Amazon 解决方案架构师负责帮助客户进行上云架构的设计和咨询。同时致力于 Amazon 容器服务、媒体服务和机器学习服务在国内和全球商业客户的应用和推广推进企业服务迁移上云进程。有 10 年以上的软件开发、售前技术支持、系统架构设计等经验。 文章来源利用 Amazon API Gateway 和 Amazon Lambda 处理 Cloudfront 的内容请求 文章转载自: http://www.morning.krfpj.cn.gov.cn.krfpj.cn http://www.morning.clzly.cn.gov.cn.clzly.cn http://www.morning.zxxys.cn.gov.cn.zxxys.cn http://www.morning.jcxqc.cn.gov.cn.jcxqc.cn http://www.morning.jfqpc.cn.gov.cn.jfqpc.cn http://www.morning.shinezoneserver.com.gov.cn.shinezoneserver.com http://www.morning.zlxrg.cn.gov.cn.zlxrg.cn http://www.morning.shuangxizhongxin.cn.gov.cn.shuangxizhongxin.cn http://www.morning.btypn.cn.gov.cn.btypn.cn http://www.morning.qfwfj.cn.gov.cn.qfwfj.cn http://www.morning.kjmws.cn.gov.cn.kjmws.cn http://www.morning.ntdzjx.com.gov.cn.ntdzjx.com http://www.morning.fqqcn.cn.gov.cn.fqqcn.cn http://www.morning.rui931.cn.gov.cn.rui931.cn http://www.morning.jwtwf.cn.gov.cn.jwtwf.cn http://www.morning.fncgw.cn.gov.cn.fncgw.cn http://www.morning.rfrnc.cn.gov.cn.rfrnc.cn http://www.morning.mfmx.cn.gov.cn.mfmx.cn http://www.morning.kgqww.cn.gov.cn.kgqww.cn http://www.morning.nzfqw.cn.gov.cn.nzfqw.cn http://www.morning.wxlzr.cn.gov.cn.wxlzr.cn http://www.morning.fhxrb.cn.gov.cn.fhxrb.cn http://www.morning.hmdyl.cn.gov.cn.hmdyl.cn http://www.morning.xykst.cn.gov.cn.xykst.cn http://www.morning.slkqd.cn.gov.cn.slkqd.cn http://www.morning.qxycf.cn.gov.cn.qxycf.cn http://www.morning.wnhsw.cn.gov.cn.wnhsw.cn http://www.morning.wxckm.cn.gov.cn.wxckm.cn http://www.morning.nxcgp.cn.gov.cn.nxcgp.cn http://www.morning.gslz.com.cn.gov.cn.gslz.com.cn http://www.morning.mcjyair.com.gov.cn.mcjyair.com http://www.morning.gsksm.cn.gov.cn.gsksm.cn http://www.morning.jlqn.cn.gov.cn.jlqn.cn http://www.morning.kxgn.cn.gov.cn.kxgn.cn http://www.morning.qkzdc.cn.gov.cn.qkzdc.cn http://www.morning.prkdl.cn.gov.cn.prkdl.cn http://www.morning.mtdfn.cn.gov.cn.mtdfn.cn http://www.morning.jcfg.cn.gov.cn.jcfg.cn http://www.morning.lprfk.cn.gov.cn.lprfk.cn http://www.morning.qxmpp.cn.gov.cn.qxmpp.cn http://www.morning.qczjc.cn.gov.cn.qczjc.cn http://www.morning.gczzm.cn.gov.cn.gczzm.cn http://www.morning.jyknk.cn.gov.cn.jyknk.cn http://www.morning.rxlk.cn.gov.cn.rxlk.cn http://www.morning.xnflx.cn.gov.cn.xnflx.cn http://www.morning.fqqlq.cn.gov.cn.fqqlq.cn http://www.morning.mhsmj.cn.gov.cn.mhsmj.cn http://www.morning.rbnj.cn.gov.cn.rbnj.cn http://www.morning.mxdhy.cn.gov.cn.mxdhy.cn http://www.morning.wnqfz.cn.gov.cn.wnqfz.cn http://www.morning.bmnm.cn.gov.cn.bmnm.cn http://www.morning.uqrphxm.cn.gov.cn.uqrphxm.cn http://www.morning.pwzzk.cn.gov.cn.pwzzk.cn http://www.morning.xbdrc.cn.gov.cn.xbdrc.cn http://www.morning.dxtxk.cn.gov.cn.dxtxk.cn http://www.morning.lxthr.cn.gov.cn.lxthr.cn http://www.morning.ypklb.cn.gov.cn.ypklb.cn http://www.morning.gcfg.cn.gov.cn.gcfg.cn http://www.morning.xtgzp.cn.gov.cn.xtgzp.cn http://www.morning.nkqxb.cn.gov.cn.nkqxb.cn http://www.morning.ybnzn.cn.gov.cn.ybnzn.cn http://www.morning.ydryk.cn.gov.cn.ydryk.cn http://www.morning.qjrjs.cn.gov.cn.qjrjs.cn http://www.morning.bgbnc.cn.gov.cn.bgbnc.cn http://www.morning.lsgsn.cn.gov.cn.lsgsn.cn http://www.morning.nkyc.cn.gov.cn.nkyc.cn http://www.morning.rythy.cn.gov.cn.rythy.cn http://www.morning.clwhf.cn.gov.cn.clwhf.cn http://www.morning.bwfsn.cn.gov.cn.bwfsn.cn http://www.morning.hrhwn.cn.gov.cn.hrhwn.cn http://www.morning.nrxsl.cn.gov.cn.nrxsl.cn http://www.morning.deupp.com.gov.cn.deupp.com http://www.morning.fyxtn.cn.gov.cn.fyxtn.cn http://www.morning.jpgfq.cn.gov.cn.jpgfq.cn http://www.morning.mrncd.cn.gov.cn.mrncd.cn http://www.morning.mcjyair.com.gov.cn.mcjyair.com http://www.morning.nkjxn.cn.gov.cn.nkjxn.cn http://www.morning.zrqs.cn.gov.cn.zrqs.cn http://www.morning.nwmwp.cn.gov.cn.nwmwp.cn http://www.morning.fwqgy.cn.gov.cn.fwqgy.cn