From 225d9d61f85ba71e038e81f48d0edebf8173996a Mon Sep 17 00:00:00 2001 From: "Y.H LIEN" Date: Mon, 9 Oct 2023 22:42:20 +0900 Subject: [PATCH 1/3] fix: adsApi lambda function problem --- lib/constructs/common/lambda-functions.ts | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/lib/constructs/common/lambda-functions.ts b/lib/constructs/common/lambda-functions.ts index 7ddf0bf5e..2457dfc8b 100644 --- a/lib/constructs/common/lambda-functions.ts +++ b/lib/constructs/common/lambda-functions.ts @@ -812,7 +812,7 @@ export class ForumCommentFunctions extends Construct { export class ThreadImageProcessFunctions extends Construct { // readonly getFunction: lambda.Function; - readonly syncImageFunction: lambda.Function; + // readonly syncImageFunction: lambda.Function; readonly resizeImageFunction: lambda.Function; // readonly deleteFunction: lambda.Function; @@ -877,19 +877,6 @@ export class ThreadImageProcessFunctions extends Construct { }, ); - this.syncImageFunction = new lambda_py.PythonFunction(this, 'sync-image', { - entry: 'src/lambda/sync-image', - description: - 'post image to dyanamo db database when image inputed in s3 bucket', - functionName: 'sync-image', - logRetention: logs.RetentionDays.ONE_MONTH, - memorySize: 256, - role: DBSyncRole, - runtime: lambda.Runtime.PYTHON_3_9, - timeout: Duration.seconds(5), - environment: props.envVars, - }); - this.resizeImageFunction = new lambda_py.PythonFunction( this, 'resize-image', From 12b5761d8f83f09fb46bee13ea99522297d42be4 Mon Sep 17 00:00:00 2001 From: "Y.H LIEN" Date: Mon, 9 Oct 2023 23:11:15 +0900 Subject: [PATCH 2/3] fix: adsApi lambda function problem second --- lib/constructs/common/lambda-functions.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/constructs/common/lambda-functions.ts b/lib/constructs/common/lambda-functions.ts index 2457dfc8b..8f3412282 100644 --- a/lib/constructs/common/lambda-functions.ts +++ b/lib/constructs/common/lambda-functions.ts @@ -931,13 +931,13 @@ export class AdsImageProcessFunctions extends Construct { const DBReadRole: iam.LazyRole = new iam.LazyRole( this, - 'dynamo-s3-read-role', + 'dynamodb-s3-lambda-ads-imgs-read', { assumedBy: new iam.ServicePrincipal(AwsServicePrincipal.LAMBDA), description: 'Allow lambda function to perform read operation on dynamodb and s3', path: `/service-role/${AwsServicePrincipal.LAMBDA}/`, - roleName: 'dynamodb-s3-lambda-read-ads-imgs', // Changed the new role name + roleName: 'dynamodb-s3-lambda-ads-imgs-read', managedPolicies: [ iam.ManagedPolicy.fromManagedPolicyArn( this, @@ -960,13 +960,13 @@ export class AdsImageProcessFunctions extends Construct { const DBSyncRole: iam.LazyRole = new iam.LazyRole( this, - 'dynamo-s3-ads-sync-role', + 'dynamo-s3-ads-imgs-sync-role', { assumedBy: new iam.ServicePrincipal(AwsServicePrincipal.LAMBDA), description: 'Allow lambda function to perform crud operation on dynamodb and s3', path: `/service-role/${AwsServicePrincipal.LAMBDA}/`, - roleName: 'dynamodb-s3-ads-sync-role', + roleName: 'dynamodb-s3-ads-imgs-sync-role', managedPolicies: [ iam.ManagedPolicy.fromManagedPolicyArn( this, From 9754d90266c3d5f001499f5911f3c10b4750163e Mon Sep 17 00:00:00 2001 From: "Y.H LIEN" Date: Tue, 10 Oct 2023 00:05:07 +0900 Subject: [PATCH 3/3] fix: adsApi lambda function problem third --- lib/constructs/business/rest-api-service.ts | 6 +- lib/constructs/common/lambda-functions.ts | 106 +++++++++----------- lib/constructs/persistence/data-pipeline.ts | 16 +-- 3 files changed, 58 insertions(+), 70 deletions(-) diff --git a/lib/constructs/business/rest-api-service.ts b/lib/constructs/business/rest-api-service.ts index ae9faebf7..b321ef4ec 100644 --- a/lib/constructs/business/rest-api-service.ts +++ b/lib/constructs/business/rest-api-service.ts @@ -28,7 +28,7 @@ import { TimetableFunctions, ForumThreadFunctions, ForumCommentFunctions, - AdsImageProcessFunctions, + AdsImageProcessFunctionsAPI, } from '../common/lambda-functions'; import { AbstractRestApiEndpoint } from './api-endpoint'; @@ -68,7 +68,7 @@ export class ForumAdsApiService extends RestApiService { // Create resources for the api const root = scope.apiEndpoint.root.addResource('adsImgs'); - const adsImageProcessFunctions = new AdsImageProcessFunctions( + const adsImageProcessFunctionsAPI = new AdsImageProcessFunctionsAPI( this, 'crud-functions', { @@ -79,7 +79,7 @@ export class ForumAdsApiService extends RestApiService { ); const getIntegration = new apigw.LambdaIntegration( - adsImageProcessFunctions.getFunction, + adsImageProcessFunctionsAPI.getFunction, { proxy: true }, ); diff --git a/lib/constructs/common/lambda-functions.ts b/lib/constructs/common/lambda-functions.ts index 8f3412282..5c8cc9eb3 100644 --- a/lib/constructs/common/lambda-functions.ts +++ b/lib/constructs/common/lambda-functions.ts @@ -920,44 +920,16 @@ export class ThreadImageProcessFunctions extends Construct { } } -export class AdsImageProcessFunctions extends Construct { - readonly getFunction: lambda.Function; +// Function for pipeline +export class AdsImageProcessFunctionsPipeline extends Construct { + // readonly getFunction: lambda.Function; readonly syncImageFunction: lambda.Function; - readonly resizeImageFunction: lambda.Function; + // readonly resizeImageFunction: lambda.Function; // readonly deleteFunction: lambda.Function; constructor(scope: Construct, id: string, props: FunctionsProps) { super(scope, id); - const DBReadRole: iam.LazyRole = new iam.LazyRole( - this, - 'dynamodb-s3-lambda-ads-imgs-read', - { - assumedBy: new iam.ServicePrincipal(AwsServicePrincipal.LAMBDA), - description: - 'Allow lambda function to perform read operation on dynamodb and s3', - path: `/service-role/${AwsServicePrincipal.LAMBDA}/`, - roleName: 'dynamodb-s3-lambda-ads-imgs-read', - managedPolicies: [ - iam.ManagedPolicy.fromManagedPolicyArn( - this, - 'basic-exec', - 'arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole', - ), - iam.ManagedPolicy.fromManagedPolicyArn( - this, - 'db-read-only', - 'arn:aws:iam::aws:policy/AmazonDynamoDBReadOnlyAccess', - ), - iam.ManagedPolicy.fromManagedPolicyArn( - this, - 's3-read-only', - 'arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess', - ), - ], - }, - ); - const DBSyncRole: iam.LazyRole = new iam.LazyRole( this, 'dynamo-s3-ads-imgs-sync-role', @@ -999,6 +971,47 @@ export class AdsImageProcessFunctions extends Construct { timeout: Duration.seconds(5), environment: props.envVars, }); + } +} + +// Function for api +export class AdsImageProcessFunctionsAPI extends Construct { + readonly getFunction: lambda.Function; + // readonly syncImageFunction: lambda.Function; + // readonly resizeImageFunction: lambda.Function; + // readonly deleteFunction: lambda.Function; + + constructor(scope: Construct, id: string, props: FunctionsProps) { + super(scope, id); + + const DBReadRole: iam.LazyRole = new iam.LazyRole( + this, + 'dynamodb-s3-lambda-ads-imgs-read', + { + assumedBy: new iam.ServicePrincipal(AwsServicePrincipal.LAMBDA), + description: + 'Allow lambda function to perform read operation on dynamodb and s3', + path: `/service-role/${AwsServicePrincipal.LAMBDA}/`, + roleName: 'dynamodb-s3-lambda-ads-imgs-read', + managedPolicies: [ + iam.ManagedPolicy.fromManagedPolicyArn( + this, + 'basic-exec', + 'arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole', + ), + iam.ManagedPolicy.fromManagedPolicyArn( + this, + 'db-read-only', + 'arn:aws:iam::aws:policy/AmazonDynamoDBReadOnlyAccess', + ), + iam.ManagedPolicy.fromManagedPolicyArn( + this, + 's3-read-only', + 'arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess', + ), + ], + }, + ); this.getFunction = new lambda_py.PythonFunction(this, 'get-imgs-list', { entry: 'src/lambda/get-imgs-list', @@ -1011,34 +1024,5 @@ export class AdsImageProcessFunctions extends Construct { timeout: Duration.seconds(3), environment: props.envVars, }); - - // this.resizeImageFunction = new lambda_py.PythonFunction( - // this, - // "resize-image", - // { - // entry: "src/lambda/resize-image", - // description: - // "Resize uploaded image to a thumbnail and store in s3 bucket", - // functionName: "patch-image", - // logRetention: logs.RetentionDays.ONE_MONTH, - // memorySize: 256, - // role: DBSyncRole, - // runtime: lambda.Runtime.PYTHON_3_9, - // timeout: Duration.seconds(5), - // environment: props.envVars, - // } - // ); - - // this.deleteFunction = new lambda_py.PythonFunction(this, "delete-comment", { - // entry: "src/lambda/delete-comment", - // description: "Delete forum comment in the database.", - // functionName: "delete-forum-comment", - // logRetention: logs.RetentionDays.ONE_MONTH, - // memorySize: 128, - // role: DBPutRole, - // runtime: lambda.Runtime.PYTHON_3_9, - // timeout: Duration.seconds(3), - // environment: props.envVars, - // }); } } diff --git a/lib/constructs/persistence/data-pipeline.ts b/lib/constructs/persistence/data-pipeline.ts index 5862b2f70..5af74099e 100644 --- a/lib/constructs/persistence/data-pipeline.ts +++ b/lib/constructs/persistence/data-pipeline.ts @@ -16,7 +16,7 @@ import { SyllabusScraper, SyllabusUpdateFunction, ThreadImageProcessFunctions, - AdsImageProcessFunctions, + AdsImageProcessFunctionsPipeline, } from '../common/lambda-functions'; export enum Worker { @@ -285,12 +285,16 @@ export class AdsDataPipeline extends AbstractDataPipeline { this.dataWarehouse = props.dataWarehouse!; - this.processor = new AdsImageProcessFunctions(this, 'image-process-func', { - envVars: { - ['BUCKET_NAME']: this.dataSource.bucketName, - ['TABLE_NAME']: this.dataWarehouse.tableName, + this.processor = new AdsImageProcessFunctionsPipeline( + this, + 'image-process-func', + { + envVars: { + ['BUCKET_NAME']: this.dataSource.bucketName, + ['TABLE_NAME']: this.dataWarehouse.tableName, + }, }, - }).syncImageFunction; + ).syncImageFunction; this.processor.addEventSource( new event_sources.S3EventSource(this.dataSource, {