美文网首页
AWS Lambda 操作 DynamoDB 【CRUD】

AWS Lambda 操作 DynamoDB 【CRUD】

作者: Hamiltonian | 来源:发表于2022-12-27 16:49 被阅读0次

    AWS Lambda 操作 DynamoDB 【CRUD】

    按照 AWS官方文档走不通,网上教程良莠不齐。特此记录下来供大家参考

    A.析构请求参数

    // 调试参数
    {
      "username": "value1",
      "password": "value2"
    }
    exports.handler = async (event, context, callback) => {
        
        //析构函数
         const { username, password } = event
          
        const response = {
            statusCode: 200,
            headers: {
                'Content-Type': 'text/html',
            },
            body: {
                'key':username
            },
        };
        return callback(null,response);
    };
    
    

    B1. 如何解决AccessDeniedException【is not authorized to perform】

    {
      "errorType": "AccessDeniedException",
      "errorMessage": "User: arn:aws:sts::224706089330:assumed-role/myfunc2-role-c74c5cwn/myfunc2 is not authorized to perform: dynamodb:PutItem on resource: arn:aws:dynamodb:eu-north-1:224706089330:table/newkey_media because no identity-based policy allows the dynamodb:PutItem action",
    }
    解决方法:
    A.在Lambda函数页面,选择Configuration=>Permissions=>Execution role
    B.Policy name确保有以下两项:AWSLambdaBasicExecutionRole、AmazonDynamoDBFullAccess
    

    B.增

    // Load the AWS SDK for Node.js
    var AWS = require('aws-sdk');
    // Set the region 
    AWS.config.update({region: 'eu-north-1'});
    
    // Create the DynamoDB service object
    var ddb = new AWS.DynamoDB({apiVersion: '2012-08-10'});
    
    var params = {
      TableName: 'newkey_media',
      Item: {
        'new_entity' : {S: '000099'},
        'list' : {S: 'Richard Roe'}
      }
    };
    
    
    // // Create DynamoDB document client
    // var docClient = new AWS.DynamoDB.DocumentClient({apiVersion: '2012-08-10'});
    
    exports.handler = async (event, context, callback) => {
        
    // Call DynamoDB to add the item to the table
      await ddb.putItem(params, function(err, data) {
      if (err) {
        console.log("Error", err);
        callback(err);
      } else {
        console.log("Success", data);
        callback(null,data);
      }
    }).promise();
    
    };
    
    

    C.删

    // Load the AWS SDK for Node.js
    var AWS = require('aws-sdk');
    // Set the region 
    AWS.config.update({region: 'eu-north-1'});
    
    // Create the DynamoDB service object
    var ddb = new AWS.DynamoDB({apiVersion: '2012-08-10'});
    
    var params = {
      TableName: 'newkey_media',
     Key: {
        'new_entity': {S: '001'}
      }
    };
    
    
    // // Create DynamoDB document client
    // var docClient = new AWS.DynamoDB.DocumentClient({apiVersion: '2012-08-10'});
    
    exports.handler = async (event, context, callback) => {
        
    // Call DynamoDB to add the item to the table
      await ddb.deleteItem(params, function(err, data) {
      if (err) {
        console.log("Error", err);
        callback(err);
      } else {
        console.log("Success", data);
        callback(null,data);
      }
    }).promise();
    };
    

    D.查

    // Load the AWS SDK for Node.js
    var AWS = require('aws-sdk');
    // Set the region 
    AWS.config.update({region: 'eu-north-1'});
    
    // Create the DynamoDB service object
    var ddb = new AWS.DynamoDB({apiVersion: '2012-08-10'});
    
    var params = {
      TableName: 'newkey_media'
    };
    
    
    // // Create DynamoDB document client
    // var docClient = new AWS.DynamoDB.DocumentClient({apiVersion: '2012-08-10'});
    
    exports.handler = async (event, context, callback) => {
        
    // Call DynamoDB to add the item to the table
      await ddb.scan(params, function(err, data) {
      if (err) {
        console.log("Error", err);
        callback(err);
      } else {
        console.log("Success", data);
        callback(null,data);
      }
    }).promise();
    
    };
    
    

    E.改

    // Load the AWS SDK for Node.js
    var AWS = require('aws-sdk');
    // Set the region 
    AWS.config.update({region: 'eu-north-1'});
    
    // Create the DynamoDB service object
    var ddb = new AWS.DynamoDB({apiVersion: '2012-08-10'});
    
    var params = {
      TableName: 'newkey_media',
      Item: {
        'new_entity' : {S: '000099'},
        'list' : {S: 'Renkkkkkkkk'}
      }
    };
    
    
    // // Create DynamoDB document client
    // var docClient = new AWS.DynamoDB.DocumentClient({apiVersion: '2012-08-10'});
    
    exports.handler = async (event, context, callback) => {
        
    // Call DynamoDB to add the item to the table
      await ddb.putItem(params, function(err, data) {
      if (err) {
        console.log("Error", err);
        callback(err);
      } else {
        console.log("Success", data);
        callback(null,data);
      }
    }).promise();
    
    };
    
    

    相关文章

      网友评论

          本文标题:AWS Lambda 操作 DynamoDB 【CRUD】

          本文链接:https://www.haomeiwen.com/subject/qszoqdtx.html