• lambda和DynamoDB连接


    1. 在DynamoDB当作创建一个新表,然后在项目当中创建,把string value的值填写一下。
    2. 在IAM创建角色附加 AWSLambdaDynamoDBExecutionRole权限
    3. 在lambda中创建函数,选择DynamoDB不附加其他条件的蓝图
    4. 选择第1步创建的DynamoDB表,完成lambda函数的创建。
    5. 打开lambda函数的index.js,代码进行重写,内容如下

    var AWS = require('aws-sdk');

    var dynamo = new AWS.DynamoDB({

        region: 'ap-northeast-1'

    });

    exports.handler = function(event, context) {

        var params = {

            "TableName": "sbrtest",         #表名

            "KeyConditionExpression":"this = :sbrtest",   #建表时候的关键字和函数名

            "ExpressionAttributeValues": {

                ":sbrtest" : {"S": event.sbrtest}    #函数名

            }

        };

        console.log("event:", event);

        dynamo.query(params, function(err, data) {

            console.log("dynamo_data:", data);

            console.log("dynamo_err:", err);

            context.done(null, data);

        });

    };

    1. 内容进行保存
    2. 对编辑好的表进行测试,测试代码如下

    {

      "Records": [

        {

          "eventID": "1",

          "eventVersion": "1.0",

          "dynamodb": {

            "Keys": {

              "Id": {

                "N": "101"

              }

            },

            "NewImage": {

              "Message": {

                "S": "New item!"

              },

              "Id": {

                "N": "101"

              }

            },

            "StreamViewType": "NEW_AND_OLD_IMAGES",

            "SequenceNumber": "111",

            "SizeBytes": 26

          },

          "awsRegion": "ap-northeast-1",

          "eventName": "INSERT",

          "eventSourceARN": "arn:aws:dynamodb:ap-northeast-1:382459439888:table/sbrtest0313",

          "eventSource": "aws:dynamodb"

        },

        {

          "eventID": "2",

          "eventVersion": "1.0",

          "dynamodb": {

            "OldImage": {

              "Message": {

                "S": "New item!"

              },

              "Id": {

                "N": "101"

              }

            },

            "SequenceNumber": "222",

            "Keys": {

              "Id": {

                "N": "101"

              }

            },

            "SizeBytes": 59,

            "NewImage": {

              "Message": {

                "S": "This item has changed"

              },

              "Id": {

                "N": "101"

              }

            },

            "StreamViewType": "NEW_AND_OLD_IMAGES"

          },

          "awsRegion": "ap-northeast-1",

          "eventName": "MODIFY",

          "eventSourceARN": "arn:aws:dynamodb:ap-northeast-1:382459439888:table/sbrtest0313",

          "eventSource": "aws:dynamodb"

        },

        {

          "eventID": "3",

          "eventVersion": "1.0",

          "dynamodb": {

            "Keys": {

              "Id": {

                "N": "101"

              }

            },

            "SizeBytes": 38,

            "SequenceNumber": "333",

            "OldImage": {

              "Message": {

                "S": "This item has changed"

              },

              "Id": {

                "N": "101"

              }

            },

            "StreamViewType": "NEW_AND_OLD_IMAGES"

          },

          "awsRegion": "ap-northeast-1",

          "eventName": "REMOVE",

          "eventSourceARN": "arn:aws:dynamodb:ap-northeast-1:382459439888:table/sbrtest0313",

          "eventSource": "aws:dynamodb"

        }

      ]

    }

    1. 函数页面会显示测试成功
    2. 在DynamoDB的触发选项中,也能够发现出现了新的数据。
  • 相关阅读:
    在VS Code中配置GO开发环境并调试
    go文件操作实践[读写zip tar xlsx文件]
    go 文件操作实践[读写json xlm gob txt]
    go inject 实践
    go的反射reflect
    go goroutine channel 和C# Task BlockingCollection 以及python该如何实现
    beego redis作为缓存
    beego Session redis存储以及是否阻塞
    Beego generate自动生成代码 以及注解路由 @router
    bee go用base64Captcha生成base64的验证码
  • 原文地址:https://www.cnblogs.com/cnqfz/p/8603139.html
Copyright © 2020-2023  润新知