AWS HTTP API网关可用于移动应用程序的后端,它可以作为处理请求并将其转发到正确的微服务的入口点。以下是一个简单的示例,说明如何使用AWS HTTP API网关处理移动应用的请求。
首先,设置一个HTTP API网关,并将请求路由到后端Lambdas或其他微服务。
Resources:
MobileAPI:
Type: AWS::Serverless::HttpApi
Properties:
Name: mobile-api
Route1:
Path: /users
Method: GET
Integration:
Type: AWS_PROXY
Uri: arn:aws:lambda:us-west-2:123456789012:function:GetUsersLambda
在此示例中,当移动应用请求“/users”路径时,请求将通过AWS HTTP API网关集成到名为GetUsersLambda的Lambda函数中。您可以根据需要添加其他路由,以便将请求分配给不同的后端服务。
接下来,您可以使用AWS Cognito身份验证为移动应用的用户提供安全的请求访问。以下代码片段展示了如何使用AWS Cognito插件保护您的API。
Resources:
CognitoUserPool:
Type: AWS::Cognito::UserPool
Properties:
UserPoolName: mobile-users
CognitoUserPoolClient:
Type: AWS::Cognito::UserPoolClient
Properties:
UserPoolId: !Ref CognitoUserPool
ClientName: mobile-app
HttpApiWithAuth:
Type: AWS::Serverless::HttpApi
Properties:
Name: mobile-api-with-auth
CorsConfiguration:
AllowOrigins:
- '*'
AllowMethods:
- GET
- POST
- PUT
- DELETE
AllowHeaders:
- '*'
Authorization:
- 'AWS_IAM'
DefaultAuthorizer:
AuthorizerUri: !Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03