[AWS Lambda] Including dependencies in your AWS Lambda function

4559 단어
This lesson teaches you how to use dependencies, such as faker.js in your Lambda functions. We will update the lambda code from the Lambda and API Gateway lesson to generate random work history and endorsements using the faker npm module then upload our code to Lambda and test.
 
When you code have dependencies. You cannot just write code in AWS inline editor. You have to zip your code and upload to the AWS. 
 
index.js:
var faker = require('faker');

exports.handler = function(event, context){
    var career = {};
    career.firstName = event.firstName;
    career.lastName = event.lastName;
    career.socialMediaName = getSocialMediaName();
    career.careerHistory = [];
    for (var i =0; i<5; i++){
        var company = {};
        company.companyName = getCompanyName();
        company.desc = getCompanyDesc();
        company.companyTitle = getTitle();
        company.startDate = getDate();
        company.city = getCity();
        company.state = getState();
        company.country = getCountry();
        career.careerHistory.push(company);
    }

    career.endorsements = [];
    for(var i = 0; i<20; i++){
        career.endorsements.push(getEndorsement());
    }

    context.succeed(career);

};

function getSocialMediaName() {
    return faker.internet.userName();
}

function getCompanyName() {
    return faker.company.companyName();
}


function getCompanyDesc() {
    return faker.company.bs();
}

function getTitle() {
    return faker.company.bsAdjective() + " " + faker.company.bsAdjective() + " " + faker.company.bsNoun();
}

function getDate() {
    return faker.date.past();
}

function getCity() {
    return faker.address.city();
}

function getState() {
    return faker.address.state();
}

function getCountry() {
    return faker.address.country();
}

function getEndorsement() {
    return faker.company.catchPhrase();
}

 
Zip index.js and node_modules folder into a zip folder, then upload to the Lambda, save and test. It may report some error, but if you test from Postman, if it works if its fine.

좋은 웹페이지 즐겨찾기