AWS上多个API之间的共享python库 [英] Shared python libraries between multiple APIs on AWS

查看:115
本文介绍了AWS上多个API之间的共享python库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个使用AWS lambda运行的不同的python API(即python脚本).标准方法是生成一个zip文件,其中包括lambda函数所需的所有外部库,然后将其上传到AWS.现在,我有了一些不同API之间共有的功能(例如,自定义utils函数,例如解析文本文件或日期).目前,我正在复制每个zip文件中的文件utils.py.但是,这种方法效率很低(我不喜欢重复代码).我想要一个S3存储桶,其中包含我所有的.py共享文件,并让我的API直接加载这些文件.这可能吗? 一种简单的方法是将文件下载到tmp文件夹并加载它们,但是我不确定这是最好/最快的方法:

I have several different python APIs (i.e python scripts) that run using AWS lambda. The standard approach is to generate a zip file including all the external libraries that are necessary for the lambda function and then upload it to AWS. Now, I have some functions that are in common between different APIs (e.g. custom utils functions such as parse text files or dates). Currently, I am simpling duplicating the file utils.py in every zip file. However, this approach is quite inefficient (I don't like to duplicate code). I'd like to have a S3 bucket that contains all my .py shared files and have my APIs directly loading those. Is this possible? A simple approach would be to download the files to a tmp folder and load them, but I am not sure this is the best/fastest way:

import boto3
client_s3 = boto3.client("s3")
client_s3.download_file("mybucket", "utils.py", "/tmp/utils.py")

这可以用更优雅的方式完成吗?

Can this be done in a more elegant way?

推荐答案

实际上,这不是一个简单的解决问题.一段时间以来,我们一直在使用 lambda层 ,旨在解决该问题,因此您可以共享通用代码. lambda图层的问题在于,当您更改图层(图层+ lambda函数)中的某些内容时,您必须重新部署两次.脖子上的疼痛很快,就CICD而言,您可能还会遇到问题.

It's actually not a simple problem to solve. We've been using lambda layers for a while, that is designed to solve that issue, so you can share common code. The problem with lambda layers is that you have to re-deploy twice when you change something inside your layer (the layer + your lambda function). It's rapidly a pain in the neck, and in terms of CICD you might also have issues.

我们尝试了一段时间,现在返回包装代码,并将代码包含在lambda中.如果您想避免重复代码,但效率不高,但是至少您没有所有与您忘记部署依赖项功能有关的错误.

We tried this for some time, now we're back to packaging code and including the code inside the lambda. Not efficient if you want to avoid code duplicates but at least you don't have all the bugs related to the fact you forgot to deploy the dependency function.

这篇关于AWS上多个API之间的共享python库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