How to use create_lambda_archive method in localstack

Best Python code snippet using localstack_python

test_lambda.py

Source:test_lambda.py Github

copy

Full Screen

...29 lambda_name = 'test_lambda_%s' % short_uid()30 bucket_name = 'test_bucket_lambda'31 bucket_key = 'test_lambda.zip'32 # upload zip file to S333 zip_file = testutil.create_lambda_archive(load_file(TEST_LAMBDA_PYTHON), get_content=True,34 libs=TEST_LAMBDA_LIBS, runtime=LAMBDA_RUNTIME_PYTHON27)35 s3_client.create_bucket(Bucket=bucket_name)36 s3_client.upload_fileobj(BytesIO(zip_file), bucket_name, bucket_key)37 # create lambda function38 lambda_client.create_function(39 FunctionName=lambda_name, Handler='handler.handler',40 Runtime=lambda_api.LAMBDA_RUNTIME_PYTHON27, Role='r1',41 Code={42 'S3Bucket': bucket_name,43 'S3Key': bucket_key44 }45 )46 # invoke lambda function47 data_before = b'{"foo": "bar"}'48 result = lambda_client.invoke(FunctionName=lambda_name, Payload=data_before)49 data_after = result['Payload'].read()50 assert json.loads(to_str(data_before)) == json.loads(to_str(data_after))51def test_lambda_runtimes():52 lambda_client = aws_stack.connect_to_service('lambda')53 # deploy and invoke lambda - Python 2.754 zip_file = testutil.create_lambda_archive(load_file(TEST_LAMBDA_PYTHON), get_content=True,55 libs=TEST_LAMBDA_LIBS, runtime=LAMBDA_RUNTIME_PYTHON27)56 testutil.create_lambda_function(func_name=TEST_LAMBDA_NAME_PY,57 zip_file=zip_file, runtime=LAMBDA_RUNTIME_PYTHON27)58 result = lambda_client.invoke(FunctionName=TEST_LAMBDA_NAME_PY, Payload=b'{}')59 assert result['StatusCode'] == 20060 result_data = result['Payload'].read()61 assert to_str(result_data).strip() == '{}'62 if use_docker():63 # deploy and invoke lambda - Python 3.664 zip_file = testutil.create_lambda_archive(load_file(TEST_LAMBDA_PYTHON3), get_content=True,65 libs=TEST_LAMBDA_LIBS, runtime=LAMBDA_RUNTIME_PYTHON36)66 testutil.create_lambda_function(func_name=TEST_LAMBDA_NAME_PY3,67 zip_file=zip_file, runtime=LAMBDA_RUNTIME_PYTHON36)68 result = lambda_client.invoke(FunctionName=TEST_LAMBDA_NAME_PY3, Payload=b'{}')69 assert result['StatusCode'] == 20070 result_data = result['Payload'].read()71 assert to_str(result_data).strip() == '{}'72 # deploy and invoke lambda - Java73 if not os.path.exists(TEST_LAMBDA_JAVA):74 mkdir(os.path.dirname(TEST_LAMBDA_JAVA))75 download(TEST_LAMBDA_JAR_URL, TEST_LAMBDA_JAVA)76 zip_file = testutil.create_zip_file(TEST_LAMBDA_JAVA, get_content=True)77 testutil.create_lambda_function(func_name=TEST_LAMBDA_NAME_JAVA, zip_file=zip_file,78 runtime=LAMBDA_RUNTIME_JAVA8, handler='cloud.localstack.sample.LambdaHandler')79 result = lambda_client.invoke(FunctionName=TEST_LAMBDA_NAME_JAVA, Payload=b'{}')80 assert result['StatusCode'] == 20081 result_data = result['Payload'].read()82 assert to_str(result_data).strip() == '{}'83 if use_docker():84 # deploy and invoke lambda - Node.js85 zip_file = testutil.create_zip_file(TEST_LAMBDA_NODEJS, get_content=True)86 testutil.create_lambda_function(func_name=TEST_LAMBDA_NAME_JS,87 zip_file=zip_file, handler='lambda_integration.handler', runtime=LAMBDA_RUNTIME_NODEJS)88 result = lambda_client.invoke(FunctionName=TEST_LAMBDA_NAME_JS, Payload=b'{}')89 assert result['StatusCode'] == 20090 result_data = result['Payload'].read()91 assert to_str(result_data).strip() == '{}'92def test_lambda_environment():93 lambda_client = aws_stack.connect_to_service('lambda')94 # deploy and invoke lambda without Docker95 zip_file = testutil.create_lambda_archive(load_file(TEST_LAMBDA_ENV), get_content=True,96 libs=TEST_LAMBDA_LIBS, runtime=LAMBDA_RUNTIME_PYTHON27)97 testutil.create_lambda_function(func_name=TEST_LAMBDA_NAME_ENV,98 zip_file=zip_file, runtime=LAMBDA_RUNTIME_PYTHON27, envvars={'Hello': 'World'})99 result = lambda_client.invoke(FunctionName=TEST_LAMBDA_NAME_ENV, Payload=b'{}')100 assert result['StatusCode'] == 200101 result_data = result['Payload']...

