title: Using AWS Lambda and boto3 to append new lines to text file objects in S3
body: <p>I'm trying to use a python lambda function to append a text file with a new line on a object stored in S3. Since objects stored in S3 are immutable, you must first download the file into '/tmp/', then modify it, then upload the new version back to S3. My code appends the data, however it will not append it with a new line.</p>
  
  <pre><code>BUCKET_NAME = 'mybucket' 
  KEY = 'test.txt'
  s3 = boto3.resource('s3')
  
  def lambda_handler(event, context):
      try:
          s3.Object(BUCKET_NAME, KEY).download_file('/tmp/test.txt')
      except botocore.exceptions.ClientError as e:
          if e.response['Error']['Code'] == "404":
              print("The object does not exist.")
          else:
              raise
      with open('/tmp/test.txt', 'a') as fd:
          fd.write("this is a new string\n")
  
  
      s3.meta.client.upload_file('/tmp/test.txt', BUCKET_NAME, KEY)
  </code></pre>
  
  <p>The file is always appended with the new string but never with a new line. Any ideas?</p>
  
  <p>UPDATE: This problem does not occur on linux machines or on a Mac. Lambda functions run on linux containers, which means the file in /tmp/ is saved as a Unix-formatted text file. Some Windows applications will not show line breaks on Unix-formatted text files, which was the case here. I'm dumb. </p>