简体   繁体   中英

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:

  1. Create a lambda function
  2. Install boto3
  3. Use invoke function

Lets say I have a simple 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:

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:

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. 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. 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.

Tha lambda_handler that shows up when you create a function in the console is simply boiler plate code.

You can name your handler anything, or simply place your function code under lambda_handler

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

The name lambda_handler is configurable, meaning you could use the code

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.

I'd recommend reading through the docs specific to python handlers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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