Call aws-cli from AWS Lambda

前端 未结 5 1990
我寻月下人不归
我寻月下人不归 2020-12-02 20:03

is there ANY way to execute aws-cli inside AWS Lambda? It doesn\'t seem to be pre-installed. (I\'ve checked with \"which aws\" via Node.js child-process, and it didn\'t exis

5条回答
  •  旧时难觅i
    2020-12-02 20:41

    aws-cli is a python package. To make it available on a AWS Lambda function you need to pack it with your function zip file.

    1) Start an EC2 instance with 64-bit Amazon Linux;

    2) Create a python virtualenv:

    mkdir ~/awscli_virtualenv
    virtualenv ~/awscli_virtualenv
    

    3) Activate virtualenv:

    cd ~/awscli_virtualenv/bin
    source activate
    

    4) Install aws-cli and pyyaml:

    pip install awscli
    python -m easy_install pyyaml
    

    5) Change the first line of the aws python script:

    sed -i '1 s/^.*$/\#\!\/usr\/bin\/python/' aws
    

    6) Deactivate virtualenv:

    deactivate
    

    7) Make a dir with all the files you need to run aws-cli on lambda:

    cd ~
    mkdir awscli_lambda
    cd awscli_lambda
    cp ~/awscli_virtualenv/bin/aws .
    cp -r ~/awscli_virtualenv/lib/python2.7/dist-packages .
    cp -r ~/awscli_virtualenv/lib64/python2.7/dist-packages .
    

    8) Create a function (python or nodejs) that will call aws-cli:

    For example (nodejs):

    var Q = require('q');
    var path = require('path');
    var spawn = require('child-process-promise').spawn;    
    
    exports.handler = function(event, context) {
    
        var folderpath = '/folder/to/sync';
        var s3uel = 's3://name-of-your-bucket/path/to/folder';
    
        var libpath = path.join(__dirname, 'lib');
        var env = Object.create(process.env);
        env.LD_LIBRARY_PATH = libpath;
    
        var command = path.join(__dirname, 'aws');
        var params = ['s3', 'sync', '.', s3url];
        var options = { cwd: folderpath };
    
        var spawnp = spawn(command, params, options);
    
        spawnp.childProcess.stdout.on('data', function (data) {
            console.log('[spawn] stdout: ', data.toString());
        });
    
        spawnp.childProcess.stderr.on('data', function (data) {
            console.log('[spawn] stderr: ', data.toString());
        });
    
        return spawnp
        .then(function(result) {
    
            if (result['code'] != 0) throw new Error(["aws s3 sync exited with code", result['code']].join(''));
    
            return result;
    
        });
    
    }
    

    Create the index.js file (with the code above or your code) on ~/awscli_lambda/index.js

    9) Zip everything (aws-cli files and dependencies and your function):

    cd ~
    zip -r awscli_lambda.zip awscli_lambda
    

提交回复
热议问题