zl程序教程

您现在的位置是:首页 >  其他

当前栏目

[AWS] What is Serverless Aurora

AWS is What Serverless
2023-09-27 14:23:24 时间

 

 

一、了解 serverless DB 

Ref: AWS Aurora Serverless Tutorial

Create a MySQL database by Aurora.

The capacity type is Serverless.

选中 Data API.

 

 

Subnets 应该如何指定??
 

通过Query Editor链接数据库,并执行SQL语句。

 

  • 内部测试

 创建一个表,添加一条数据,然后再查询之,即可完成简单测试。

CREATE TABLE Customers (
  CustomerId int,
  FirstName varchar(255)
)

INSERT INTO Customers (CustomerId, FirstName) VALUES (1, 'John Doe')

SELECT * FROM Customers

 

 

二、实例数据库

Ref: AWS RDS Aurora Postgres Database Setup | Step by Step Tutorial

  产品时,不需要public access。debugging/dev时,可以使用public access。

 

 

三、结合Lambda

Ref: Create a Serverless Backend on AWS with Lambda and Aurora | Step by Step Tutorial

Ref: Building serverless applications with Amazon Aurora Serverless【官方文档,step by step 的 tutorials】 

记得选中 Data API,能够允许 SQL HTTP endpoint. 然后,使用SQL Editor测试一下。

开始配置 Lambda:

其中,secretArn涉及到另一个服务:Secrets Manager(保存了数据库的配置信息,也就是这些信息的密文)

安全密钥存储,视频中:13:40开始。加密后,获得 Secret ARN。

 

  • Serverless API 调用测试

import json
import boto3

rds_client = boto3.client('rds-data')

database_name = 'serverlessdemo'
db_cluster_arn = 'arn:aws:rds:us-east-1:xxxxxxxxxxxxx:cluster:auroraserverlessdemo'
db_credentials_secrets_store_arn = 'arn:aws:secretsmanager:us-east-1:xxxxxxxxxxxx:secret:rds-db-credentials/cluster-xxxxxxxxxxxxxxxxxx/admin-LraHcS'

# entrance.
def lambda_handler(event, context):
    response = execute_statement('SELECT * FROM serverlessdemo.Customers');
    return response;
    
    
def execute_statement(sql):
    response = rds_client.execute_statement(
        secretArn=db_credentials_secrets_store_arn, 
        database=database_name, 
        resourceArn=db_cluster_arn, 
        sql=sql)
    return response;

其中,在创建数据库时,有顺便在 AWS Secrets Manager 中创建密钥相关的东东,也就是这里的 secretArn 会有用到。

 

  • 替换为新权限

创建Lambda时,会有一个默认的role,包含了一些默认的权限。

准备好新权限;否则,会遇到权限问题:AccessDeniedException。

添加:AuroraLambdaRole,该role包含以下policies。

 

再回到Lambda去测试,可获得正确结果如下,cheers!

...
...
"records": [ [ { "longValue": 1 }, { "stringValue": "John Doe" } ] ] }

 

 

Django 的 ORM 能否顺利与其对接?

Yes, it can do. Deploying completely serverless Django with Zappa and Aurora Serverless

实践中遇到的问题,总结在如下集合中。

 

 

  • 配置 Static files 

命令 python manage.py collectstatic 的作用:

详解django中的collectstatic命令以及STATIC_URL、STATIC_ROOT配置

  

  • Setup Serverless MySQL Database

过程当中,是新建的vpc subnet 以及 security group。NAT 没有启动。 

使用zappa后,也有了对应的命令行,非常棒。

zappa manage dev migrate 

 

 

 

 

常见问题集合


一、sqlite版本问题 

Ref: SQLite ImproperlyConfigured exception #1880

pollsapi$ zappa tail
Calling tail for stage dev..
[1626348636051] Instancing..
[1626348639806] SQLite 3.8.3 or later is required (found 3.7.17).: ImproperlyConfigured
Traceback (most recent call last):
  File "/var/task/handler.py", line 657, in lambda_handler
  return LambdaHandler.lambda_handler(event, context)
  File "/var/task/handler.py", line 251, in lambda_handler

 

zappa的本身的问题,将本地的virtual env一起打包,包括sqlite3/base.py。在内部 改掉 这个version check即可

 

  • MySql类似的问题

Ref: 解决django.core.exceptions.ImproperlyConfiguredmysqlclient 1.3.13 or

修改 ~/.virtualenvs/building-api-django/lib/python3.6/site-packages/django/db/backends/mysql/base.py

use_precompiled_packages 可以阻止zappa使用默认的package,但这里暂不需要这个。 

 

二、设置CORS 只能Json格式

Ref: https://docs.aws.amazon.com/zh_cn/AmazonS3/latest/userguide/ManageCorsUsing.html

[
    {
        "AllowedHeaders": [],
        "AllowedMethods": [
            "GET"
        ],
        "AllowedOrigins": [
            "*"
        ],
        "ExposeHeaders": [
            "Authorization"
        ],
        "MaxAgeSeconds": 3000
    }
]

 

 

 

 To-do list.