aws云服务器怎么使用
Are you sick of sending long cumbersome URL via mails or chats which looks messy. URL shortener is a great way of managing such long URLs and whats more amazing about it that you can make it in-house with a Serverless approach which lowers your cost of maintaining an application. When it comes to Serverless there is nothing better than AWS Managed Service. AWS has already some blog regarding the same(check reference) but unlike the blogs, we will use DynamoDB today instead Amazon S3 since it has very high access speed. Even if you have no prior knowledge of AWS just get an account, read about the services being used and follow through the steps.
您是否厌倦了通过邮件或聊天发送冗长的URL,看起来很乱。 URL缩短器是管理如此长的URL的一种好方法,更令人惊奇的是,您可以使用无服务器方法在内部将其缩短,从而降低维护应用程序的成本。 谈到无服务器,没有什么比AWS Managed Service好。 AWS已经有一些与此相同的博客(检查参考),但是与博客不同,我们今天将使用DynamoDB代替Amazon S3,因为它具有非常高的访问速度。 即使您不具备AWS的先验知识,也只需获得一个帐户,请阅读所使用的服务并按照步骤进行操作。
AWS服务以构建无服务器URL应用程序 (AWS Services to build a Serverless URL application)
AWS DynamoDB : Amazon DynamoDB is a key-value and document database that delivers single-digit millisecond performance at any scale. It’s a fully managed, multiregion, multimaster, durable database with built-in security, backup and restore, and in-memory caching for internet-scale applications. In our application we are using it as a persistent store to store the mappings of long URLs and their shorter version.
AWS DynamoDB: Amazon DynamoDB是一个键值和文档数据库,可提供任意规模的毫秒级性能。 它是一个完全托管的,多区域,多主机,持久的数据库,具有内置的安全性,备份和还原功能以及用于Internet规模应用程序的内存缓存。 在我们的应用程序中,我们将其用作持久性存储来存储长URL及其较短版本的映射。
AWS Lambda : AWS Lambda lets you run code without provisioning or managing servers. Also better know as Function As A Service(FaaS), you pay only for the compute time you consume. We have two lambda codes written: One for to convert long_url into short_url and push the data to DynamoDB. Another for retrieving the long_url from the dynamo table every-time a user visits the short_url.
AWS Lambda: AWS Lambda允许您运行代码而无需置备或管理服务器。 更好地称为“功能即服务”(FaaS),您只需为消耗的计算时间付费。 我们编写了两个lambda代码:一个用于将long_url转换为short_url并将数据推送到DynamoDB的代码。 另一个用于在用户每次访问short_url时从发电机表中检索long_url。
AWS API Gateway : Amazon API Gateway is a fully managed service that makes it easy for developers to create, publish, maintain, monitor, and secure APIs at any scale. APIs act as the “front door” for applications to access data, business logic, or functionality from your backend services. For us, API gateway has 3 methods: GET /admin → to help us load the index page, POST /create → to help us convert the long_url to short_url, GET /{short_id} → to redirect the short_url to the long one.
AWS API Gateway: Amazon API Gateway是一项完全托管的服务,使开发人员可以轻松地创建,发布,维护,监控和保护各种规模的API。 API充当应用程序从后端服务访问数据,业务逻辑或功能的“前门”。 对于我们来说,API网关有3种方法:GET / admin→帮助我们加载索引页,POST / create→帮助我们将long_url转换为short_url,GET / {short_id}→将short_url重定向到长网址。
AWS CloudFront : Amazon CloudFront is a fast content delivery network (CDN) service that securely delivers data, videos, applications, and APIs to customers globally with low latency, high transfer speeds, all within a developer-friendly environment. For us it allows us to set our origin as the API gateway endpoint and caches the content for us.
AWS CloudFront: Amazon CloudFront是一项快速的内容交付网络(CDN)服务,可在开发人员友好的环境中以低延迟,高传输速度安全地向全球客户交付数据,视频,应用程序和API。 对于我们来说,它允许我们将源设置为API网关端点并为我们缓存内容。
AWS IAM : AWS Identity and Access Management (IAM) enables you to manage access to AWS services and resources securely. We use to to define Role and Policy for Permissions to call DynamoDB from Lambda.
AWS IAM: AWS Identity and Access Management(IAM)使您能够安全地管理对AWS服务和资源的访问。 我们用于定义从Lambda调用DynamoDB的权限的角色和策略。
AWS Route53 : Amazon Route 53 is a highly available and scalable cloud Domain Name System (DNS) web service. It will act as a DNS service for the a custom domain that you want to associate to your API.
AWS Route53: Amazon Route 53是一种高度可用且可扩展的云域名系统(DNS)Web服务。 它将充当您要与API关联的自定义域的DNS服务。
AWS ACM : AWS Certificate Manager is a service that lets you easily provision, manage, and deploy public and private Secure Sockets Layer/Transport Layer Security (SSL/TLS) certificates for use with AWS services and your internal connected resources. If we have a custom domain, ACM will help us get a custom certificate for the application.
AWS ACM: AWS Certificate Manager是一项服务,可让您轻松地调配,管理和部署公共和私有安全套接字层/传输层安全性(SSL / TLS)证书,以用于AWS服务和您的内部连接资源。 如果我们有一个自定义域,ACM将帮助我们获得该应用程序的自定义证书。
架构图和工作流程 (Architectural Diagram and Workflow)
When user wants to shorten the URL:
当用户想缩短URL时:
Client will make a request to the custom domain which hits Route53
客户端将向命中Route53的自定义域发出请求
Route53 entry for your domain to resolve to the CNAME value of the target domain name which will be cloudfront distribution(CDN)
您的域的Route53条目解析为目标域名的CNAME值,该目标域名将是Cloudfront Distribution(CDN)
You can register an certificate for your Domain on ACM and link ACM to CDN helps to secure the connection
您可以在ACM上为您的域注册证书,并将ACM链接到CDN有助于保护连接
CDN has the origin setup as API gateway
CDN的原始设置为API网关
API Gateway send a GET request to /admin and gets a response as the index page where user can enter a long URL
API网关向/ admin发送GET请求,并获得响应作为索引页,用户可以在其中输入长网址
Once User enters the long URL it sends a POST request to /create method which calls a lambda function
用户输入长网址后,它将向/ create方法发送POST请求,该方法将调用lambda函数
The lambda function stores entry on Dynamo db table and returns the short URL to the user
lambda函数将条目存储在Dynamo db表上,并将短URL返回给用户
When user browses the short URL:
当用户浏览短网址时:
8. When user enters the Short URL it calls a GET method from the API Gateway to a lambda function
8.当用户输入短URL时,它将从API网关调用一个lambda函数的GET方法。
9. The lambda function looks up in the dynamo table and gives back the long URL
9. lambda函数在dynamo表中查找并返回长URL
10. API Gateway provides a redirection (HTTP 301 status code) to the long url
10. API网关提供了到长URL的重定向(HTTP 301状态代码)
DynamoDB (DynamoDB)
Create a Dynamo DB table:
url-shortener-table
创建Dynamo数据库表:
url-shortener-table
Add a Primary Key Value which is String :
short_id
添加一个主键值,它是String:
short_id
IAM政策 (IAM Policy)
Create an IAM Policy with the :
lambda-dynamodb-url-shortener
使用以下命令创建IAM策略:
lambda-dynamodb-url-shortener
Download Policy Details
下载政策详情
Make sure u enter the correct details for AWS-REGION(where dynamo table is create), AWS-ACCOUNT and DYNAMO-TABLE(in our case it is
url-shortener-table
)确保您输入AWS-REGION(在其中创建发电机表的位置),AWS-ACCOUNT和DYNAMO-TABLE(在我们的情况下为
url-shortener-table
)的正确详细信息
IAM角色 (IAM Role)
- Select Trusted Entity as Lambda from the Services 从服务中选择可信实体作为Lambda
Create an IAM Role:
lambda-dynamodb-url-shortener-role
创建IAM角色:
lambda-dynamodb-url-shortener-role
Attach the IAM Policies:
AWSLambdaBasicExecution
and the one created abovelambda-dynamodb-url-shortener
附加IAM策略:
AWSLambdaBasicExecution
和在lambda-dynamodb-url-shortener
上方创建的策略
Lambda创建短网址 (Lambda to create Short URL)
Create a Lambda function.
创建一个Lambda函数。
NAME:
名称:
url-shortener-create
url-shortener-create
RUNTIME:
运行:
Python 3.6
\ ROLE:lambda-dynamodb-url-shortener-role
\Python 3.6
\作用:lambda-dynamodb-url-shortener-role
\Add the Code to the Function Note that I have added comments in the function to understand better. Make sure to set the region and dynamo db table name to appropriate value.
将代码添加到函数中请注意,我已经在函数中添加了注释以更好地理解。 确保将区域和dynamo数据库表名称设置为适当的值。
- Add 3 environment variables to the lambda function. \ 将3个环境变量添加到lambda函数。 \
APP_URL : CLOUNDFRONT URL or CUSTOM DOMAIN URL to be added later (example : https://d24bkyagqs44nj.cloudfront.net/t/ )
MIN_CHAR : 12
MAX_CHAR : 16
Lambda创建检索长URL (Lambda to create Retrieve Long URL)
Create a Lambda function
创建一个Lambda函数
NAME:
名称:
url-shortener-retrieve
url-shortener-retrieve
RUNTIME:
运行:
Python 3.6
Python 3.6
ROLE:
角色:
lambda-dynamodb-url-shortener-role
\lambda-dynamodb-url-shortener-role
\Add the code below to the lambda function Make sure to set the region and dynamo db table name to appropriate value.
将以下代码添加到lambda函数中确保将region和dynamo db表名设置为适当的值。
创建新的API (Create NEW API)
Build a new REST API from the API Gateway console:
url-shortener-api
!从API Gateway控制台构建新的REST API:
url-shortener-api
!
为管理页面创建API资源 (Create API Resource for admin page)
Now we will set up an API method so that if we enter http://URL/admin
, it fetches our homepage.
现在,我们将设置一个API方法,以便如果输入 http://URL/admin
,它将获取我们的主页。
Create a new Resource from actions called :
/admin
. This will be location which displays the homepage通过名为
/admin
操作创建新的资源。 这是显示主页的位置
2. Create a GET
method with integration type as MOCK
. Once done it will show you a screen similar to the once below
2.创建一个集成类型为MOCK
的GET
方法。 完成后,将显示类似于以下内容的屏幕
3. Select the Intergration Response
under GET method
3.在“ GET”方法下选择“ Intergration Response
”
4. Under Mapping Templates
, click application\json
.
4.在Mapping Templates
,单击application\json
。
5. Copy paste the Code present here just like on the image shown below.
5.复制粘贴此处显示的代码,如下图所示。
创建API资源以缩短URL (Create API Resource to shorten the URL)
When we enter the long URL on the home page(http://URL/admin) and select the Button to shorten it, it send a POST request to the resource /create via API Gateway to trigger a lambda function.
当我们在首页( http:// URL / admin ) 上输入长URL 并选择Button来缩短它时,它将通过API网关向资源/ create发送POST请求以触发lambda函数。
Select the root resource
/
resource and then create another resource called/create
.选择根资源
/
资源,然后创建另一个名为/create
资源。
2. Create a POST
method, select Integration Type
as Lambda function url-shortener-create
which was used to convert long url to short.
2.创建一个POST
方法,选择Integration Type
作为Lambda函数url-shortener-create
,该函数用于将长url转换为短url。
3. Select the root resource /
resource and then create another resource called \t
. We do this do that any short url which is created will be of format http://URL/t/shortid
3.选择根资源/
资源,然后创建一个名为另一个资源\t
。 我们这样做是为了使所有创建的短网址都具有以下格式:http:// URL / t / shortid
创建用于缩短URL的API资源以重定向到长URL (Create API Resource for shorten URL to redirect to the long URL)
When we recieve the short url its in the format https://URL/t/xxxxx. What this does is when we provide the short url it triggers the lambda function url-shortener-retrieve
. This function returns {"statusCode": 301,"location": long_url}
. The Intergration Response takes the short url which we provide and redirects it to the long_url.
当我们收到短网址时,其格式为 https:// URL / t / xxxxx 。 这是当我们提供短网址时触发lambda函数 url-shortener-retrieve
。 此函数返回 {"statusCode": 301,"location": long_url}
。 集成响应采用我们提供的短网址,并将其重定向到long_url。
Select
/t
and create another resource/shortid
withResource Path
as{shortid}
.选择
/t
并创建另一个资源/shortid
,其Resource Path
为{shortid}
。
2. Create a /GET
method under it. Select the lambda as url-shortener-retrieve
which is used to return long url when we browse short url ![](images/Screenshot 2020-08-08 at 5.21.31 PM.png)
2.在其下创建一个/GET
方法。 选择lambda作为url-shortener-retrieve
,当我们浏览短URL时将其用于返回长url![](图片/屏幕快照2020-08-08,5.21.31 PM.png)
3. Go to the Integration Request
under the above created GET method
3.在上面创建的GET方法下转到Integration Request
4. Under Mapping Templates
, add ContentType as application/json
and add the below json
4.在Mapping Templates
,将ContentType添加为application/json
并添加以下json
{
"short_id": "$input.params('shortid')"
}
5. Under Method Response
for GET, delete the 200 HTTP status code and add a 301 status code which will be used for redirection.
5.在“ GET的Method Response
”下,删除200 HTTP状态代码并添加301状态代码,该代码将用于重定向。
6. Add Location
as the Response header ![](images/Screenshot 2020-08-08 at 5.22.18 PM.png)
6.添加Location
作为Response标头![](图片/屏幕截图2020-08-08,5.22.18 PM.png)
7. Under Integration Response
for the GET, delete the 200 Status Code and add 301
. Add Response hearder Location
value as integration.response.body.location
. This extracts the value from the lambda function ![](images/Screenshot 2020-08-08 at 5.24.44 PM.png)
7.在GET的“ Integration Response
”下,删除200状态代码并添加301
。 将Response listener Location
值添加为integration.response.body.location
。 这将从lambda函数![]中提取值(images / Screenshot 2020-08-08 at 5.24.44 PM.png)
8. Once done, Select the Deploy API
option from action, provide the stage name such as test and click Deploy. You will be provided with the endpoint followed by the stage name. ![](images/Screenshot 2020-08-08 at 5.51.38 PM.png)
8.完成后,从操作中选择Deploy API
选项,提供阶段名称(例如test),然后单击Deploy。 将为您提供端点,后跟阶段名称。 ![](图片/截屏2020-08-08,5.51.38 PM.png)
创建一个CloudFront分配 (Create a CloudFront Distribution)
Lets quickly create a Cloudfront distribution which can server as an endpoint and caching service for your application
让我们快速创建一个Cloudfront发行版,该发行版可以作为您的应用程序的端点和缓存服务
Enter the Origin Domain as the API gateway endpoint excluding th path(for example: in my case it is https://v2ohu2mu66.execute-api.us-east-2.amazonaws.com)
输入Origin域作为API网关终结点(不包括路径)(例如,在我的情况下为https://v2ohu2mu66.execute-api.us-east-2.amazonaws.com )
- Enter your Origin path. This basically will be the stage name when you deployed the API (for example: in my case it is /test) 输入您的原始路径。 基本上,这将是部署API时的阶段名称(例如:在我的情况下为/ test)
Under the cache behavior settings, add
Viewer Protocol Policy
asRedirect HTTP to HTTPS
在缓存行为设置下,将“
Viewer Protocol Policy
添加为“Redirect HTTP to HTTPS
Put
Allowed HTTP Methods
asGET, HEAD, OPTIONS, PUT, POST, PATCH, DELETE
将
Allowed HTTP Methods
放入GET, HEAD, OPTIONS, PUT, POST, PATCH, DELETE
- Then create the distribution. ![](images/Screenshot 2020–08–08 at 7.42.44 PM.png) 然后创建发行版。 ![](图片/ 2020年8月8日的屏幕截图,位于7.42.44 PM.png)
6. Add the CDN endpoint +/t
in the lambda function environment variable APP_URL url-shortener-create
6.将CDN端点+ /t
添加到lambda函数环境变量APP_URL url-shortener-create
shortener url-shortener-create
通过curl测试应用程序 (Test the Application via curl)
Jeeris-MacBook-Air:~ jdeka$ curl -XGET https://d24bkyagqs44nj.cloudfront.net/admin
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Private URL shortener</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script type="text/javascript"> $(document).ready(function() { // used only to allow local serving of files
$.ajaxSetup({
beforeSend: function(xhr) {
if (xhr.overrideMimeType) {
xhr.overrideMimeType("application/json");
}
}
});
........Jeeris-MacBook-Air:~ jdeka$ curl -XPOST -H "Content-Type: application/json" https://d24bkyagqs44nj.cloudfront.net/create -d '{"long_url": "https://www.google.com/search?q=helloworld"}'
{"short_id":"https://d24bkyagqs44nj.cloudfront.net/t/oVlz0wb6IHiEvXaD","long_url":"https://www.google.com/search?q=helloworld"}Jeeris-MacBook-Air:~ jdeka$ curl -XGET https://d24bkyagqs44nj.cloudfront.net/t/oVlz0wb6IHiEvXaD
{"statusCode": 301, "location": "https://www.google.com/search?q=helloworld"}
让我们检查应用程序 (Let Check the Application out)
CDN endpoint + /admin
gives us the home page Response back as the short URl
CDN端点+ /admin
为我们提供主页响应,返回为简短的URl
自定义域和保护应用程序 (Custom Domain and Securing the application)
Our basic execution of the application is over. These are some of the added steps which can be performed by you:
该应用程序的基本执行已结束。 这些是您可以执行的一些附加步骤:
- If we have a Custom Domain we can create a Route53 entry for our domain to resolve to the CNAME of the CDN 如果我们有一个自定义域,我们可以为我们的域创建一个Route53条目,以解析为CDN的CNAME
- Update the lambda function APP_URL to your custom domain 将lambda函数APP_URL更新为您的自定义域
- Request for a certificate to your domain from AWS Certificate Manager 从AWS Certificate Manager请求到您的域的证书
- Add the certificate in the Cloudfront Distribution 在Cloudfront发行版中添加证书
资料来源 (Sources)
[1] https://aws.amazon.com/dynamodb[2] https://aws.amazon.com/lambda[3] https://aws.amazon.com/api-gateway[4] https://aws.amazon.com/api-gateway[5] https://aws.amazon.com/iam[6] https://aws.amazon.com/route53[7] https://aws.amazon.com/cloudfront[8] https://aws.amazon.com/certificate-manager[9] https://aws.amazon.com/blogs/compute/build-a-serverless-private-url-shortener[10] https://blog.ruanbekker.com/blog/2018/11/30/how-to-setup-a-serverless-url-shortener-with-api-gateway-lambda-and-dynamodb-on-aws
[1] https://aws.amazon.com/dynamodb [2] https://aws.amazon.com/lambda [3] https://aws.amazon.com/api-gateway [4] https:/ /aws.amazon.com/api-gateway [5] https://aws.amazon.com/iam [6] https://aws.amazon.com/route53 [7] https://aws.amazon.com / cloudfront [8] https://aws.amazon.com/certificate-manager [9] https://aws.amazon.com/blogs/compute/build-a-serverless-private-url-shortener [10] https ://blog.ruanbekker.com/blog/2018/11/30/how-to-setup-a-serverless-url-shortener-with-api-gateway-lambda-and-dynamodb-on-aws
翻译自: https://medium.com/@jeeri95/serverless-url-shortener-using-aws-97f1929c475e
aws云服务器怎么使用