aws docker_如何使用Docker的功能来摆脱AWS Lambda函数的困境

aws docker

by Liz Rice

丽兹·赖斯(Liz Rice)

如何使用Docker的功能来摆脱AWS Lambda函数的困境 (How to claw your way out of AWS Lambda function hell using the power of Docker)

When you’re building Lambda functions, it’s easy to get trapped in an “Invalid ELF header” nightmare. The problem is that your binaries are built for the wrong platform. Here’s what’s going on, and how you can fix it easily using Docker.

在构建Lambda函数时,很容易陷入“无效的ELF标头”噩梦。 问题是您的二进制文件是为错误的平台构建的。 这是正在发生的事情,以及如何使用Docker轻松修复它。

When most people get started with Lambda functions, they’ll use the online editor in the console to input their code. This is fine for your first example or two, but pretty soon you’ll want to do something wild and crazy like, y’know, import a library.

当大多数人开始使用Lambda函数时,他们将使用控制台中的在线编辑器输入代码。 这对于您的第一个或两个示例很好,但是很快您将想要做一些疯狂而疯狂的事情,例如,您知道导入一个库。

Over the past few weekends I’ve been working on my first Alexa skill for an Amazon Echo and I’ve reached that point where I want to use some third party code to add some additional functionality.

在过去的几个周末中,我一直在研究Amazon Echo的第一个Alexa技能,而现在我想使用一些第三方代码来添加一些其他功能。

The online editor simply lets you edit a single file. If you want to refer to other files — including imported libraries — you can upload them in a ZIP file (Amazon call this a deployment package). But if you‘re working on a Mac or a Windows computer, there’s a catch.

在线编辑器仅使您可以编辑单个文件。 如果您要引用其他文件(包括导入的库),则可以将它们上传到ZIP文件中(亚马逊称其为部署包 )。 但是,如果您在Mac或Windows计算机上工作,就会遇到麻烦。

When you use pip to install Python libraries on your laptop, it gives you binaries (.so files) that are built to run on your machine. But when the Lambda function runs in the AWS cloud it is going to be running on Linux — and binaries built for Mac (these are often called ‘darwin’ builds) or Windows won’t run on Linux (and vice versa).

当您使用pip在笔记本电脑上安装Python库时,它将为您提供可在计算机上运行的二进制文件(.so文件)。 但是,当Lambda函数在AWS云中运行时,它将在Linux上运行-并且为Mac(通常称为“ darwin”版本)构建的二进制文件或Windows将不会在Linux上运行(反之亦然)。

If you upload the Mac version, you’ll see “invalid ELF header” logs when you try to test your Lambda function.

如果您上传Mac版本,则在尝试测试Lambda函数时,您会看到“无效的ELF标头”日志。

So you’re going to need Linux versions of those library files. But what if you don’t have a Linux box to hand?

因此,您将需要这些库文件的Linux版本。 但是,如果您没有Linux设备,该怎么办?

You could grab yourself an EC2 instance from Amazon (or a droplet on Digital Ocean, or any Linux VM of your preference) but to my mind that’s quite a performance, and could even cost you a little bit of money (especially if you forget to take the EC2 box down again when you don’t need it).

您可以从Amazon(或Digital Ocean上的Droplet,或您喜欢的任何Linux VM)上获取EC2实例,但是在我看来,这是相当不错的性能,甚至可能会花一些钱(尤其是如果您忘记了不需要时将EC2盒再次放下)。

I think the easiest solution is to use Docker.

我认为最简单的解决方案是使用Docker。

Docker方法 (The Docker approach)

With Docker you can very easily can run a Linux container locally on your Mac, install the Python libraries within the container so they are automatically in the right Linux format, and zip up the files ready to upload to AWS. You’ll need Docker for Mac (or Windows) installed first.

借助Docker,您可以非常轻松地在Mac上本地运行Linux容器,在容器中安装Python库,以便它们自动以正确的Linux格式运行,并压缩文件以准备上传到AWS。 您首先需要安装适用于Mac(或Windows)的Docker

Spin up an Ubuntu container that can see the Lambda code you want to upload.

旋转一个可以查看您要上传的Lambda代码的Ubuntu容器。

