使用cloud9将AWS Lambda部署到另一个区域的最佳实践

时间:2019-01-25 16:55:39

标签: python bash amazon-ec2 aws-lambda aws-cloud9

问题:如果我将lambda部署在爱尔兰,则lambda无法使用在伦敦托管的s3触发器。理想情况下,我将在伦敦启动一个cloud9实例,但是该服务在伦敦尚不可用。

Cloud9非常适合测试和开发需要与其他AWS服务交互的Lambda,需要更改在本地系统上开发的其他AWS服务的代码才能进行部署,这显然是不理想的。因此,为什么我宁愿使用cloud9 IDE进行开发。

作为一种变通办法,我一直在开发基于爱尔兰的EC2实例,然后编写了一个脚本,其中包含复制lambda的基本文件并将其压缩并发送到s3存储桶的脚本。然后,我可以在任何地方生成lambda并从s3存储桶中导入zip。

但是,这似乎很麻烦,必须有更好的方法吗?

以下是代码示例(1)lambda

import json
import re
import boto3
import numpy    

s3 = boto3.client('s3')  

def lambda_handler(event, context):
    """DO SOMETHING"""

    return {
        'statusCode': 200,
        'body': json.dumps('lambda run sucessfully')
    }

(2)python脚本来打包lambda(调用bash脚本以zip格式)并将其发送到s3:

import os
import sys
import boto3
import subprocess


def package_lambda(packageName, pathToFiles):
    """Calls a bash script which packages the lambda function and 
        its dependencies into a zip"""

    print('calling bash script')
    subprocess.check_call(['/home/ec2-user/environment/shell_scripts/package_lambda.sh', packageName, pathToFiles])
    print('bash script finished')

    return

def moveFileToBucket(myfile, bucket):
    """pushes a file from the local system to an s3 bucket"""

    if os.path.exists(myfile):
        s3 = boto3.client('s3')
        key=str(myfile).split('/')[-1]
        s3.upload_file(myfile, bucket, key)
        print("{} uploaded sucessfully".format(myfile))
    else:
        raise Exception('No file named: '+myfile)

    return


if __name__=='__main__':
    package_lambda(sys.argv[1], sys.argv[2])
    moveFileToBucket(sys.argv[3], sys.argv[4])

(3)python脚本调用的bash脚本来压缩lambda:

#!/bin/bash

packageName=$1
pathToFiles=$2

ext1="_package"

# print path incase debuggin is needed
ls -lt $pathToFiles

# set variables
lambda=$pathToFiles/$packageName/$packageName/lambda_function.py
lambda_packages=$pathToFiles/$packageName/

# Remove any previous copies of the package
rm -r $pathToFiles/$packageName$ext1

# print paths for logging/debuggin purposes
ls $lambda
ls $lambda_packages

# copy the lambda
cp -r $lambda_packages $pathToFiles/$packageName$ext1
# move the lambda for packaging
cp $lambda $pathToFiles/$packageName$ext1/lambda_function.py

# remove directories and files which are not needed for zip
rm -r  $pathToFiles/$packageName$ext1/venv
rm -r  $pathToFiles/$packageName$ext1/$packageName
rm $pathToFiles/$packageName$ext1/__init__.py
rm $pathToFiles/$packageName$ext1/template.yaml
rm $pathToFiles/$packageName$ext1/requirements.txt

# move into the package copy to zip
cd $pathToFiles/$packageName$ext1
zip -r ../lambda_function.zip *

然后从命令行调用打包脚本,以将其发送到存储桶,以便可以将其部署到任何地方。

python3 /home/ec2-user/environment/pythonScripts/packageAndMoveFileToBucket.py myLambdaName /home/ec2-user/environment/pythonLambdaRepo /home/ec2-user/environment/pythonLambdaRepo/lambda_function.zip bucketForLambdas

也许社区会认为这是解决此问题的完美方法?但这感觉有点麻烦,而且我还没有看到亚马逊或其他地方的任何文档提出“更好”的方式?任何反馈都会很棒吗?

1 个答案:

答案 0 :(得分:0)

听起来您想更改AWS Cloud9 IDE当前设置的AWS区域,然后将AWS Lambda函数部署到该新区域。如果是这样,请参见以下说明,这些说明描述了如何更改区域:https://docs.aws.amazon.com/cloud9/latest/user-guide/lambda-functions.html#lambda-functions-prepare-region