码迷,mamicode.com
首页 > 其他好文 > 详细

[AWS Lambda] Including dependencies in your AWS Lambda function

时间:2016-05-08 23:46:35      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:

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.

[AWS Lambda] Including dependencies in your AWS Lambda function

标签:

原文地址:http://www.cnblogs.com/Answer1215/p/5472053.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!