skip to Main Content

Overall Usecase: I have 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

Edit-1 Updates
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 import package from layers. I have simply import 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, function returing below error. I tried to look at the AWS documenation but all steps leads to manual creation and adding lambda layers to the function. But I want to create/add to the lambda using Terraform.

Edit-1 — Reproduce actual error

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

2

Answers


  1. Chosen as BEST ANSWER

    I think the issue was with my directory structure when creating and uploading files to layers. Layers libraries won't work if the directory name is anything other than Python. I have updated my directory structure and finally API worked as expected.

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


  2. The error message you provided indicates a syntax error in the Python code module named gcp_get_assignment_role_data. The error specifically points to an unexpected indent on line 4 of the gcp_get_assignment_role_data.py file. Python is sensitive to indentation, so this error occurs when the indentation level is not as expected.

    To resolve this issue, you need to ensure that the indentation in the gcp_get_assignment_role_data.py file is consistent and correct.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search