title: Creating an on-the-fly zip file from string content for AWS Lambda in Python
body: <p>I have a Python script that creates a Lambda script in AWS along with all the policies and triggers. I use python boto3 library for that. I create the zip file for the lambda as on-the-fly rather than uploading a static zip file from the hard drive. I use this simple code from below to create my zip file. It creates the zip file without any problems and my python code uploads this zip file as a lambda script and I can view my lambda script in the AWS without any problems. But when I run my lambda script it gives me the module not found error even though I can clearly see that both the module name and the file name does exist and is view-able.</p>
  
  <p><strong>Unable to import module 'xxxx': No module named xxxx</strong></p>
  
  <p>In the file system I double click that zip file that was created by this code and see that the content is created and everything looks normal.</p>
  
  <p>If I bypass zipping on the fly and create the zip statically using WinZip and let the rest of the Python &amp; boto3 script upload this file then it works just fine.</p>
  
  <pre><code>def CreateLambdaZip(self, fileName, fileContent):
      with zipfile.ZipFile('Lambda/' + fileName + '.zip', 'w') as myzipc:
          myzipc.writestr( fileName + '.py', fileContent)
          myzipc.close()
  </code></pre>
  
  <p>It kinda looks like for the zip file I'm skipping some special headers that is needed by Aws Lambda. Is there such thing? Because in the file system the zip file that is created by Python code and the other one that is created by WinZip are exactly the same. So I know there's nothing wrong with the lambda script.</p>
  
  <p><strong>Update</strong>: I'm uploading the zip file using the below code that reads the zip file which was created using the above snippet.</p>
  
  <pre><code>with open('Lambda/'+ fileName +'.zip', 'rb') as zipFile:
      func = boto3.client("Lambda").create_function(
                  FunctionName=lambdaFunction,
                  Runtime='python2.7',
                  Role=role['Role']['Arn'],
                  Handler= fileName + "." + functionName,
                  Description=description,
                  Timeout=10,
                  MemorySize=256,
                  Publish=True,
                  Code={'ZipFile': zipFile.read()},
              )
  </code></pre>
  
  <p>When I use zipFile.read() I get 2 different headers for the same content when I zip it using WinZip and when I zip it using Python's module.
  Zip file that's created programmatically using Python</p>
  
  <pre><code>b'PK\x03\x04\x14\x00\x00\x00\x00\x00\xe4~\x01IO\x96J=Z\x07\x00\x00Z\x07\x00\x00\x19\x00\x00\x00schedule-ec2-snapshots.pyimport json\nimport boto3\nimport time\nfrom datetime import date, timedelta\n\nprint(\'Loading scheduled EC2 backup actions\')\n\ndef create_snapshots(event, context):\n    """\n    Lambda function that executes daily snapshots for the instances that
  </code></pre>
  
  <p>and zipfile created by WinZip</p>
  
  <pre><code>b'PK\x03\x04\x14\x00\x02\x00\x08\x004X\xfcH\x88\x1f\xce\xb5&amp;\x03\x00\x00b\x07\x00\x00\x19\x00\x00\x00schedule-ec2-snapshots.py\x8dU]k\xdb@\x10|7\xf4?,\nA\x12qL\xda\x06B\r~I\x93Bh\x9b\x87&amp;\xf4E\x15\xe1\xac[\xdb\xd7HwBw2\t\xc1\xff\xbd{+\xeb\xcb.\xb4\n\xc4\xba\xdb\xd1\xec\xce\xdc\xae\xa4\x8a\xd2T\x0e~[\xa3\'\xaa\xb9_\x1ag&gt;\xb6\x0b\xa7\n\x9c\xac*S\x80\x14\x0e\xfd\n\xf6\x11\xbf\x9er\\b\xee\xc4dRVJ\xbb(\xfcf\x84Tz\r6\xdb\xa0\xacs\x94p\xfb\xf9\x03,E\xf6\\\x97 
  </code></pre>