Lambda Layer issue: Python module not found

0

Overall Usecase: I have a lambda function and I am adding custom Python package layers to the lambda using Terraform. Lambda was created successfully and also layer was attached to lambda. Here is my folder structure I have installed the python google API library in gogoleapis folder using the below command

pip install google-cloud-iam -t /Users/Documents/GitHub/Project/project/step_function/common/googleapis

project-dir
     |-step_function
                   |-common
                           |-googleapis
                           |-zip
                   |-python
                           |-lambda_function.py
                   |-zip
                        |-lambda_function.zip
                   |-main.tf
     |-main.tf

I have added block to create zip folder of the googleapis and creating layers. Googleapis filder contains all google cloud libraries needed for python function.

# Add Google API Layers
data "archive_file" "googleapis_layer_archive" {
  type        = "zip"
  source_dir = "${path.module}/common/googleapis"
  output_path = "${path.module}/common/zip/googleapis.zip"
}

# Python googleapis layer
resource "aws_lambda_layer_version" "googleapis_layer" {
    filename            = "${path.module}/common/zip/googleapis.zip"
    layer_name          = "googleapis"
    source_code_hash    = data.archive_file.snow_gcp_permission_handler_archive.output_base64sha256
    compatible_runtimes = ["python3.8"]
}

Lambda function gcp_get_assignment_role_data.py code which imports a package from layers. I have imported the google packages which are part of googleapis folder.

Lambda function: gcp_get_assignment_role_data.py

import json, os
from datetime import date
import boto3, requests
from google.oauth2 import service_account

def lambda_handler(event, context):
    # TODO implement
    return "hello-world"

When I try to run the lambda, the function returns the below error. I tried to look at the AWS documentation but all steps lead to manual creation and adding lambda layers to the function. But I want to create/add to the lambda using Terraform.

{
  "errorMessage": "Unable to import module 'gcp_get_assignment_role_data': No module named 'google'",
  "errorType": "Runtime.ImportModuleError",
  "stackTrace": []
}
1 Answer
1

When creating layers, there is a particular folder structure you have to follow. https://docs.aws.amazon.com/lambda/latest/dg/packaging-layers.html

You need to update the the ZIP file to look like the following..

googleapis.zip
         |-python
                |-googleapis
                |-googleapis-x.x.x.x.dist-info

Then the libary should work so long as the layer was correctly attached to your function which you said was.

When you installed the GOOGLE API libary its installed in the root of your common\googleapis folder. You need to have a python\googleapi folder.

You could create a new folder in common and install libary as such. (There is 2 python folders for a reason)

pip install google-cloud-iam -t /Users/Documents/GitHub/Project/project/step_function/common/python/python

And then your TF would look like

data "archive_file" "googleapis_layer_archive" {
  type        = "zip"
  source_dir = "${path.module}/common/python"
  output_path = "${path.module}/common/zip/googleapis.zip"
}

# Python googleapis layer
resource "aws_lambda_layer_version" "googleapis_layer" {
    filename            = "${path.module}/common/zip/googleapis.zip"
    layer_name          = "googleapis"
    source_code_hash    = data.archive_file.snow_gcp_permission_handler_archive.output_base64sha256
    compatible_runtimes = ["python3.8"]
}
profile picture
EXPERT
answered 9 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions