Skip to content

Commit

Permalink
Merge pull request #634 from umccr/bugfix/upgrade-lambda-mem-sizes
Browse files Browse the repository at this point in the history
  • Loading branch information
alexiswl authored Nov 2, 2024
2 parents 27f5e3b + 7121cb4 commit 6da7013
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ Given a cttsov2 success event we need to
import { Construct } from 'constructs';
import * as dynamodb from 'aws-cdk-lib/aws-dynamodb';
import path from 'path';
import { NagSuppressions } from 'cdk-nag';
import * as sfn from 'aws-cdk-lib/aws-stepfunctions';
import * as iam from 'aws-cdk-lib/aws-iam';
import * as ssm from 'aws-cdk-lib/aws-ssm';
import * as secretsManager from 'aws-cdk-lib/aws-secretsmanager';
import * as events from 'aws-cdk-lib/aws-events';
Expand Down Expand Up @@ -107,6 +109,7 @@ export class Cttsov2CompleteToPieriandxConstruct extends Construct {
index: 'get_data_from_redcap.py',
handler: 'handler',
timeout: Duration.seconds(60),
memorySize: 2048,
});

const getDeidentifiedCaseMetadataPyLambdaObj = new PythonFunction(
Expand All @@ -118,6 +121,7 @@ export class Cttsov2CompleteToPieriandxConstruct extends Construct {
architecture: lambda.Architecture.ARM_64,
index: 'get_deidentified_case_metadata.py',
handler: 'handler',
memorySize: 1024,
}
);
const getIdentifiedCaseMetadataPyLambdaObj = new PythonFunction(
Expand All @@ -129,6 +133,7 @@ export class Cttsov2CompleteToPieriandxConstruct extends Construct {
architecture: lambda.Architecture.ARM_64,
index: 'get_identified_case_metadata.py',
handler: 'handler',
memorySize: 1024,
}
);
const getPieriandxDataFilesPyLambdaObj = new PythonFunction(
Expand All @@ -144,6 +149,7 @@ export class Cttsov2CompleteToPieriandxConstruct extends Construct {
environment: {
ICAV2_ACCESS_TOKEN_SECRET_ID: props.icav2AccessTokenSecretObj.secretName,
},
memorySize: 1024,
}
);
const getProjectInfoPyLambdaObj = new PythonFunction(this, 'getProjectInfoPyLambdaObj', {
Expand All @@ -157,11 +163,22 @@ export class Cttsov2CompleteToPieriandxConstruct extends Construct {
/*
Handle lambda permissions
*/
props.redcapLambdaObj.latestVersion.grantInvoke(getDataFromRedCapPyLambdaObj.currentVersion);
props.redcapLambdaObj.grantInvoke(getDataFromRedCapPyLambdaObj.currentVersion);
getDataFromRedCapPyLambdaObj.addEnvironment(
'REDCAP_LAMBDA_FUNCTION_NAME',
props.redcapLambdaObj.functionName
);
// FIXME - cannot get the 'current' version of an IFunction object
NagSuppressions.addResourceSuppressions(
getDataFromRedCapPyLambdaObj,
[
{
id: 'AwsSolutions-IAM5',
reason: 'Cannot get latest version of redcap lambda function ($LATEST) will not work',
},
],
true
);

// Allow the getPieriandxDataFilesPyLambdaObj to read the secret
props.icav2AccessTokenSecretObj.grantRead(getPieriandxDataFilesPyLambdaObj.currentVersion);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"""

# Standard imports
import logging
import typing
from typing import List
from time import sleep
Expand All @@ -18,14 +19,14 @@
import json
import pytz
from datetime import datetime
import logging

if typing.TYPE_CHECKING:
from mypy_boto3_lambda import LambdaClient

# Set logger
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
logger = logging.getLogger()
logger.setLevel(level=logging.INFO)

# Globals
AUS_TIMEZONE = pytz.timezone("Australia/Melbourne")
Expand Down Expand Up @@ -86,6 +87,7 @@ def warm_up_lambda():
)
return True
except ClientError as e:
print(f"Error warming up lambda: {e}")
logger.info(f"Error warming up lambda: {e}")
return False

Expand Down Expand Up @@ -287,8 +289,10 @@ def handler(event, context) -> Dict:
:return:
"""
# Wait for lambda to warm up
logger.info("Warming up redcap lambda")
while not warm_up_lambda():
sleep(10)
logger.info("Redcap lambda warmup complete!")

# Return
try:
Expand Down Expand Up @@ -320,6 +324,25 @@ def handler(event, context) -> Dict:
# indent=4
# )
# )


# if __name__ == '__main__':
# # Or 'umccr-staging' / 'umccr-production'
# environ['AWS_PROFILE'] = 'umccr-development'
# environ['AWS_REGION'] = 'ap-southeast-2'
# # Or 'redcap-apis-stg-lambda-function' / 'redcap-apis-prod-lambda-function'
# environ['REDCAP_LAMBDA_FUNCTION_NAME'] = 'redcap-apis-dev-lambda-function'
# print(
# json.dumps(
# handler(
# event={
# "library_id": "L2401529"
# },
# context=None
# ),
# indent=4
# )
# )
#
# # {
# # "redcap_data": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"""

# Imports
import typing
from typing import Dict

import pytz
Expand All @@ -43,7 +42,7 @@
# Globals
AUS_TIMEZONE = pytz.timezone("Australia/Melbourne")
AUS_TIME = datetime.now(AUS_TIMEZONE)
AUS_TIME_AS_STR = f"{AUS_TIME.date().isoformat()}T{AUS_TIME.time().isoformat(timespec='seconds')}{AUS_TIME.strftime("%z")}"
AUS_TIME_AS_STR = f"{AUS_TIME.date().isoformat()}T{AUS_TIME.time().isoformat(timespec='seconds')}{AUS_TIME.strftime('%z')}"

DEFAULT_INDICATION = "NA"

Expand Down

0 comments on commit 6da7013

Please sign in to comment.