简体   繁体   English

将 AWS Lambda Function 代码直接存储在 S3 存储桶中

[英]Storing AWS Lambda Function code directly in S3 Bucket

AWS Lambda Functions have an option to enter the code uploaded as a file from S3. AWS Lambda 函数可以选择输入从 S3 作为文件上传的代码。 I have a successfully running lambda function with the code taken as a zip file from an S3 Bucket, however, any time you would like to update this code you would need to either manually edit the code inline within the lambda function or upload a new zip file to S3 and go into the lambda function and manually re-upload the file from S3. I have a successfully running lambda function with the code taken as a zip file from an S3 Bucket, however, any time you would like to update this code you would need to either manually edit the code inline within the lambda function or upload a new zip文件到 S3 和 go 到 lambda function 并手动从 S3 重新上传文件。 Is there any way to get the lambda function to link to a file in S3 so that it will automatically update its function code when you update the code file (or zip file) contained in S3? Is there any way to get the lambda function to link to a file in S3 so that it will automatically update its function code when you update the code file (or zip file) contained in S3?

Lambda doesn't actually reference the S3 code when it runs--just when it sets up the function. Lambda 在运行时实际上并没有引用 S3 代码——只是在它设置 function 时。 It is like it takes a copy of the code in your bucket and then runs the copy.这就像它在您的存储桶中获取代码的副本,然后运行该副本。 So while there isn't a direct way to get the lambda function to automatically run the latest code in your bucket, you can make a small script to update the function code using SDK methods. So while there isn't a direct way to get the lambda function to automatically run the latest code in your bucket, you can make a small script to update the function code using SDK methods. I don't know which language you might want to use, but for example, you could write a script to call the AWS CLI to update the function code.我不知道您可能想要使用哪种语言,但例如,您可以编写一个脚本来调用 AWS CLI 来更新 function 代码。 See https://docs.aws.amazon.com/cli/latest/reference/lambda/update-function-code.html请参阅https://docs.aws.amazon.com/cli/latest/reference/lambda/update-function-code.html

Updates a Lambda function's code.更新 Lambda 函数的代码。

The function's code is locked when you publish a version.发布版本时,函数的代码被锁定。 You can't modify the code of a published version, only the unpublished version.您不能修改已发布版本的代码,只能修改未发布版本的代码。

See also: AWS API Documentation另请参阅:AWS API 文档

See 'aws help' for descriptions of global parameters.有关全局参数的说明,请参阅“aws 帮助”。

Synopsis概要

update-function-code --function-name [--zip-file ] [--s3-bucket ] [--s3-key ] [--s3-object-version ] [--publish |更新功能代码 --function-name [--zip-file ] [--s3-bucket ] [--s3-key ] [--s3-object-version ] [--publish | --no-publish] [--dry-run | --no-publish] [--dry-run | --no-dry-run] [--revision-id ] [--cli-input-json ] [--generate-cli-skeleton ] --no-dry-run] [--revision-id ] [--cli-input-json ] [--generate-cli-skeleton ]

You could do similar things using Python or PowerShell as well, such as using https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/lambda.html#Lambda.Client.update_function_code您也可以使用 Python 或 PowerShell 来做类似的事情,例如使用https://boto3.amazonaws.com/v1/lambdalateClient/

You can set up an AWS Code deploy pipeline to get your code build and deployed on code commit in your code repository(github,bitbucket,etc)您可以设置AWS 代码部署管道,以便在代码存储库中构建和部署代码(github、bitbucket 等)

CodeDeploy is a deployment service that automates application deployments to Amazon EC2 instances, on-premises instances, serverless Lambda functions, or Amazon ECS services. CodeDeploy 是一种部署服务,可自动将应用程序部署到 Amazon EC2 实例、本地实例、无服务器 Lambda 功能或 Amazon ECS 服务。

Also, wanted to add if you want to go on a more unattended route of deploying your Updated code to the Lambda use this flow in your code Pipeline此外,如果您想要 go 在更无人看管的路线上将更新后的代码部署到 Lambda,请在您的代码管道中使用此流程
Source -> Code Build (npm installs and zipping etc.) -> S3 Upload (sourcecode.zip in S3 bucket) -> Code Build (another build just for aws lambda update-funtion-code) Source -> Code Build (npm installs and zipping etc.) -> S3 Upload (sourcecode.zip in S3 bucket) -> Code Build (another build just for aws lambda update-funtion-code)

Make sure the role for the last stage has both S3 getObject and Lambda UpdateFunctionCode policies attached to it.确保最后阶段的角色同时附加了S3 getObjectLambda UpdateFunctionCode策略。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM