Creating a lambda function in AWS from zip file

后端 未结 6 1371
一整个雨季
一整个雨季 2020-12-12 20:26

I am trying to create a simple lambda function, and I\'m running into an error.

My code is basically

console.log(\'Loading function\');

exports.han         


        
6条回答
  •  醉梦人生
    2020-12-12 21:05

    Automation - using Grunt

    Complete AWS Lambda Seed project is available on Git.

    Step 1: Init npm module

    npm init

    Step 2: Install Grunt

    npm install --save-dev grunt grunt-cli

    Step 3: Install grunt-aws-lambda

    npm install --save-dev grunt-aws-lambda

    Step 4: Create Folder for Lambda service

    # Create directory
    mkdir lambdaTest
    
    # Jump into folder
    cd lambdaTest
    
    # Create service file
    touch lambdaTest.js
    
    # Initialize npm
    npm init
    

    Keep your logic/code into lambdaTest.js

    'use strict'
    
    exports.handler = (event, context, callback) => {
        console.log("Hello it's looks like working");
    };
    

    Step 5: Create Gruntfile.js

    Navigate back to root folder touch Gruntfile.js

    'use strict'
    module.exports = function (grunt) {
        grunt.initConfig({
            lambda_invoke: {
                lambdaTest: {
                    options: {
                        file_name: "lambdaTest/lambdaTest.js",
                        event: "lambdaTest/test.json",
                    }
                }
            },
            lambda_package: {
                lambdaTest: {
                    options: {
                        package_folder: 'lambdaTest/'
                    }
                }
            },
            lambda_deploy: {
                lambdaTest: {
                    arn: 'arn:aws:lambda:eu-central-1:XXXXXXXX:function:lambdaTest',
                    options: {
                        credentialsJSON: 'awsCredentials.json',
                        region: "eu-central-1"
                    },
                }
            },
        });
    
        grunt.loadNpmTasks('grunt-aws-lambda');
    
    
        grunt.registerTask('ls-deploy', ['lambda_package:lambdaTest', 'lambda_deploy:lambdaTest']);
    };
    

    Step 6: Create awsCredentials.js

    Create AWS IAM User with custom policy, Custom policy should have access to lambda:GetFunction, lambda:UploadFunction, lambda:UpdateFunctionCode, lambda:UpdateFunctionConfiguration and iam:PassRole

    {
        "accessKeyId": "XXXXXXXXXXXXXXXXXXXX",
        "secretAccessKey": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
    }
    

    Step 7: Create a zip and deploy to AWS Lambda.

    ls-deploy is custom task created by in Gruntfile above, which creates a zip of source code and deploy to Lambda.

    grunt ls-deploy

    Complete AWS Lambda Seed project is available on Git.

提交回复
热议问题