From 3a1557bedc53e3f503cf427c25845cf49df67537 Mon Sep 17 00:00:00 2001 From: Ahmad Saleem Z Date: Sat, 10 Jun 2023 22:49:54 +0200 Subject: [PATCH] added aws/lambda sample --- aws/lambda/Dockerfile | 40 ++++++++++++++++++++++++++ aws/lambda/README.md | 37 ++++++++++++++++++++++++ aws/lambda/example_handler/__init__.py | 27 +++++++++++++++++ aws/lambda/requirements.txt | 1 + 4 files changed, 105 insertions(+) create mode 100644 aws/lambda/Dockerfile create mode 100644 aws/lambda/README.md create mode 100644 aws/lambda/example_handler/__init__.py create mode 100644 aws/lambda/requirements.txt diff --git a/aws/lambda/Dockerfile b/aws/lambda/Dockerfile new file mode 100644 index 0000000000..5340c675d3 --- /dev/null +++ b/aws/lambda/Dockerfile @@ -0,0 +1,40 @@ +# Dockerfile for Running AWS Lambda Function with Python and IfcOpenShell + +# Set the location of your Lambda function code +ARG FUNCTION_DIR="/var/task" + +# Base image: Python 3.9 from AWS's public container registry +FROM public.ecr.aws/docker/library/python:3.9 AS build + +# Install necessary packages +RUN apt-get -y update && apt-get -y install unzip curl + +# Install AWS Lambda runtime interface client +RUN pip install --target ${FUNCTION_DIR} awslambdaric + +# Set the IfcOpenShell build version (check available builds at: https://blenderbim.org/docs-python/ifcopenshell-python/installation.html) +ARG IFC_OPENSHELL_BUILD="v0.7.0-476ab50" + +# Download and extract IfcOpenShell +RUN curl https://s3.amazonaws.com/ifcopenshell-builds/ifcopenshell-python-${IFC_OPENSHELL_BUILD}-linux64.zip -O && \ + unzip ifcopenshell-python-${IFC_OPENSHELL_BUILD}-linux64.zip && \ + mv ifcopenshell ${FUNCTION_DIR} && \ + rm ifcopenshell-python-${IFC_OPENSHELL_BUILD}-linux64.zip + +# Copy the requirements file and install dependencies +COPY requirements.txt . +RUN pip install -r requirements.txt + +# Copy the Lambda function code +COPY ./example_handler ${FUNCTION_DIR}/example_handler + +# Set the working directory +WORKDIR ${FUNCTION_DIR} + +# Set the entrypoint for the Lambda function +ENTRYPOINT [ "/usr/local/bin/python", "-m", "awslambdaric" ] + +# Set the Python import path to the Lambda function handler +# This path is relative to the root of the Lambda function code (FUNCTION_DIR) +# Lambda invocation will look for this path +CMD ["example_handler.extract_wall_psets"] diff --git a/aws/lambda/README.md b/aws/lambda/README.md new file mode 100644 index 0000000000..84a413b5e3 --- /dev/null +++ b/aws/lambda/README.md @@ -0,0 +1,37 @@ +Dockerized AWS Lambda Function with Python and IfcOpenShell +=========================================================== + +This repository provides a Dockerfile and sample code to help you run an AWS Lambda function written in Python and utilizing IfcOpenShell library. + +Prerequisites +------------- + +Make sure you have Docker installed on your machine. You can download Docker from the official website: [Docker Instalation](https://docs.docker.com/engine/install/) + +Getting Started +--------------- + +1. Clone this repository to your local machine: + +2. Customize the Lambda function code: + - Replace the sample Lambda function code in the `example_handler` directory with your own code. + - Update the import path in the Dockerfile's `CMD` instruction to match your Lambda function's handler function. + +3. Install the required Python packages: + - Edit the `requirements.txt` file and add any additional dependencies required by your Lambda function. + +4. Build the Docker image: + + ```shell + $ docker build -t lambda-ifcopenshell . + ``` + +5. Run the Docker container: + + ```shell + $ docker run lambda-ifcopenshell + ``` + +6. Deploy to lambda + + This is beyond the scope of this example. Please refer to AWS documentation. Some tools that could be useful are - AWS CloudFormaton, AWS CDK, pulumi or terraform diff --git a/aws/lambda/example_handler/__init__.py b/aws/lambda/example_handler/__init__.py new file mode 100644 index 0000000000..5796334cf3 --- /dev/null +++ b/aws/lambda/example_handler/__init__.py @@ -0,0 +1,27 @@ +import ifcopenshell +import ifcopenshell.util.element +import boto3 + +s3 = boto3.resource('s3') + +def handler(event, context): + print("Hello from lambda") + print("Event: {}".format(event)) + print("Context: {}".format(context)) + + filename = event['body']['filename'] + + s3.Bucket('my_ifc_files').download_file(filename, f'/tmp/{filename}') + ifc_file = ifcopenshell.open(f'/tmp/{filename}') + walls = ifc_file.by_type('IfcWall') + + psets = [] + for wall in walls: + wall_data = ifcopenshell.util.element.get_psets(wall) + wall_data['id'] = wall.GlobalId + psets.append(wall_data) + + return { + 'statusCode': 200, + 'body': psets + } \ No newline at end of file diff --git a/aws/lambda/requirements.txt b/aws/lambda/requirements.txt new file mode 100644 index 0000000000..1db657b6b3 --- /dev/null +++ b/aws/lambda/requirements.txt @@ -0,0 +1 @@ +boto3 \ No newline at end of file