AreyousickofsendinglongcumbersomeURLviamailsorchatswhichlooksmessy.URLshortenerisagreatwayofmanagingsuchlongURLsandwhatsmoreamazingaboutitthatyoucanmakeitin-housewithaServerlestoServerlessthereisnothingbetterthanAWSManagedService.AWShasalreadysomeblogregardingthesame(checkreference)butunliketheblogs,ount,readabouttheservicesbeingusedandfollowthroughthesteps.
您是否厌倦了通过邮件或聊天发送冗长的URL,看起来很乱。URL缩短器是管理如此长的URL的一种好方法,更令人惊奇的是,您可以使用无服务器方法在内部将其缩短,从而降低维护应用程序的成本。谈到无服务器,没有什么比AWSManagedService好。AWS已经有一些与此相同的博客(检查参考),但是与博客不同,我们今天将使用DynamoDB代替AmazonS3,因为它具有非常高的访问速度。即使您不具备AWS的先验知识,也只需获得一个帐户,请阅读所使用的服务并按照步骤进行操作。
AWSDynamoDB:AmazonDynamoDBisakey-valueanddocumentdatabasethatdeliverssingle-digitmillisecondperformanceatanyscale.It’safullymanaged,multiregion,multimaster,durabledatabasewithbuilt-insecurity,backupandrestore,-scaleapplications.InourapplicationweareusingitasapersistentstoretostorethemappingsoflongURLsandtheirshorterversion.
AWSDynamoDB:AmazonDynamoDB是一个键值和文档数据库,可提供任意规模的毫秒级性能。它是一个完全托管的,多区域,多主机,持久的数据库,具有内置的安全性,备份和还原功能以及用于规模应用程序的内存缓存。在我们的应用程序中,我们将其用作持久性存储来存储长URL及其较短版本的映射。
AWSLambda:AWSLambdaletsyouruncodewithoutprovisioningormanagingservers.AlsobetterknowasFunctionAsAService(FaaS),putetimeyouconsume.Wehavetwolambdacodeswritten:Onefortoconvertlong_urlintoshort_urlandpushthedatatoDynamoDB.Anotherforretrievingthelong_urlfromthedynamotableevery-timeauservisitstheshort_url.
AWSLambda:AWSLambda允许您运行代码而无需置备或管理服务器。更好地称为“功能即服务”(FaaS),您只需为消耗的计算时间付费。我们编写了两个lambda代码:一个用于将long_url转换为short_url并将数据推送到DynamoDB的代码。另一个用于在用户每次访问short_url时从发电机表中检索long_url。
AWSAPIGateway:AmazonAPIGatewayisafullymanagedservicethatmakesiteasyfordeveloperstocreate,publish,maintain,monitor,andsecureAPIsatanyscale.APIsactasthe“frontdoor”essdata,businesslogic,orfunctionalityfromyourbackendservices.Forus,APIgatewayhas3methods:GET/admin→tohelpusloadtheindexpage,POST/create→tohelpusconvertthelong_urltoshort_url,GET/{short_id}→toredirecttheshort_urltothelongone.
AWSAPIGateway:AmazonAPIGateway是一项完全托管的服务,使开发人员可以轻松地创建,发布,维护,监控和保护各种规模的API。API充当应用程序从后端服务访问数据,业务逻辑或功能的“前门”。对于我们来说,API网关有3种方法:GET/admin→帮助我们加载索引页,POST/create→帮助我们将long_url转换为short_url,GET/{short_id}→将short_url重定向到长网址。
AWSCloudFront:work(CDN)servicethatsecurelydeliversdata,videos,applications,andAPIstocustomersgloballywithlowlatency,hightransferspeeds,allwithinadeveloper-friendlyenvironment.ForusitallowsustosetouroriginastheAPIgatewayendpointandcachesthecontentforus.
AWSCloudFront:AmazonCloudFront是一项快速的内容交付网络(CDN)服务,可在开发人员友好的环境中以低延迟,高传输速度安全地向全球客户交付数据,视频,应用程序和API。对于我们来说,它允许我们将源设置为API网关端点并为我们缓存内容。
AWSIAM:essManagement(IAM)esstoAWSservicesandresourcessecurely.WeusetotodefineRoleandPolicyforPermissionstocallDynamoDBfromLambda.
AWSIAM:essManagement(IAM)使您能够安全地管理对AWS服务和资源的访问。我们用于定义从Lambda调用DynamoDB的权限的角色和策略。
AWSRoute53:AmazonRoute53isahighlyavailableandscalablecloudDomainNameSystem(DNS)webservice.ItwillactasaDNSservicefortheacustomdomainthatyouwanttoassociatetoyourAPI.
AWSRoute53:AmazonRoute53是一种高度可用且可扩展的云域名系统(DNS)Web服务。它将充当您要与API关联的自定义域的DNS服务。
AWSACM:AWSCertificateManagerisaservicethatletsyoueasilyprovision,manage,anddeploypublicandprivateSecureSocketsLayer/TransportLayerSecurity(SSL/TLS)certificatesforusewithAWSservicesandyourinternalconnectedresources.Ifwehaveacustomdomain,ACMwillhelpusgetacustomcertificatefortheapplication.
AWSACM:AWSCertificateManager是一项服务,可让您轻松地调配,管理和部署公共和私有安全套接字层/传输层安全性(SSL/TLS)证书,以用于AWS服务和您的内部连接资源。如果我们有一个自定义域,ACM将帮助我们获得该应用程序的自定义证书。
ClientwillmakearequesttothecustomdomainwhichhitsRoute53
Route53entryforyourdomaintoresolvetotheCNAMEvalueofthetargetdomainnamewhichwillbecloudfrontdistribution(CDN)
您的域的Route53条目解析为目标域名的CNAME值,该目标域名将是CloudfrontDistribution(CDN)
YoucanregisterancertificateforyourDomainonACMandlinkACMtoCDNhelpstosecuretheconnection
APIGatewaysendaGETrequestto/adminandgetsaresponseastheindexpagewhereusercanenteralongURL
OnceUserentersthelongURLitsendsaPOSTrequestto/createmethodwhichcallsalambdafunction
用户输入长网址后,它将向/create方法发送POST请求,该方法将调用lambda函数
ThelambdafunctionstoresentryonDynamodbtableandreturnstheshortURLtotheuser
8.WhenuserenterstheShortURLitcallsaGETmethodfromtheAPIGatewaytoalambdafunction
9.ThelambdafunctionlooksupinthedynamotableandgivesbackthelongURL
10.APIGatewayprovidesaredirection(HTTP301statuscode)tothelongurl
CreateanIAMPolicywiththe:lambda-dynamodb-url-shortener
MakesureuenterthecorrectdetailsforAWS-REGION(wheredynamotableiscreate),AWS-ACCOUNTandDYNAMO-TABLE(inourcaseitisurl-shortener-table)
确保您输入AWS-REGION(在其中创建发电机表的位置),AWS-ACCOUNT和DYNAMO-TABLE(在我们的情况下为url-shortener-table)的正确详细信息
CreateanIAMRole:lambda-dynamodb-url-shortener-role
AttachtheIAMPolicies:AWSLambdaBasicExecutionandtheonecreatedabovelambda-dynamodb-url-shortener
附加IAM策略:AWSLambdaBasicExecution和在lambda-dynamodb-url-shortener上方创建的策略
Python3.6ROLE:lambda-dynamodb-url-shortener-role
Python3.6作用:lambda-dynamodb-url-shortener-role
mentsinthefunctiontounderstandbetter.Makesuretosettheregionanddynamodbtablenametoappropriatevalue.
将代码添加到函数中请注意,我已经在函数中添加了注释以更好地理解。确保将区域和dynamo数据库表名称设置为适当的值。
AddthecodebelowtothelambdafunctionMakesuretosettheregionanddynamodbtablenametoappropriatevalue.
将以下代码添加到lambda函数中确保将region和dynamodb表名设置为适当的值。
BuildanewRESTAPIfromtheAPIGatewayconsole:url-shortener-api!
从APIGateway控制台构建新的RESTAPI:url-shortener-api!
NowwewillsetupanAPImethodsothatifweenterariableAPP_URLurl-shortener-create
6.将CDN端点 /t添加到lambda函数环境变量APP_URLurl-shortener-createshortenerurl-shortener-create
CDNendpoint /admingivesusthehomepageResponsebackastheshortURl
Ourbasicexecutionoftheapplicationisover.Thesearesomeoftheaddedstepswhichcanbeperformedbyyou:
[1]/dynamodb[2]/lambda[3]/api-gateway[4]/api-gateway[5]/iam[6]/route53[7]/cloudfront[8]/certificate-manager[9]pute/build-a-serverless-private-url-shortener[10]/blog//11/30/how-to-setup-a-serverless-url-shortener-with-api-gateway-lambda-and-dynamodb-on-aws
[1]/dynamodb[2]/lambda[3]/api-gateway[4]/api-gateway[5]/iam[6]/route53[7]/cloudfront[8]/certificate-manager[9]pute/build-a-serverless-private-url-shortener[10]/blog//11/30/how-to-setup-a-serverless-url-shortener-with-api-gateway-lambda-and-dynamodb-on-aws
翻译自:/@jeeri95/serverless-url-shortener-using-aws-97fc475e
1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。