How can I check lambda in python local use?

Is there a way I can check aws lambda in local? I know there is a package whose name is "localstack", but it seems that there are not many people who have tried it.

+13
source share
4 answers

You can run your lambda functions the same way you run any python script, for example.

if __name__ == "__main__":
    event = []
    context = []
    lambda_handler(event, context)

If you use virtual environments, this will help you install all the necessary dependencies for your lambda function along with the correct version of python.

- , , , ?

+15

, Moto, , AWS, .

, Python, :

 import boto3

 class MyModel(object):
    def __init__(self, name, value):
        self.name = name
        self.value = value

    def save(self):
        s3 = boto3.client('s3', region_name='us-east-1')
        s3.put_object(Bucket='mybucket', Key=self.name, Body=self.value)

, .

, Moto:

import boto3
from moto import mock_s3
from mymodule import MyModel

@mock_s3
def test_my_model_save():
conn = boto3.resource('s3', region_name='us-east-1')
# We need to create the bucket since this is all in Moto 'virtual' AWS account
conn.create_bucket(Bucket='mybucket')

model_instance = MyModel('steve', 'is awesome')
model_instance.save()

body = conn.Object('mybucket', 'steve').get()['Body'].read().decode("utf-8")

assert body == b'is awesome'    

, s3 . .

+7
+5

[ ]

AWS , . Live . ... ... .

-, AWS SAM CLI .

, S3, Dynamodb .., CLI Stackery CLI AWS SAM, .

CLI Stackery , Python .

+2

Source: https://habr.com/ru/post/1016178/


All Articles