title: How to check AWS lambda handler in unit test python
body: <p>I need to write a unit test for the AWS lambda handler. I want to give some JSON files to the handler method and get some output. But I come across trouble with mocking the S3 &quot;event&quot;.
  My lambda handler looks like this:</p>
  <pre><code>def handler(event, context):
      print(f'Event: {event}')
  
      s3 = boto3.resource('s3')
      bucket = s3.Bucket(event[&quot;bucket&quot;])
  
      word_correction = correction.WordCorrection()
  
      for obj in bucket.objects.all():
          key = obj.key
          body = obj.get()['Body'].read()
          data = get_data_from_file(body)
  
          if key.endswith('.json'):
              try:
                  word_correction.create_duplicated_words_file(data)
              except Exception as e:
                  print(e)
                  return {
                      &quot;success&quot;: False,
                      &quot;response&quot;: f&quot;Failed to read file - {e}&quot;
                  }
              try:
                  corrected_word_list = word_correction.spell_words(json.loads(body))
              except Exception as e:
                  print(e)
                  return {
                      &quot;success&quot;: False,
                      &quot;response&quot;: f&quot;Failed to correct words - {e}&quot;
                  }
          else:
              return {
                  &quot;success&quot;: False,
                  &quot;response&quot;: &quot;Invalid file type. File must have .json extension.&quot;
              }
          return {
              &quot;success&quot;: True,
              &quot;response&quot;: corrected_word_list
          }
  
  </code></pre>
  <p>Here I put some JSON data to my module word_correction.
  From this point my unit test</p>
  <pre><code>S3_BUCKET_NAME = 'dev-ayazv-lambda-spell-correction'
  DEFAULT_REGION = 'us-east-1'
  
  S3_TEST_FILE_KEY = 'pdfs/manual_test/page-data-page-9.json'
  S3_TEST_FILE_CONTENT = {&quot;Blocks&quot;: [{&quot;BlockType&quot;: &quot;WORD&quot;, &quot;Confidence&quot;: 93.18, &quot;Text&quot;: &quot;Test&quot;}]}
  
  
  @mock_s3
  class TestLambdaFunction(unittest.TestCase):
  
      def setUp(self):
          self.s3 = boto3.resource('s3', region_name=DEFAULT_REGION)
          self.s3_bucket = self.s3.create_bucket(Bucket=S3_BUCKET_NAME)
          self.s3_bucket.put_object(Key=S3_TEST_FILE_KEY,
                                    Body=json.dumps(S3_TEST_FILE_CONTENT))
  
      def test_get_data_from_file(self):
          from functions.spell_correction.src.index import get_data_from_file
  
          json_encode_data = json.dumps(S3_TEST_FILE_CONTENT, indent=2).encode('utf-8')
          file_content = get_data_from_file(json_encode_data)
  
          self.assertEqual(file_content, S3_TEST_FILE_CONTENT)
  
      def test_handler(self):
          from functions.spell_correction.src.index import handler
  
          event = {
              'bucket': {
                  'name': S3_BUCKET_NAME
              },
              'object': {
                  'key': S3_TEST_FILE_KEY
              }
          }
  
          result = handler(event, {})
          self.assertEqual(result, {&quot;success&quot;: True, &quot;response&quot;: []})
  </code></pre>
  <p>I was trying to mock the S3_Bucket S3_File_Key and S3_File Content.
  But I face with the problem that my tests drops in this moment</p>
  <pre><code>bucket = {'name': 'dev-ayazv-lambda-spell-correction'}
  
      def validate_bucket_name(params, **kwargs):
          if 'Bucket' not in params:
              return
          bucket = params['Bucket']
  &gt;       if not VALID_BUCKET.search(bucket) and not VALID_S3_ARN.search(bucket):
  E       TypeError: expected string or bytes-like object
  </code></pre>