aws-lambda
AWS Lambda with S3
サーチ…
前書き
ラムダの開発者は、他のAWSリソースの使用を必要とする問題を処理します。このトピックでは、静的ファイルやその他の構成を格納するために一般的に使用されるS3(Simple Storage Service)に焦点を当てています。このドキュメントでは、ラムダのAWS-SDKの使用、ラムダのS3のファイルへのアクセス、S3のイベントが発生したときのラムダ関数のトリガについて検討します
カンニングペーパー
javascript用のAWS-SDK
Lambdaには、グローバルにaws-sdk( https://aws.amazon.com/sdk-for-node-js/)が含まれているため、このノードモジュールをzipにアップロードする必要はありません。
const AWS = require('aws-sdk');
サンプル関数
module.exports.myFunction = (event, context, callback) => {
const response = {
statusCode: 200,
body: 'Hello Lambda!',
};
return callback(null, response);
};
S3の実行
const s3 = new AWS.S3();
Elasticache Redisでの使用
//make sure redis node-module is added in zip
const redis = require('redis');
//the redis information should be stored in the environment, not hard coded
const redis_options = {
host: process.env.REDIS_HOST,
port: process.env.REDIS_PORT
};
module.exports.myFunction = (event, context, callback) => {
try {
let client = redis.createClient(redis_options);
context.callbackWaitsForEmptyEventLoop = false;
client.on('connect', () => {
console.log('Connected:', client.connected);
});
client.on('end', () => {
console.log('Connection closed.');
});
client.on('ready', function () {
console.log('Connection ready.');
client.keys('*', (err, keys) => {
//always quit the redis client when no longer needed
//else the connection will be used up
client.quit();
const response = {
statusCode: 200,
body: keys,
};
return callback(null, response);
});
} catch (err) {
if (client) { client.quit();}
console.log('Error!: ' + err.message);
callback(err);
}
};
Modified text is an extract of the original Stack Overflow Documentation
ライセンスを受けた CC BY-SA 3.0
所属していない Stack Overflow