Full Screen

Full Screen

34122_test_lambda.py

Source:34122_test_lambda.py Github

copy

Full Screen

...29 lambda_name = 'test_lambda_%s' % short_uid()30 bucket_name = 'test_bucket_lambda'31 bucket_key = 'test_lambda.zip'32 # upload zip file to S333 zip_file = testutil.create_lambda_archive(load_file(TEST_LAMBDA_PYTHON), get_content=True,34 libs=TEST_LAMBDA_LIBS, runtime=LAMBDA_RUNTIME_PYTHON27)35 s3_client.create_bucket(Bucket=bucket_name)36 s3_client.upload_fileobj(BytesIO(zip_file), bucket_name, bucket_key)37 # create lambda function38 lambda_client.create_function(39 FunctionName=lambda_name, Handler='handler.handler',40 Runtime=lambda_api.LAMBDA_RUNTIME_PYTHON27, Role='r1',41 Code={42 'S3Bucket': bucket_name,43 'S3Key': bucket_key44 }45 )46 # invoke lambda function47 data_before = b'{"foo": "bar"}'48 result = lambda_client.invoke(FunctionName=lambda_name, Payload=data_before)49 data_after = result['Payload'].read()50 assert json.loads(to_str(data_before)) == json.loads(to_str(data_after))51def test_lambda_runtimes():52 lambda_client = aws_stack.connect_to_service('lambda')53 # deploy and invoke lambda - Python 2.754 zip_file = testutil.create_lambda_archive(load_file(TEST_LAMBDA_PYTHON), get_content=True,55 libs=TEST_LAMBDA_LIBS, runtime=LAMBDA_RUNTIME_PYTHON27)56 testutil.create_lambda_function(func_name=TEST_LAMBDA_NAME_PY,57 zip_file=zip_file, runtime=LAMBDA_RUNTIME_PYTHON27)58 result = lambda_client.invoke(FunctionName=TEST_LAMBDA_NAME_PY, Payload=b'{}')59 assert result['StatusCode'] == 20060 result_data = result['Payload'].read()61 assert to_str(result_data).strip() == '{}'62 if use_docker():63 # deploy and invoke lambda - Python 3.664 zip_file = testutil.create_lambda_archive(load_file(TEST_LAMBDA_PYTHON3), get_content=True,65 libs=TEST_LAMBDA_LIBS, runtime=LAMBDA_RUNTIME_PYTHON36)66 testutil.create_lambda_function(func_name=TEST_LAMBDA_NAME_PY3,67 zip_file=zip_file, runtime=LAMBDA_RUNTIME_PYTHON36)68 result = lambda_client.invoke(FunctionName=TEST_LAMBDA_NAME_PY3, Payload=b'{}')69 assert result['StatusCode'] == 20070 result_data = result['Payload'].read()71 assert to_str(result_data).strip() == '{}'72 # deploy and invoke lambda - Java73 if not os.path.exists(TEST_LAMBDA_JAVA):74 mkdir(os.path.dirname(TEST_LAMBDA_JAVA))75 download(TEST_LAMBDA_JAR_URL, TEST_LAMBDA_JAVA)76 zip_file = testutil.create_zip_file(TEST_LAMBDA_JAVA, get_content=True)77 testutil.create_lambda_function(func_name=TEST_LAMBDA_NAME_JAVA, zip_file=zip_file,78 runtime=LAMBDA_RUNTIME_JAVA8, handler='cloud.localstack.sample.LambdaHandler')79 result = lambda_client.invoke(FunctionName=TEST_LAMBDA_NAME_JAVA, Payload=b'{}')80 assert result['StatusCode'] == 20081 result_data = result['Payload'].read()82 assert to_str(result_data).strip() == '{}'83 if use_docker():84 # deploy and invoke lambda - Node.js85 zip_file = testutil.create_zip_file(TEST_LAMBDA_NODEJS, get_content=True)86 testutil.create_lambda_function(func_name=TEST_LAMBDA_NAME_JS,87 zip_file=zip_file, handler='lambda_integration.handler', runtime=LAMBDA_RUNTIME_NODEJS)88 result = lambda_client.invoke(FunctionName=TEST_LAMBDA_NAME_JS, Payload=b'{}')89 assert result['StatusCode'] == 20090 result_data = result['Payload'].read()91 assert to_str(result_data).strip() == '{}'92def test_lambda_environment():93 lambda_client = aws_stack.connect_to_service('lambda')94 # deploy and invoke lambda without Docker95 zip_file = testutil.create_lambda_archive(load_file(TEST_LAMBDA_ENV), get_content=True,96 libs=TEST_LAMBDA_LIBS, runtime=LAMBDA_RUNTIME_PYTHON27)97 testutil.create_lambda_function(func_name=TEST_LAMBDA_NAME_ENV,98 zip_file=zip_file, runtime=LAMBDA_RUNTIME_PYTHON27, envvars={'Hello': 'World'})99 result = lambda_client.invoke(FunctionName=TEST_LAMBDA_NAME_ENV, Payload=b'{}')100 assert result['StatusCode'] == 200101 result_data = result['Payload']...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run localstack automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful