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

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

  1. 如何使用Java创建AWS Lambda函数

    在本教程中,我们将看到如何在Java中创建AWS Lambda函数,我告诉你,这样做非常容易-- 基本上,我们可以通过三种方式创建AWS Lambda函数: –通过实现RequestHandler接口 ...

  2. 功能样式:Lambda函数和映射

    一等函数:Lambda函数和映射 什么是一流的功能? 您之前可能已经听过它说某种特定的语言是有用的,因为它具有"一流的功能".正如我在本系列关于函数式编程的第一篇文章中所说,我不同 ...

  3. aws docker_深入介绍AWS上的Docker

    aws docker Container virtualization - most visibly represented by Docker - is a server paradigm that ...

  4. docker 网络配置_Kafka的AWS Docker网络设置

    Kafka是一个分布式流处理平台,最近几年获得了长足的发展和进步.这篇文章主要针对在AWS 上部署 Kafka Docker镜像的注意事项.其中最容易出问题的部分就是Kafka的listeners配置 ...

  5. Spring Boot项目集成AWS SDK连接到AWS S3,实现上传下载功能

    本文主要描写在Spring Boot项目里集成AWS SDK连接到AWS S3,实现上传下载功能的具体代码和注意事项.如有不足和错误之处,欢迎指正. AWS S3相关介绍 AWS S3(官网): ht ...

  6. 云智慧监控宝Docker监控功能评测

    之前看到dockone社区<[实战]五个Docker监控工具的对比>(http://dockone.io/article/397)的文章,前两天也尝试了新上线的Docker监控工具监控宝. ...

  7. 使用AWS CloudWatch 调优Lambda函数 | 技术头条

    戳蓝字"CSDN云计算"关注我们哦! 技术头条:干货.简洁.多维全面.更多云计算精华知识尽在眼前,get要点.solve难题,统统不在话下! 译者:风车牛马 整理:刘丹 Kyle ...

  8. aws lambda_为什么我会自动删除所有旧的推文以及我用来执行此操作的AWS Lambda函数...

    aws lambda From now on, my tweets are ephemeral. Here's why I'm deleting all my old tweets, and the ...

  9. node aws 内存溢出_如何使用Node.js和AWS快速创建无服务器RESTful API

    node aws 内存溢出 by Mark Hopson 马克·霍普森(Mark Hopson) 如何使用Node.js和AWS快速创建无服务器RESTful API (How to quickly ...

最新文章

  1. SQL的基本数据类型
  2. pandas使用groupby函数计算dataframe数据中每个分组的N个数值的滚动最小值(rolling min)、例如,计算某公司的多个店铺每N天(5天)的滚动销售额最小值
  3. 开放产品开发(OPD):Archi 汉化工具下载
  4. 【深度学习】有个洋人小哥把整个 CNN 都给可视化了,卷积、池化清清楚楚!...
  5. web服务器-Apache
  6. android开发中的数据库SQLite的使用
  7. 接好!畅销数学界的科普书,我们免费送!
  8. 爬虫时安装的newspaper 新闻包
  9. 温馨剪纸风三八妇女节PSD分层海报模板
  10. tenorflow+python程序打包成.exe(收集方法1)
  11. 决策树_预测泰坦尼可号幸存者
  12. needs to declare permission android.permission.REQUEST_INSTALL_PACKAGES
  13. HTML从入门到入土 - CSS基础
  14. springboot+高校自习室座位管理小程序 毕业设计-附源码191028
  15. 离散数学中的x|y是什么意思?
  16. Deprecated:function eregi() is deprecated in /usr/local/apache/libraries/lib_lang.php on line 8
  17. python修改sheet名称_openpyxl修改sheet名,sheet颜色,删除sheet的方法
  18. 柠檬水找零---贪心算法(c++)实现
  19. 语法分析 自顶向下分析
  20. mysql语句添加、删除索引(转)

热门文章

  1. 异常的分类 java 1615309080
  2. 吃货阶段02 商品类的定义 需求 0925
  3. dj鲜生-32-用户中心-收货地址
  4. 爬虫-02-了解http与https
  5. django-pycharm下省略python manage.py的办法
  6. python-演练-输出一个等腰三角形-
  7. dojo 官方翻译 dojo/json 版本1.10
  8. 在windows下rust编译出现gcc.exe的错误
  9. 私有云为先 ZStack还在谋划一个更大的混合云世界
  10. React学习资料+css进阶资料总结