具有lambda和fargate的事件驱动的容器

    科技2023-12-21  82

    TLDR (TLDR)

    Serverless containers provisioned by Fargate and triggered by Lambda good. PyODBC deployed on Lambda bad. Use Fargate for long-running jobs or jobs that are well supported as containers and minimize CICD build complexity.

    由Fargate设置并由Lambda good触发的无服务器容器。 在Lambda上部署PyODBC不好。 将Fargate用于长时间运行的工作或作为容器得到良好支持的工作,并最大程度地减少CICD的构建复杂性。

    PyODBC与Lambda (PyODBC vs Lambda)

    In the very recent past I was wrapping up what I thought to be the more challenging portion of a project; writing and testing the application. Whoops. Big mistake. Post-development the client scheduled a meeting titled “Integration of Project X into ACME Architecture”. Birthed from this meeting was the client’s desire to integrate this new function in an event-driven manner.

    在最近的过去,我总结了我认为是项目中更具挑战性的部分。 编写和测试应用程序。 哎呀 大错。 开发后,客户安排了一个名为“ 将Project X集成到ACME Architecture中 ”的会议。 这次会议的诞生是客户希望以事件驱动的方式集成此新功能。

    Normally, this type of nomenclature causes heavy salivation and dilated eyes. Whipping out functions to Lambda and watching the fruits fall from the serverless tree during a San Andreas shakedown are highlights of development. So, what’s my problem? MSSQL… That is correct. Microsoft SQL Server. Have you ever attempted to deploy a Lambda function written in Python 3.X that requires PyODBC drivers? Bring out the thumbscrews.

    通常,这种命名法会导致大量流涎和眼睛散开。 发展的亮点是,在Lambda上增加功能,并在San Andreas重组期间观察果实从无服务器的树上掉落。 那我有什么问题 MSSQL…是正确的。 Microsoft SQL Server。 您是否曾尝试部署需要PyODBC驱动程序的用Python 3.X编写的Lambda函数? 拔出指旋螺钉。

    Making lite of physical torture is in poor taste. Deploying PyODBC drivers is more emotional and mental abuse. Don’t believe me? Well, spend some of those free Medium credits and do some digging. Here is a brief summary of the steps.

    轻度施加酷刑的味道很差。 部署PyODBC驱动程序会更加情绪化和精神化。 不相信我吗 好吧,花一些免费的中级学分并做一些挖掘。 这是步骤的简要概述。

    You’ll need an EC2 running the same AMI as the Lambda you plan to use. Get that spun up.

    您将需要运行与打算使用的Lambda相同的AMI的EC2。 加快速度。 Update that EC2 with all the drivers, packages, and other goodies you will need.

    使用您需要的所有驱动程序,软件包和其他工具更新该EC2。 Pull your code.

    拉您的代码。 Bundle everything up in a friendly compressed format and send off to S3

    将所有内容打包为友好的压缩格式,然后发送到S3 Create a lambda to pull that package.

    创建一个lambda来拉该包。 Boom. Serverless MSSQL on Lambda.

    繁荣。 Lambda上的无服务器MSSQL。

    演示地址

    The emotional response from maintaining MSSQL driver for Python on Lambda 在Lambda上维护用于Python的MSSQL驱动程序的情感React

    肯定有更好的办法 (There Must Be a Better Way)

    Ever hear of Raymond Hettinger? A true pythonista; a core python developer for longer than many devs have been smashing keys for profit. One of his quotes that echoes throughout my working sessions is this:

    听说过Raymond Hettinger吗? 真正的pythonista; 一个比许多开发人员花更长的时间来破坏利润键的python核心开发人员。 在我的所有工作会议中回荡的他的一句话是:

    *slaps podium* There must be a better way! — Raymond Hettinger

    *在演讲台上打**必须有更好的方法! —雷蒙德·海廷格(Raymond Hettinger)

    Enter Fargate. Fargate is serverless compute for containers! A few steps of configuration, a push to your ECR, a dash of Lambda and boom, event-driven containers.

    输入Fargate 。 Fargate是用于容器的无服务器计算! 只需几个配置步骤,即可推动您的ECR,一些Lambda和繁荣事件驱动型容器。

    There was a blog post written by Rupak Ganguly from the Serverless team detailing the steps to configure a system with the architecture described in the diagram below. Users upload a file to S3. The s3:ObjectCreated:Put event triggers the associated Lambda, passing the Bucket and Key in the event payload. The Lambda passes the Bucket and Key as environment variables to the container running in Fargate. The container downloads the config file and executes its programmatic purpose.

    Serverless团队的Rupak Ganguly撰写了一篇博客文章,详细介绍了使用下图中描述的体系结构配置系统的步骤。 用户将文件上传到S3。 s3:ObjectCreated:Put事件触发关联的Lambda,并在event有效负载中传递Bucket和Key 。 Lambda将Bucket和Key作为环境变量传递给在Fargate中运行的容器。 容器下载配置文件并执行其编程目的。

    Event flow 活动流程

    Rupak Ganguly’s blog post is a fantastic generalized approach. While some familiarity with the Serverless framework and AWS are expected, even the uninitiated will be able to move through his example.

    Rupak Ganguly的博客文章是一种出色的通用方法。 虽然期望对无服务器框架和AWS有一定的了解,但是即使是没有经验的人也可以通过他的例子来学习。

    I can hear your thoughts. “Where is this dude’s value add if he is referencing another Medium article as the how-to section?” Thanks to your subconscious for bubbling up such an astute observation. Rupak Ganguly’s post does not deal with deployments within an existing secure VPC. Rupak guides us through a standalone deployment; no architectural constraints! Deploying inside a VPC with a self-hosted database requires a few key changes in the parameters and cloud setup. Aside from opening the subnet ports for inter-subnet requests to the database, you’ll need a NAT gateway so Fargate can download the container and the networkConfiguration portion of the ECS.run_task parameters populated with the correct subnets and security groups.

    我能听到你的想法。 “如果该花花公子将另一篇中型文章作为“操作方法”部分,那么他的增值值在哪里?” 感谢您的潜意识,冒犯了如此精明的观察。 Rupak Ganguly的帖子不涉及现有安全VPC中的部署。 Rupak指导我们进行独立部署; 没有架构上的限制! 在具有自托管数据库的VPC内进行部署需要对参数和云设置进行一些关键更改。 除了打开子网端口以向数据库发送子网间请求外,您还需要一个NAT网关,以便Fargate可以下载容器和ECS.run_task参数的networkConfiguration部分,其中填充了正确的子网和安全组。

    Here is a quick summary of the steps to take. These steps are a melange from the original work by Rupak and my efforts setting this up inside a client’s VPC.

    这是要采取的步骤的快速摘要。 这些步骤是Rupak原始工作的杂色,也是我在客户的VPC中进行设置的努力。

    Containerize your code

    容器化您的代码 Push to ECR

    推送至ECR

    Create ECS cluster pulling the image you just pushed to ECR; ${MY_ECR_IMAGE}. Make sure to set your number of instances to ZERO to avoid the service constantly running. When invoked from Lambda, you’ll provide a count override to provision a new container.

    创建ECS集群,将您刚刚推送到ECR的映像拉出; $ {MY_ECR_IMAGE}。 确保将实例数设置为零,以避免该服务不断运行。 从Lambda调用时,您将提供一个计数替代值以配置新的容器。

    Using the S3 triggered Lambda blueprint, create the new Lambda.

    使用S3触发的Lambda蓝图,创建新的Lambda。 Boto3 was already imported, you’ll need to instantiate the ECS client from the Boto3 package.

    Boto3已导入,您需要从Boto3程序包实例化ECS客户端。 Almost there! The params for the ECS.run_task(**kwargs) will be the penultimate step.

    差不多了! ECS.run_task(** kwargs)的参数将是倒数第二步。

    7. At this point, if you have attempted to fire off your event-driven service within that locked down VPC you should see a “Cannot Pull Container Error”. The final twist in this Rubik’s cube solution is NAT Gateway. These Network Address Translation Gateway allow for instances within the private subnet to have contact with the outside world. One requirement will be an allocated Elastic IP, which is limited to 5 per region per account.

    7.此时,如果您试图在锁定的VPC内启动事件驱动的服务,则应该看到“ Cannot Pull Container Error ”。 该Rubik多维数据集解决方案的最后一个难题是NAT Gateway 。 这些网络地址转换网关允许专用子网内的实例与外界联系。 一个要求是分配的弹性IP,每个帐户每个区域限制为5个。

    通过Lambda调用 (Invocation via Lambda)

    All devs love examples. I’ve included the parameters required to fire the Fargate task within the VPC. Firing a Lambda from S3 has become a trivial task thanks to Lambda Blueprints. The code snippet below is a modified version of that template. Here are the primary gotchas, all of which are parameters to the Fargate invocation.

    所有开发人员都喜欢示例。 我已经包含了在VPC中触发Fargate任务所需的参数。 借助Lambda蓝图,从S3发射Lambda已成为一项微不足道的任务。 下面的代码段是该模板的修改版本。 这是主要的陷阱,所有陷阱都是Fargate调用的参数。

    overrides : The name is the service name that you defined when setting up the Fargate cluster

    overrides :该name是您在设置Fargate集群时定义的服务名称

    environment : These are config file uploads triggering the execution, so my service requires updating the BUCKET and KEY environment vars.

    environment :这些是触发执行的配置文件上载,因此我的服务需要更新BUCKET和KEY环境变量。

    networkConfiguration : Inside a secured VPC, these settings become requirements. Listing out the subnets and security group(s) is all it takes to provide the necessary network information.

    networkConfiguration :在受保护的VPC内,这些设置成为要求。 列出子网和安全组是提供必要的网络信息所需要的。

    networkConfiguration : assignPublicIP=DISABLED This is the step where the NAT Gateway is required. Fargate requires internet access to pull the container from ECR. Read more of the AWS documentation to fully comprehend.

    networkConfiguration : assignPublicIP=DISABLED这是需要NAT网关的步骤。 Fargate需要访问Internet才能将容器从ECR中拉出。 阅读更多AWS文档以完全理解。

    """Lambda handler to pass config file from S3 event through Lambda to container provisioned on ECS Fargate."""import jsonimport urllib.parseimport boto3import oss3 = boto3.client('s3')def lambda_handler(event, context): # Get the bucket and key bucket = event['Records'][0]['s3']['bucket']['name'] key = urllib.parse.unquote_plus(event['Records'][0]['s3']['object']['key'], encoding='utf-8') try: # Populate the required parameters to invoke the container params = fargate_container_params(bucket, key) # Get the ECS client and submit the task ecs = boto3.client("ecs") response = ecs.run_task(**params) print(response) except Exception as e: print(e) raise edef fargate_container_params(bucket, key): return { "cluster": os.getenv("ECS_CLUSTER_NAME"), "count": 1, "launchType": "FARGATE", "taskDefinition": os.getenv("ECS_TASK_DEFINITION"), "overrides": { "containerOverrides": [{ "name": "My-Special-Service", "environment": [ { "name": "BUCKET", "value": bucket }, { "name": "KEY", "value": key } ] }] }, "networkConfiguration": { "awsvpcConfiguration":{ "subnets": ["vpc-subnet-1", "vpc-subnet-2], "securityGroups": ["sg-abc123"], "assignPublicIp": "DISABLED" } } }

    #获奖 (#Winning)

    We’ve reached the end, dear reader. I hope that the next time you’re faced with a complex Lambda dependency that you will turn to this new device is your arsenal. Size limits, run-time limits, dependency limits, all of them we can sidestep as easily as Roadrunner does Wile E. Coyote.

    亲爱的读者,我们已经到了尽头。 我希望下次您遇到复杂的Lambda依赖关系时,会使用此新设备作为您的武器库。 大小限制,运行时限制,依赖项限制,所有这些我们都可以像Roadrunner的Wile E. Coyote一样轻松地回避。

    Go build something awesome.

    去建立一些很棒的东西。

    演示地址

    Skeletor’s reaction after containerizing his domination service 骨架化将他的统治服务容器化之后的React

    翻译自: https://medium.com/@mileshill_40155/event-driven-containers-with-lambda-and-fargate-a2ca8940085f

    相关资源:四史答题软件安装包exe
    Processed: 0.013, SQL: 8