added aws/lambda sample

This commit is contained in:
Ahmad Saleem Z
2023-06-10 22:49:54 +02:00
parent c07cb2141e
commit 829870a416
4 changed files with 105 additions and 0 deletions
+40
View File
@@ -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"]
+37
View File
@@ -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
+27
View File
@@ -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
}
+1
View File
@@ -0,0 +1 @@
boto3