-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* increate timeout; get key from api and then query the presignurl * fix url * use poll * promise * promise2 * add HEAD to s3 method * promise race * promise race * promise race * promise wait * invoke report upload lambda * fix invoke size * fix makefile deploy lambda * add report bucket * export typeorm module * fix duplicate connection * fix duplicate connection * remove console * remove console * clean up the code * fix duplicate code * no undefined type as there is either null or string * fix param name to ha_pcn_id * fix param name to ha_pcn_id --------- Co-authored-by: Jerry Wang <jerryappleid761208@gmail.com>
- Loading branch information
1 parent
2fb979e
commit 7714762
Showing
9 changed files
with
243 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { NestFactory } from '@nestjs/core'; | ||
import { Context, Handler } from 'aws-lambda'; | ||
import { AppModule } from './app.module'; | ||
import { AppLogger } from './common/logger.service'; | ||
import { ReportService } from './report/report.service'; | ||
import { ReportS3Service } from './report/report.s3.service'; | ||
|
||
let app: any = null; | ||
|
||
/** | ||
* Design this function to trigger existing NestJs appliation services without Api-Getway | ||
* All the schedule and backgroud job trigger will be added here. | ||
* This handler will cache the report 4 data for every existing period | ||
*/ | ||
export const handler: Handler = async (event, context: Context) => { | ||
if (!app) { | ||
app = await NestFactory.create(AppModule); | ||
await app.init(); | ||
} | ||
const reportS3Service = app.get(ReportS3Service); | ||
const reportService = app.get(ReportService); | ||
|
||
const appLogger = app.get(AppLogger); | ||
appLogger.log({ event }); | ||
appLogger.log({ context }); | ||
try { | ||
if ( | ||
event.hasOwnProperty('s3Key') && | ||
event.hasOwnProperty('param') && | ||
event.hasOwnProperty('path') | ||
) { | ||
const { s3Key, path, param } = event; | ||
appLogger.log('Start uploading reports...'); | ||
let data = []; | ||
if (path === 'extract-data') { | ||
data = await reportService.extractApplicantsData( | ||
{ from: param.from, to: param.to }, | ||
param.ha_pcn_id, | ||
); | ||
} else if (path === 'extract-milestone') { | ||
data = await reportService.extractMilestoneData( | ||
{ from: param.from, to: param.to }, | ||
param.ha_pcn_id, | ||
); | ||
} | ||
await reportS3Service | ||
.uploadFile(s3Key, data) | ||
.then(() => { | ||
appLogger.log('File uploaded successfully.'); | ||
}) | ||
.catch((err: any) => { | ||
appLogger.error('File upload failed: ', err); | ||
}); | ||
} | ||
} catch (e) { | ||
appLogger.error(e); | ||
} | ||
appLogger.log('...end caching reports'); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
resource "aws_lambda_function" "UploadReports" { | ||
description = "Trigger uploading of Reports" | ||
function_name = local.report_s3_upload_lambda_name | ||
role = aws_iam_role.lambda.arn | ||
runtime = "nodejs18.x" | ||
filename = "./build/empty_lambda.zip" | ||
source_code_hash = filebase64sha256("./build/empty_lambda.zip") | ||
handler = "api/uploadreports.handler" | ||
memory_size = var.function_memory_mb | ||
timeout = 300 | ||
|
||
vpc_config { | ||
security_group_ids = [data.aws_security_group.app.id] | ||
subnet_ids = data.aws_subnet_ids.app.ids | ||
} | ||
|
||
lifecycle { | ||
ignore_changes = [ | ||
# Ignore changes to tags, e.g. because a management agent | ||
# updates these based on some ruleset managed elsewhere. | ||
filename, | ||
source_code_hash, | ||
source_code_size, | ||
last_modified, | ||
] | ||
} | ||
|
||
environment { | ||
variables = { | ||
NODE_ENV = "production" | ||
RUNTIME_ENV = "hosted" | ||
AUTH_URL = data.aws_ssm_parameter.keycloak_url.value | ||
AUTH_REALM = data.aws_ssm_parameter.keycloak_realm.value | ||
TARGET_ENV = var.target_env | ||
AWS_S3_REGION = var.region | ||
BUILD_ID = var.build_id | ||
BUILD_INFO = var.build_info | ||
POSTGRES_USERNAME = var.db_username | ||
POSTGRES_PASSWORD = data.aws_ssm_parameter.postgres_password.value | ||
POSTGRES_HOST = aws_rds_cluster.pgsql.endpoint | ||
POSTGRES_DATABASE = aws_rds_cluster.pgsql.database_name | ||
REPORTS_BUCKET = var.reports_bucket | ||
NO_COLOR = "true" | ||
} | ||
} | ||
} | ||
|
||
resource "aws_lambda_permission" "allow_invoke_by_lambda" { | ||
statement_id = "AllowInvokeByAnotherLambda" # Unique ID for the permission | ||
action = "lambda:InvokeFunction" | ||
function_name = aws_lambda_function.UploadReports.function_name | ||
principal = "lambda.amazonaws.com" # This specifies that the invoking service is Lambda | ||
# ARN of the role of the Lambda that will invoke this function | ||
source_arn = aws_lambda_function.api.arn # Adjust this with the correct resource | ||
} |