docker run -v <directory with your code>:/working -it --rm ubuntu
  • The -vflag makes your code directory available inside the container in a directory called “working”.

    -v标志使您的代码目录在容器内的“工作”目录中可用。

  • The -itflag means you get to interact with this container.

    -it标志意味着您可以与此容器进行交互。

  • The --rm flag means Docker will remove the container when you’re finished.

    --rm标志表示完成后Docker将删除容器。

  • ubuntu is the name of an official container image containing, you guessed it, Ubuntu. If this container image isn’t already on your machine, Docker will download it for you.

    ubuntu是官方容器映像的名称,您猜中包含Ubuntu。 如果您的机器上还没有此容器映像,Docker将为您下载它。

You should now be inside the container at a shell prompt looking something like this:

您现在应该在容器提示符下的容器内,如下所示:

root@c1996f32a397:/#

Install pip and zip:

安装pip和zip:

$ apt-get update$ apt-get install python-pip$ apt-get install zip

Move into the working directory (you should be able to see your Lambda function code here):

移至工作目录(您应该可以在此处看到Lambda函数代码):

$ cd working

Use pip to get the library/ies you’re interested in. You can use the -t flag to tell pip to put the libraries here in the current directory, which will be more convenient later as it’s where the AWS deployment package wants them to be:

使用pip获取您感兴趣的库。您可以使用-t标志告诉pip将库放在当前目录中,这将在以后更加方便,因为这是AWS部署软件包希望它们是:

$ pip install -t . <library>

If you’re very curious, you can take a look to see what this installs. In my own case I installed the editdistance library, which gave me the following additional directories and files.

如果您很好奇,可以看看它的安装内容。 我自己安装了editdistance库,该库为我提供了以下其他目录和文件。

editdistance:__init__.py __init__.pyc _editdistance.h bycython.so def.h
editdistance-0.3.1.dist-info:DESCRIPTION.rst INSTALLER METADATA RECORD WHEEL metadata.json top_level.txt

You can see that bycython.so file? This is the correct, Linux version of the binary that AWS was objecting to when I hit the Invalid ELF header (shown in the error log screenshot above).

您可以看到bycython.so文件吗? 这是我击中Invalid ELF标头时AWS反对的二进制文件的正确Linux版本(如上面的错误日志屏幕截图所示)。

Create the ZIP file with your Lambda code (in my case, a single file called lambda_function.py) and the libraries (for me, the two editdistance directories and their contents.

使用您的Lambda代码(在我的情况下为一个名为lambda_function.py的文件)和库(对我来说,两个editdistance目录及其内容)创建ZIP文件。

$ zip linux-lambda.zip lambda_function.py
$ zip -r linux-lambda.zip edit*

The -r flag on zip tells it to recursively add the contents of directories.

zip上的-r标志告诉它以递归方式添加目录的内容。

Now you have an archive file called linux-lambda.zip which is ready to upload to AWS. And because the directory is mounted from the host (your Mac) into the container, you can simply upload the file into the console.

现在,您有了一个名为linux-lambda.zip的存档文件,可以将其上传到AWS。 而且由于目录是从主机(您的Mac)装载到容器中的,因此您只需将文件上传到控制台即可。

Back in the terminal type exit to quit the container, and it will be as if it never existed, except for the existence of the linux-lambda.zip file, which is still available on the host.

返回终端,然后exit以退出该容器,这就像从未存在过一样,只是存在linux-lambda.zip文件,该文件在主机上仍然可用。

Upload the ZIP file in the console, save it and try running a test. Invalid ELF header error no more!

在控制台中上传ZIP文件,将其保存并尝试运行测试。 无效的ELF标头错误不再存在!

If this article helps you out, please hit the ? button to make it easier for other people to find it. If you really like it, why not go bananas and share it too?

如果本文对您有所帮助,请点击? 按钮,使其他人更容易找到它。 如果您真的喜欢它,为什么不也去分享香蕉呢?

I’ve written a few other posts about what I’m learning as I write my first Alexa skill, like this one where I add database storage capabilities to my Lambda function. If you find them useful, you might be interested in a book I’m writing called Adventures with Alexa. Pick your own price!

当我写我的第一个Alexa技能时,我还写了一些其他关于我所学知识的文章,例如我在Lambda函数中添加数据库存储功能的文章 。 如果您发现它们有用,那么您可能会对我写的一本名为《 Alexa的冒险》的书感兴趣。 选择您自己的价格!

翻译自: https://www.freecodecamp.org/news/escaping-lambda-function-hell-using-docker-40b187ec1e48/

aws docker

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值