iT邦幫忙

2023 iThome 鐵人賽

DAY 16
0
Cloud Native

AWS AI交易室實戰系列 第 16

Day 16 - AWS API Gateway & Dynamo DB

  • 分享至 

  • xImage
  •  

~ 如果我有哆啦A夢的口袋,就不需要斷捨離了 ~
API Gateway - Lambda - DynamoDB

github: https://github.com/slindevel/modern-aws-marathon

今天我們來建立經典架構,所謂的 Serverless CRUD application,流程圖如上所示,從 API Gateway 透過 HTTP Restful 方式 invoke,中間觸發 lambda function 寫進 dynamodb 裡面

建立 Lambda 執行角色(execution role),並給予適當權限

$ aws iam create-role \        
--role-name ServerlessApiRole \
--assume-role-policy-document file://role-policy.json

//attach Lambda permissions
$ aws iam attach-role-policy \
--role-name ServerlessApiRole \                                                    
--policy-arn arn:aws:iam::aws:policy/AWSLambda_FullAccess

//attach cloudwatch permissions
$ aws iam attach-role-policy \
--role-name ServerlessApiRole \                                                    
--policy-arn arn:aws:iam::aws:policy/CloudWatchFullAccess

//attach dynamodb permissions
$ aws iam attach-role-policy \
--role-name ServerlessApiRole \                                                    
--policy-arn arn:aws:iam::aws:policy/AmazonDynamoDBFullAccess

建立 marathon-crud project

//in marathon-crud folder
$ zip function.zip *.py
$ aws lambda create-function \
--function-name marathon-dynamodb-function \
--zip-file fileb://function.zip \
--handler lambda_function.lambda_handler --runtime python3.11 \
--role arn:aws:iam::<account_id>:role/ServerlessApiRol

建立 dynamodb table

$ aws dynamodb create-table \
    --table-name students-data \
    --attribute-definitions \
        AttributeName=studentId,AttributeType=S \
    --key-schema \
        AttributeName=studentId,KeyType=HASH \
    --provisioned-throughput \
        ReadCapacityUnits=5,WriteCapacityUnits=5 \
    --table-class STANDARD

建立 API Gateway

注意最後 put integration 時 --uri 參數如下:
前面 arn:aws:apigateway:ap-northeast-1:lambda:path/2015-03-31/functions/ 固定
後面接 lambda 的 arn (Amazon Resource Name)
完整 uri: arn:aws:apigateway:ap-northeast-1:lambda:path/2015-03-31/functions/arn:aws:lambda:ap-northeast-1:<account_id>:function:marathon-dynamodb-function/invocations
建立步驟如下:

  1. 建立 rest api
  2. 建立子資源
  3. 在子資源上建立方法(http method)
  4. 將方法 (http method) 與 lambda 整合
  5. 最後部署 (deploy)
# 1. 建立 rest api
$ aws apigateway create-rest-api \
--name 'Marathon API' \
--description 'For marathon api'
{
    "id": <rest-api-id>,
    "name": "Marathon API",
    "description": "For marathon api",
   ...
    "rootResourceId": <parent-resource-id>
}

# 2. 建立子資源
$ aws apigateway create-resource \
--rest-api-id <rest-api-id> \
--parent-id  <parent-resource-id> \
--path-part 'student'
{
    "id": <child-resource-id>,
    "parentId": <parent-resource-id>,
    "pathPart": "student",
    "path": "/student"
}

# 3. 在子資源上建立方法(http method)
$ aws apigateway put-method --rest-api-id <rest-api-id> \
       --resource-id <child-resource-id> \
       --http-method POST \
       --authorization-type "NONE"

# 4. 將方法 (http method) 與 lambda 整合
$ aws apigateway put-integration \
--rest-api-id <rest-api-id> \
--resource-id <child-resource-id> \
--http-method POST \
--type AWS_PROXY \
--integration-http-method POST \
--uri arn:aws:apigateway:ap-northeast-1:lambda:path/2015-03-31/functions/\
arn:aws:lambda:ap-northeast-1:<account_id>:function:marathon-dynamodb-function/invocations

# 5. 最後部署 (deploy)
$ aws apigateway create-deployment --rest-api-id <rest-api-id> --stage-name dev

# 測試
# <stageName> here is 'dev'
$ curl -d "@payload.json" -X POST \
https://<rest-api-id>.execute-api.ap-northeast-1.amazonaws.com/<stageName>/student

接著我們可以建立其他操作的 api gateway,並以不同的 http method 呼叫看看

我們可以看到這個例子中,除了 API Gateway 提供了外部呼叫 lambda 的方式,DynamoDB 鬆散耦合的架構方式,也讓我們很方便地把我們想存的資料直接放到裡面而不受到 schema 的限制,就像小叮噹的口袋一樣

參考資料:

https://medium.com/featurepreneur/lambda-api-gateway-dynamodb-d8a35c379810
https://medium.com/aws-tip/crud-application-using-api-gateway-lambda-dynamodb-and-python-84d486c87df4
https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html


上一篇
Day 15 - AWS ECS & Fargate
下一篇
Day 17 - AWS Data Exchange
系列文
AWS AI交易室實戰30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言