Skip to content

Commit

Permalink
add lambda-container code
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed Oct 23, 2023
1 parent 7997f70 commit ac046ff
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 0 deletions.
9 changes: 9 additions & 0 deletions cdk-projects/lambda-container/app.py
Original file line number Diff line number Diff line change
@@ -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()
23 changes: 23 additions & 0 deletions cdk-projects/lambda-container/aws_proxy.sh
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions cdk-projects/lambda-container/cdk.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"app": "python3 app.py"
}
21 changes: 21 additions & 0 deletions cdk-projects/lambda-container/lambda/Dockerfile
Original file line number Diff line number Diff line change
@@ -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" ]
9 changes: 9 additions & 0 deletions cdk-projects/lambda-container/lambda/lambda-handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import python_wrapper
def main(event, context):
# save event to logs
print(event)

return {
'statusCode': 200,
'body': event
}
Original file line number Diff line number Diff line change
@@ -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()
Original file line number Diff line number Diff line change
@@ -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()
2 changes: 2 additions & 0 deletions cdk-projects/lambda-container/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
aws-cdk-lib>=2.0.0
constructs>=10.0.0
Empty file.
35 changes: 35 additions & 0 deletions cdk-projects/lambda-container/s3trigger/s3trigger_stack.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit ac046ff

Please sign in to comment.