Skip to content

Commit

Permalink
update submodules for Dockerfile generation
Browse files Browse the repository at this point in the history
  • Loading branch information
jaya.mohan@pnnl.gov committed Nov 20, 2023
1 parent 8f178db commit fd0095e
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 34 deletions.
1 change: 1 addition & 0 deletions cdk-projects/s3io/aws_proxy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ docker --version
python -m venv venv
source venv/bin/activate


#cdk library setup
pip install aws-cdk-lib==2.90.0

Expand Down
6 changes: 3 additions & 3 deletions cdk-projects/s3lambda/app.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/env python3
from aws_cdk import App
import aws_cdk as cdk

from s3lambdatrigger.s3lambdatrigger_stack import S3TriggerStack

app = App()
S3TriggerStack(app, "S3ioTriggerStack")
app = cdk.App()
S3TriggerStack(app, "S3TriggerStack")

app.synth()
5 changes: 3 additions & 2 deletions cdk-projects/s3lambda/lambda/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ ADD python_wrapper ${LAMBDA_TASK_ROOT}/python_wrapper
#RUN export http_proxy=http://proxy01.pnl.gov:3128
RUN pip install --upgrade pip
#RUN pip install -r ${LAMBDA_TASK_ROOT}/requirements.txt
ENTRYPOINT ["/var/lang/bin/python", "-m", "awslambdaric"]


# Set the CMD to your handler (could also be done as a parameter override outside of the Dockerfile)
#CMD [ "lambda_handler.main" ]
# Set the CMD to your handler- filename. lambda_handler-function name (could also be done as a parameter override outside of the Dockerfile)
CMD [ "lambda_handler.lambda_handler" ]
41 changes: 21 additions & 20 deletions cdk-projects/s3lambda/lambda/lambda_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,29 @@
def lambda_handler(event, context):
# Retrieve input file from S3
print(event)
# input_bucket = event['Records'][0]['s3in3']['bucket']
# input_file_key = event['Records'][0]['s3in3']['object']['key']

print('print event')
input_bucket = event['Records'][0]['s3']['bucket']['name']
input_file_key = event['Records'][0]['s3']['object']['key']
# Download input file -> file path should target s3 bucket file.
# local_input_path = f"/tmp/{input_file_key}"
# s3_client.download_file(input_bucket, input_file_key,local_input_path)
#
local_input_path = f"/tmp/{input_file_key}"
s3_client.download_file(input_bucket, input_file_key, local_input_path)

# Process input file using ExaGO (assuming ExaGO processing logic here)
# exago_output = subprocess.check_output(['cat', local_input_path]).decode('utf-8')
# print(exago_output)
exago_output = subprocess.check_output(
['cat', local_input_path]).decode('utf-8')
print(exago_output)
# Upload output to S3 -> Path to
# output_bucket = 's3out3'
output_bucket = os.environ['OUTPUT_BUCKET_NAME']
# Output file path in the output bucket
# output_file_key = 'processed/' + os.path.basename(local_input_path)
# s3_client.put_object(
# Body=exago_output, Bucket=output_bucket, Key=output_file_key)
output_file_key = 'processed/' + os.path.basename(local_input_path)
s3_client.put_object(
Body=exago_output, Bucket=output_bucket, Key=output_file_key)
#
# return {
# 'statusCode': 200,
# 'body': 'Processing complete.'
# }
# return {
# 'statusCode': 200,
# 'body': event
# }
return {
'statusCode': 200,
'body': 'Processing complete.'
}
return {
'statusCode': 200,
'body': event
}
44 changes: 35 additions & 9 deletions cdk-projects/s3lambda/s3lambdatrigger/s3lambdatrigger_stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
aws_lambda as _lambda,
aws_s3 as _s3,
aws_s3_notifications,
aws_ecr as ecr,
aws_iam as iam,
Stack
)
from constructs import Construct
Expand All @@ -16,25 +16,51 @@ def __init__(self, scope: Construct, id: str, **kwargs) -> None:
# ecr_repository = ecr.Repository.from_repository_name(self, "ExagoRepo",
# repository_name="test_repo") # Specify your repository name

# Define the Lambda function using a Docker container from ECR
function = _lambda.DockerImageFunction(self, "ExagoLambdaFunction",
code=_lambda.DockerImageCode.from_image_asset("lambda"))
# create s3 bucket
s3 = _s3.Bucket(self, "s3bucket")
# Create an S3 bucket for input
input_bucket = _s3.Bucket(self,
"InputBucket",
bucket_name="s3in3"
bucket_name="s3in6"
)

# Create an S3 bucket for output
output_bucket = _s3.Bucket(self,
"OutputBucket",
bucket_name="s3out3"
bucket_name="s3out6"
)

# Define the Lambda function using a Docker container from ECR
function = _lambda.DockerImageFunction(self, "ExagoLambdaFunction",
code=_lambda.DockerImageCode.from_image_asset(
"lambda"),
environment={
"OUTPUT_BUCKET_NAME": output_bucket.bucket_name
})

function.add_to_role_policy(iam.PolicyStatement(
actions=[
"s3:GetObject",
"s3:ListBucket"
],
resources=[
f"{input_bucket.bucket_arn}",
f"{input_bucket.bucket_arn}/*"
]
))
function.add_to_role_policy(iam.PolicyStatement(
actions=[
"s3:GetObject",
"s3:ListBucket",
"s3:PutObject"
],
resources=[
f"{output_bucket.bucket_arn}",
f"{output_bucket.bucket_arn}/*"
]
))

# 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)
input_bucket.add_event_notification(
_s3.EventType.OBJECT_CREATED, notification)

0 comments on commit fd0095e

Please sign in to comment.