[AWS] Serverless Kinesis and Dynamodb
AWS and Serverless
2023-09-14 08:59:13 时间
Kinesis Stream
Add Kinesis
serverless.yml, add following configuration
resources:
Resources:
orderEventsStream:
Type: AWS::Kinesis::Stream
Properties:
Name: order-events
ShardCount: 1
Add DynamoDB
serverless.yml, add following configuration
resources:
Resources:
orderEventsStream:
Type: AWS::Kinesis::Stream
Properties:
Name: order-events
ShardCount: 1
orderTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: ordersTable
AttributeDefinitions:
- AttributeName: "orderId"
AttributeType: "S"
KeySchema:
- AttributeName: "orderId"
KeyType: "HASH"
BillingMode: PAY_PER_REQUEST
When lambda handler
get trigger, what we want is 2 steps:
- Save the order to DynamoDB
- Place the order to Kinesis Stream
handler.js
:
"use strict";
const orderManager = require("./orderManager");
const createResponse = async (statusCode, message) => {
const response = {
statusCode: statusCode,
body: JSON.stringify(message),
};
return response;
};
module.exports.createOrder = async (event) => {
const body = JSON.parse(event.body);
const order = orderManager.createOrder(body);
return orderManager
.placeNewOrder(order)
.then(() => {
return createResponse(200, order);
})
.catch((error) => {
return createResponse(400, error);
});
};
orderManager.js
:
"use strict";
const { v4: uuid } = require("uuid");
const AWS = require("aws-sdk");
const dynamo = new AWS.DynamoDB.DocumentClient();
const kinesis = new AWS.Kinesis();
const TABLE_NAME = process.env.orderTableName;
const STREAM_NAME = process.env.orderStreamName;
module.exports.createOrder = (body) => {
const order = {
orderId: uuid(),
name: body.name,
address: body.address,
productId: body.productId,
quantity: body.quantity,
orderDate: Date.now(),
eventType: "order_placed",
};
return order;
};
module.exports.placeNewOrder = (order) => {
// save order in table
return saveNewOrder(order).then(() => {
return placeOrderStream(order);
});
};
function saveNewOrder(order) {
const params = {
TableName: TABLE_NAME,
Item: order,
};
return dynamo.put(params).promise();
}
function placeOrderStream(order) {
const params = {
Data: JSON.stringify(order),
PartitionKey: order.orderId,
StreamName: STREAM_NAME,
};
return kinesis.putRecord(params).promise();
}
TABLE_NAME
& STREAM_NAME
are coming from enviorment variables.
functions:
createOrder:
handler: handler.createOrder
# trigger for lambda
events:
# http: stands for APIGateWay
- http:
path: /order
method: post
environment:
orderTableName: ordersTable
orderStreamName: order-events
Enable Presmissions
In AWS, you need to give premissions everytime, none of the premissions are given in the begining.
serverless.yml:
provider:
name: aws
runtime: nodejs12.x
lambdaHashingVersion: 20201221
apiGateway:
shouldStartNameWithService: true
iam:
role:
statements:
- Effect: Allow
Action:
- dynamodb:PutItem
- dynamodb:GetItem
Resource:
- arn:aws:dynamodb:#{AWS::Region}:#{AWS::AccountId}:table/ordersTable
- Effect: Allow
Action:
- kinesis:PutRecord
Resource:
- arn:aws:kinesis:#{AWS::Region}:#{AWS::AccountId}:stream/order-events
相关文章
- Linux系统:第十二章:AWS服务器X86架构安装配置Mysql与MongoDB
- aws linux no python application found, check your startup logs for errors
- AWS re:Invent2022大会回顾:Nitro的安全设计
- AWS 2022 re:Invent 观察 | 天下武功,唯快不破
- 芯片创新如何成为AWS成功背后的 "秘诀"
- AWS回应Elastic修改开源协议:创建“真正”开源的Elasticsearch分支
- MySQL Error number: MY-011416; Symbol: ER_KEYRING_AWS_FAILED_TO_SET_REGION; SQLSTATE: HY000 报错 故障修复 远程处理
- Oracle 视图 DBA_AWS 官方解释,作用,如何使用详细说明
- MySQL中的OR与AND操作符比较(mysqlor和and)
- 多条件查询MySQL中使用And多条件查询的步骤(mysql中and)
- 在 AWS 中使用 Ansible 来管理你的 SSH 密钥
- AWS 将提供 8TB 或 16TB 内存的新型实例
- 继谷歌云TPU之后,AWS也推出 AI 芯片 Inferentia
- 因 AWS 数据中心断电,1TB 数据丢失了
- 深入浅出:MySQL中AND和OR运算符使用方法(mysql中and和or)
- Mysql中强大的AND运算符的使用方法探究(mysql中and的使用)
- MySQL中的AND和OR使用逻辑运算符优化查询语句(mysql中and与or)
- AWS改变MySQL配置,创建更加美好的未来(aws改mysql配置)
- AWS搜索 进入MySQL引擎的新时代(aws搜索MySQL引擎)
- azure sql比较分析AWS MySQL 与 Azure SQL的区别(aws mysql 还是)
- 控制利用AWS建立MySQL用户控制权限(aws mysql 用户)
- AWS MySQL备份确保数据安全(aws mysql 备份)
- 云之上,MySQL进阶之路(aws mysql 升级)
- 深入探索AWS与Oracle的强强合作(aws与oracle)
- JEDI后,AWS与微软Azure“再战”100亿美元云计算大单