diff --git a/cdk-projects/lambda-container/app.py b/cdk-projects/lambda-container/app.py new file mode 100644 index 00000000..cfa6de6a --- /dev/null +++ b/cdk-projects/lambda-container/app.py @@ -0,0 +1,9 @@ +#!/usr/bin/env python3 +from aws_cdk import App + +from s3trigger.s3trigger_stack import S3TriggerStack + +app = App() +S3TriggerStack(app, "s3trigger") + +app.synth() \ No newline at end of file diff --git a/cdk-projects/lambda-container/aws_proxy.sh b/cdk-projects/lambda-container/aws_proxy.sh new file mode 100755 index 00000000..df22d9d8 --- /dev/null +++ b/cdk-projects/lambda-container/aws_proxy.sh @@ -0,0 +1,23 @@ +#!/bin/bash + +# Setting up proxy and git +export HTTPS_PROXY=http://proxy01.pnl.gov:3128 +export https_proxy=http://proxy01.pnl.gov:3128 +export AWS_PROFILE=AdministratorAccess-305402452870 +export AWS_DEFAULT_REGION=us-west-2 + +# Configure Git credential helper +git config --global credential.helper '!aws --profile AdministratorAccess-305402452870 codecommit credential-helper $@' + +# Start docker engine +docker --version + +#virtual environment setup +python -m venv venv +source venv/bin/activate + +#cdk library setup +pip install aws-cdk-lib==2.90.0 + +# AWS configuration on CLI +aws configure sso \ No newline at end of file diff --git a/cdk-projects/lambda-container/cdk.json b/cdk-projects/lambda-container/cdk.json new file mode 100644 index 00000000..b4baa102 --- /dev/null +++ b/cdk-projects/lambda-container/cdk.json @@ -0,0 +1,3 @@ +{ + "app": "python3 app.py" +} diff --git a/cdk-projects/lambda-container/lambda/Dockerfile b/cdk-projects/lambda-container/lambda/Dockerfile new file mode 100644 index 00000000..1078d8cf --- /dev/null +++ b/cdk-projects/lambda-container/lambda/Dockerfile @@ -0,0 +1,21 @@ +FROM public.ecr.aws/lambda/python:3.11 + +# Copy requirements.txt +#COPY requirements.txt ${LAMBDA_TASK_ROOT} + +# Copy function code +COPY lambda_handler.py ${LAMBDA_TASK_ROOT} +ADD python_wrapper ${LAMBDA_TASK_ROOT}/python_wrapper + +#Setup Proxy +#RUN export HTTPS_PROXY=http://proxy01.pnl.gov:3128 +#RUN export https_proxy=http://proxy01.pnl.gov:3128 +# Install the specified packages +#RUN export HTTP_PROXY=http://proxy01.pnl.gov:3128 +#RUN export http_proxy=http://proxy01.pnl.gov:3128 +RUN pip install --upgrade pip +#RUN pip install -r ${LAMBDA_TASK_ROOT}/requirements.txt + + +# Set the CMD to your handler (could also be done as a parameter override outside of the Dockerfile) +#CMD [ "lambda_handler.main" ] diff --git a/cdk-projects/lambda-container/lambda/lambda-handler.py b/cdk-projects/lambda-container/lambda/lambda-handler.py new file mode 100644 index 00000000..1ce120ae --- /dev/null +++ b/cdk-projects/lambda-container/lambda/lambda-handler.py @@ -0,0 +1,9 @@ +import python_wrapper +def main(event, context): + # save event to logs + print(event) + + return { + 'statusCode': 200, + 'body': event + } diff --git a/cdk-projects/lambda-container/lambda/python_wrapper/__init__.py b/cdk-projects/lambda-container/lambda/python_wrapper/__init__.py new file mode 100755 index 00000000..4c32ab50 --- /dev/null +++ b/cdk-projects/lambda-container/lambda/python_wrapper/__init__.py @@ -0,0 +1,8 @@ +import exago +exago.initialize("opflow") +opf = exago.OPFLOW() +opf.read_mat_power_data('datafiles/case9/case9mod.m') +opf.solve() +opf.print_solution() +del opf +exago.finalize() diff --git a/cdk-projects/lambda-container/lambda/python_wrapper/lambda_exago.py b/cdk-projects/lambda-container/lambda/python_wrapper/lambda_exago.py new file mode 100755 index 00000000..4c32ab50 --- /dev/null +++ b/cdk-projects/lambda-container/lambda/python_wrapper/lambda_exago.py @@ -0,0 +1,8 @@ +import exago +exago.initialize("opflow") +opf = exago.OPFLOW() +opf.read_mat_power_data('datafiles/case9/case9mod.m') +opf.solve() +opf.print_solution() +del opf +exago.finalize() diff --git a/cdk-projects/lambda-container/requirements.txt b/cdk-projects/lambda-container/requirements.txt new file mode 100644 index 00000000..9eb8dd1a --- /dev/null +++ b/cdk-projects/lambda-container/requirements.txt @@ -0,0 +1,2 @@ +aws-cdk-lib>=2.0.0 +constructs>=10.0.0 diff --git a/cdk-projects/lambda-container/s3trigger/__init__.py b/cdk-projects/lambda-container/s3trigger/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/cdk-projects/lambda-container/s3trigger/s3trigger_stack.py b/cdk-projects/lambda-container/s3trigger/s3trigger_stack.py new file mode 100644 index 00000000..d395f31c --- /dev/null +++ b/cdk-projects/lambda-container/s3trigger/s3trigger_stack.py @@ -0,0 +1,35 @@ +from aws_cdk import ( + aws_lambda as _lambda, + aws_s3 as _s3, + aws_s3_notifications, + aws_ecr as ecr, + Stack +) +from constructs import Construct + +class S3TriggerStack(Stack): + + def __init__(self, scope: Construct, id: str, **kwargs) -> None: + super().__init__(scope, id, **kwargs) + + # create lambda function + #function = _lambda.Function(self, "lambda_function", + #runtime=_lambda.Runtime.PYTHON_3_7, + #handler="lambda-handler.main", + #code=_lambda.Code.from_asset("./lambda")) + #function=_lambda.DockerImageFunction(self, "ExagoLambdaFunction", + #code=_lambda.DockerImageCode.from_image_asset("lambda")) + ecr_repository = ecr.Repository(self, "ExagoRepo", + repository_name="exago_repo") # Specify your repository name + + # Define the Lambda function using a Docker container from ECR + function = _lambda.DockerImageFunction(self, "ExagoLambdaFunction", + code=_lambda.DockerImageCode.from_ecr(repository=ecr_repository)) + # create s3 bucket + s3 = _s3.Bucket(self, "s3bucket") + + # create s3 notification for lambda function + notification = aws_s3_notifications.LambdaDestination(function) + + # assign notification for the s3 event type (ex: OBJECT_CREATED) + s3.add_event_notification(_s3.EventType.OBJECT_CREATED, notification)