4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

とりあえずLambda(Node)からDynamoDBにPutする

Posted at

やること

  • Lambdaに、AmazonDynamoDBFullAccessPolicyをAttachしたRoleを設定(IAMから設定)
  • AWSSDKのAWS.DynamoDB.DocumentClientを利用してPut
    • AWS.DynamoDBは使わない!(DynamoDB特有の書き方をしないといけなくなる。例えば、MapのListを保存しようと思った時には。。。)
      • 特有の書き方、例えばこんなの 'user_id': {'N': '12345'}

サンプル

const aws = require('./node_modules/aws-sdk');
const docClient = new aws.DynamoDB.DocumentClient();

const userInfo = {
	basic: {
		name: 'taro',
		age: 21,
		career: ['HOGE株式会社', 'HOGE2株式会社']
	},
	social: {
		twitter: 'https://hogehoge.twitter.com',
		facebook: 'https://hogehoge.facebook.com'
	}
}


const params = {
	TableName: 'user_sessions',
	Item: {
		user_id: 12345,
		last_accessed_at: (new Date()).getTime(),
		user_info: userInfo
	}
};

docClient.put(params, function(err, data) {
	if (err){
		console.log(err);
	} else {
		console.log(data);
	}
});
4
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?