[AWS] Serverless Kinesis and Dynamodb
AWS and Serverless
2023-09-14 09:00:46 时间
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
相关文章
- 地主家真没粮了,基于AWS自研CPU的商业网卡问世!
- 教你处理 AWS Windows密码丢失重置的问题
- MySQL Error number: MY-011419; Symbol: ER_KEYRING_AWS_FAILED_TO_ACCESS_KEY_FROM_CONF_FILE; SQLSTATE: HY000 报错 故障修复 远程处理
- MySQL Error number: MY-011425; Symbol: ER_KEYRING_AWS_FAILED_TO_ACCESS_DATA_FILE; SQLSTATE: HY000 报错 故障修复 远程处理
- MySQL Error number: MY-011427; Symbol: ER_KEYRING_AWS_FAILED_TO_GET_KMS_CREDENTIAL_FROM_CONF_FILE; SQLSTATE: HY000 报错 故障修复 远程处理
- MySQL Error number: MY-011437; Symbol: ER_KEYRING_AWS_FAILED_TO_CONNECT_KMS; SQLSTATE: HY000 报错 故障修复 远程处理
- MySQL Error number: MY-011439; Symbol: ER_KEYRING_AWS_FAILED_TO_ENCRYPT_KEY; SQLSTATE: HY000 报错 故障修复 远程处理
- 清除AWS实例和卷详解程序员
- Oracle 视图 DBA_AWS 官方解释,作用,如何使用详细说明
- 如何在Amazon AWS上创建S3存储桶(对象存储)
- Amazon SageMaker加持,网约车领域开始有AWS智能语音方案了
- Twitter宣布牵手AWS交付其时间表,首次尝试使用公有云扩展实时服务
- 深入浅出:MySQL中AND和OR运算符使用方法(mysql中and和or)
- Oracle数据库中的AND查询(oracle and查询)
- 深入探究Mysql中IN与AND逻辑运算的应用(mysql中in与and)
- MySQL中AND的使用方法及含义解析(mysql中and的含义)
- MySQL中的AND逻辑操作符是什么(mysql中and是什么)
- 数据库使用AWS连接本地MySQL数据库(aws调用本地mysql)
- 灵活的云计算利用AWS S3和MySQL让你的业务更上一层楼(awss3mysql)
- 云服务器上MySQL的迁移AWS解决之道(aws mysql 迁移)
- 实现高可用性的AWS与MySQL复制(aws mysql 复制)
- 在AWS上搭建MySQL数据库系统的中文化指南(aws mysql 中文)
- 云上极致AWS支持Oracle创造无限可能(aws支持oracle)
- and的区别Oracle数据库中的OR与AND的差异(oracle中or和)
- JEDI后,AWS与微软Azure“再战”100亿美元云计算大单