title: AWS Lambda function to upload a file to S3 bucket
body: <p>My task is to upload a file into an AWS S3 bucket using a Lambda function. For preliminary testing, I am creating a file in the function and uploading it to the desired S3 bucket. Code:</p>
  <pre><code>import json
  import os
  import boto3
  
  session = boto3.session.Session()
  
  def lambda_handler(event, context):
      # TODO implement
      s3 = session.resource('s3')
      bucket_name = os.environ['s3_dest_bucket']
      key = 's3_lambda_function_test/' + 's3_lambda_test_DL.txt'
      
      dummy_data = b'Here we have some data'
      
      s3Obj = s3.Object(bucket_name, key=key)
      s3Obj.put(Body=dummy_data)
      print(&quot;Uploading to s3 bucket.&quot;)
      s3Obj.upload_file('s3_lambda_test_DL.txt')
      
      return {
          'statusCode': 200,
          'body': json.dumps('Successful upload')
      }
  </code></pre>
  <p>The way the code is currently written, it does exactly what I want: creates file <code>s3_lambda_test_DL.txt</code>, adds text to it, and uploads it to the desired S3 bucket. When I deploy and test the function on Lambda though, I get the following Response in the <code>Execution log</code>:</p>
  <pre><code>{
    &quot;errorMessage&quot;: &quot;[Errno 2] No such file or directory: 's3_lambda_test_DL.txt'&quot;,
    &quot;errorType&quot;: &quot;FileNotFoundError&quot;,
    &quot;requestId&quot;: &quot;274fa38d-73d7-43b7-add0-760fd78ee1f2&quot;,
    &quot;stackTrace&quot;: [
      &quot;  File \&quot;/var/task/lambda_function.py\&quot;, line 18, in lambda_handler\n    s3Obj.upload_file('s3_lambda_test_DL.txt')\n&quot;,
      &quot;  File \&quot;/var/runtime/boto3/s3/inject.py\&quot;, line 279, in object_upload_file\n    return self.meta.client.upload_file(\n&quot;,
      &quot;  File \&quot;/var/runtime/boto3/s3/inject.py\&quot;, line 130, in upload_file\n    return transfer.upload_file(\n&quot;,
      &quot;  File \&quot;/var/runtime/boto3/s3/transfer.py\&quot;, line 287, in upload_file\n    future.result()\n&quot;,
      &quot;  File \&quot;/var/runtime/s3transfer/futures.py\&quot;, line 103, in result\n    return self._coordinator.result()\n&quot;,
      &quot;  File \&quot;/var/runtime/s3transfer/futures.py\&quot;, line 266, in result\n    raise self._exception\n&quot;,
      &quot;  File \&quot;/var/runtime/s3transfer/tasks.py\&quot;, line 269, in _main\n    self._submit(transfer_future=transfer_future, **kwargs)\n&quot;,
      &quot;  File \&quot;/var/runtime/s3transfer/upload.py\&quot;, line 585, in _submit\n    upload_input_manager.provide_transfer_size(transfer_future)\n&quot;,
      &quot;  File \&quot;/var/runtime/s3transfer/upload.py\&quot;, line 244, in provide_transfer_size\n    self._osutil.get_file_size(transfer_future.meta.call_args.fileobj)\n&quot;,
      &quot;  File \&quot;/var/runtime/s3transfer/utils.py\&quot;, line 247, in get_file_size\n    return os.path.getsize(filename)\n&quot;,
      &quot;  File \&quot;/var/lang/lib/python3.9/genericpath.py\&quot;, line 50, in getsize\n    return os.stat(filename).st_size\n&quot;
    ]
  }
  </code></pre>
  <p>Any advice on this?</p>