aws-lambda
AWS Lambda con S3
Ricerca…
introduzione
Gli sviluppatori Lambda gestiranno i problemi che richiedono l'utilizzo di altre risorse AWS. Questo argomento si concentra su S3 (Simple Storage Service) che verrà comunemente utilizzato per archiviare file statici e altre configurazioni. Questa documentazione prenderà in considerazione l'utilizzo di AWS-SDK in lambda, l'accesso ai file in S3 da Lambda e l'attivazione delle funzioni Lambda quando viene generato un evento S3
Cheatsheet
AWS-SDK per javascript
Lambda contiene aws-sdk ( https://aws.amazon.com/sdk-for-node-js/) nella sua versione globale, quindi non è necessario caricare questo modulo nodo nello zip.
const AWS = require('aws-sdk');
Funzione di esempio
module.exports.myFunction = (event, context, callback) => {
const response = {
statusCode: 200,
body: 'Hello Lambda!',
};
return callback(null, response);
};
Esecuzione di S3
const s3 = new AWS.S3 ();
Utilizzare con 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
Autorizzato sotto CC BY-SA 3.0
Non affiliato con Stack Overflow