TestProject Forum

How To Execute Python REST API test suite on AWS Lambda

I am taking AWS training online and creating my own projects. Basically, I have written API tests using Python REST API test suite, below tests runs fine on my local environment, here are the tests

import requests
import json

def test_post_headers_body_json():
 url = 'https://httpbin.org/post'

  # Additional headers.
 headers = {'Content-Type': 'application/json' } 

  # Body
 payload = {'key1': 1, 'key2': 'value2'}

  # convert dict to json by json.dumps() for body data. 
   resp = requests.post(url, data = json.dumps(payload,indent=4))       

  # Validate response headers and body contents, e.g. status code.
    assert resp.status_code == 200
    resp_body = resp.json()
 assert resp_body['url'] == url

    # print response full body as text

Now to run tests at my local all I need to do is just open a command prompt and type pytest in the script folder, and you will get a test result as follows.

   ================ test session starts =======================

But how can i run the same python test suite written above in AWS LAMBDA environment because AWS lambda has below handler code which is different from what i have above ,how can I include my code here in the AWS lambda handler code?

    import json

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