Deploying Python with openpyxl on AWS Lambda

2020-09-27 / AWS Lambda / Python / openpyxl / 2 minutes

AWS Lambda is a powerful function-as-a-service (FaaS) tool that allows you to run code in the cloud without having to maintain a server and its computing environment. One limitation is that the runtimes provided do not always contain all necessary packages that you need. In these cases you have to create your deployment package outside of the AWS console and upload it manually. In this post, we see how to do this for a Python function that uses package openpyxl.

The Python Function

The openpyxl package allows you to read and write Excel workbooks in Python. As an example, we will create a function that creates a workbook, writes dummy data into it and uploads the Excel file to S3.

import json
import boto3
from openpyxl import Workbook
from openpyxl.styles import Font

def createWorkbook():
    defaultFont = Font(name='Perpetua')
    wb = Workbook()

    # New Sheet
    wb.create_sheet('My New Sheet')
    ws = wb['My New Sheet']
    ws.cell(row=1, column=1, value='The Header')
    ws.cell(row=1, column=1).style = 'Headline 1'
    ws.cell(row=1, column=1).font  = defaultFont
    return wb

def lambda_handler(event, context):
    s3 = boto3.client('s3')
    wb = createWorkbook()
    wb.save('/tmp/workbook.xlsx')
      
    s3.upload_file('/tmp/workbook.xlsx', 'bucket-for-my-workbooks', 'workbook.xlsx')

    return {
        'statusCode': 200,
        'body': 'Workbook created!'
    }

Deployment (using AWS CLI)

As openpyxl is not part of Python’s standard library it is not included in AWS Lambda. Hence, we need to include it in our deployment package for the function to work on AWS Lambda. Following the official documentation we need to do the following (using Powershell with AWS CLI):

  1. Create a project folder and change into that directory
mkdir my-python-excel-project
cd my-python-excel-project
  1. In that directory, install all dependencies locally. Here, this is openpyxl (and all of its dependencies):
pip install --target ./package openpyxl
cd package
  1. Zip all of these packages into a single zip file:
zip -r9 ../my-python-excel-project.zip .
cd ..
  1. Add your python function (here: lambdaCreateWorkbook.py) to this archive:
zip -g my-python-excel-project.zip lambdaCreateWorkbook.py
  1. Finally, deploy this package on AWS Lambda (you need to excahnge the ARN to your AWS Lambda role):
aws lambda create-function --function-name createWorkbook `
                           --runtime python3.8 `
                           --zip-file fileb://my-python-excel-project.zip `
                           --handler lambdaCreateWorkbook.lambda_handler `
                           --role arn:aws:iam::xxxxxxxxxx:role/mydefaultlambdarole

The function is now available in AWS Lambda under createWorkbook.



comments powered by Disqus