title: OSError: Errno 30 Read-only file system: Any way to create a CSV and attach it to email from Python in AWS Lambda?
body: <p>So I am trying to create a Lambda Function to Query from MySQL Database and write the query output to a CSV file, attach the created file as an attachment and send an email using Python.</p>
  
  <p>I have a working code to achieve this, but unable to execute the same in <em>AWS Lambda</em>.</p>
  
  <p>Here's the code I'm working on right now:</p>
  
  <pre><code>engine = create_engine("mysql+pymysql://username:password@MYSQL DB Creds")
  con_mysql = engine.connect()
  
  #dump_attachment_query_link
  attach = pd.read_sql("SELECT * FROM some_table)",con_mysql)
  
  
  #NAMING_FILES
  #start,yest are dates
  def filenames(start, yest):
  
  if start == yest:
      return "Dump_{}.csv".format(yest)
    else:
      return "Dump_{}_to_{}.csv".format(start, yest)
  
  attach.reset_index(drop = True, inplace = True)
  att = attach.to_csv(filenames(start, yest))
  
  
  files = filenames(start, yest)
  
  def send_mail(fromaddr, subject, message):
      access_token, expires_in = refresh_authorization(GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET, GOOGLE_REFRESH_TOKEN)
      auth_string = generate_oauth2_string(fromaddr, access_token, as_base64=True)
  
      msg = MIMEMultipart('related')
      msg['Subject'] = subject + ": %s" %yest
      msg['From'] = fromaddr
      msg['To'] = "receivers'mail"
      msg.preamble = 'This is a multi-part message in MIME format.'
      msg_alternative = MIMEMultipart('alternative')
      msg.attach(msg_alternative)
      part_text = MIMEText(lxml.html.fromstring(message).text_content().encode('utf-8'), 'plain', _charset='utf-8')
      part_html = MIMEText(message.encode('utf-8'), 'html', _charset='utf-8')
      msg_alternative.attach(part_text)
      msg_alternative.attach(part_html)
  
      part = MIMEBase('application', "octet-stream")
      part.set_payload(open(files, "rb").read())
      encoders.encode_base64(part)
      part.add_header('Content-Disposition', 'attachment; filename = {}'.format(files))
      msg.attach(part)
  
      server = smtplib.SMTP('smtp.gmail.com:587')
      server.ehlo(GOOGLE_CLIENT_ID)
      server.starttls()
      server.docmd('AUTH', 'XOAUTH2 ' + auth_string)
      server.sendmail(fromaddr,msg['To'].split(",") + msg['Cc'].split(","), msg.as_string())
      server.quit()
  </code></pre>
  
  <p>When I use the code I get the following error - [ERROR] OSError: [Errno 30] Read-only file system: 'Dump_{}<em>to</em>{}.csv'</p>
  
  <p>I'm quite new to Lambda and Python. 
  Any help regarding this is appreciated</p>