简体   繁体   English

如何在 AWS lambda 中部署脚本

[英]How to deploy a script in AWS lambda

I have two scripts which I need to deploy in AWS lambda, I have never done it before, from the documentation I created kind of a few steps which would summarize the flow:我有两个脚本需要在 AWS lambda 中部署,我以前从未这样做过,从我创建的文档中创建了一些步骤来总结流程:

  1. Create a lambda function创建 lambda function
  2. Install boto3安装boto3
  3. Use invoke function使用invoke function

Lets say I have a simple function:假设我有一个简单的 function:

def first_function():
    return print('First function')

When I go to AWS -> Lambda -> Functions -> Create function I get to the configuration part where in the editor I see this:当我 go 到 AWS -> Lambda -> 功能 -> 创建 function 时,我进入了配置部分,在编辑器中我看到了这个:

import json

def lambda_handler(event, context):
    # TODO implement
    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!')
    }

Is this how I should edit this to deploy my function:这是我应该如何编辑它来部署我的 function:

import json

def lambda_handler(event, context):
    # TODO implement
    return {
       def first_function():
           return print('First function')
       first_function()
    }

Whatever functionality you need to implement in your lambda, you should write within the lambda_handler.无论您需要在 lambda 中实现什么功能,都应该在 lambda_handler 中编写。 If you want to refer to other smaller function you can define it outside the lambda handler function and can refer to it in the handler.如果要引用其他较小的 function 可以在 lambda 处理程序 function 之外定义它,并可以在处理程序中引用它。 So it might be like below所以它可能像下面

import x

def functiona():
    print(‘something’)

def functionb():
    print(‘somethingelse’)

def lambda_handler(event,context)
    print(‘lambda entry point)
    functiona()
    functionb()

Since the module will first be imported, you can still write code outside of functions although it is usually not a good practice since you cannot access the context and parameters you have sent to lambda.由于模块将首先被导入,您仍然可以在函数之外编写代码,尽管这通常不是一个好习惯,因为您无法访问已发送到 lambda 的上下文和参数。

Tha lambda_handler that shows up when you create a function in the console is simply boiler plate code.当您在控制台中创建lambda_handler时出现的 lambda_handler 只是样板代码。

You can name your handler anything, or simply place your function code under lambda_handler您可以为您的处理程序命名任何名称,或者简单地将您的 function 代码放在lambda_handler

def lambda_handler(event, context):
    return print('First function')

The name lambda_handler is configurable, meaning you could use the code名称lambda_handler是可配置的,这意味着您可以使用代码

def first_function(event, context):
    return print('First function')

But you'll need to ensure that the function is configured to use first_function as it's handler.但是您需要确保将 function 配置为使用first_function作为其处理程序。

I'd recommend reading through the docs specific to python handlers我建议阅读特定于 python 处理程序的文档

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

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