Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: forum ads api third #344

Merged
merged 4 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/constructs/business/rest-api-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
TimetableFunctions,
ForumThreadFunctions,
ForumCommentFunctions,
AdsImageProcessFunctions,
AdsImageProcessFunctionsAPI,
} from '../common/lambda-functions';
import { AbstractRestApiEndpoint } from './api-endpoint';

Expand Down Expand Up @@ -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',
{
Expand All @@ -79,7 +79,7 @@ export class ForumAdsApiService extends RestApiService {
);

const getIntegration = new apigw.LambdaIntegration(
adsImageProcessFunctions.getFunction,
adsImageProcessFunctionsAPI.getFunction,
{ proxy: true },
);

Expand Down
106 changes: 45 additions & 61 deletions lib/constructs/common/lambda-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand All @@ -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,
// });
}
}
16 changes: 10 additions & 6 deletions lib/constructs/persistence/data-pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
SyllabusScraper,
SyllabusUpdateFunction,
ThreadImageProcessFunctions,
AdsImageProcessFunctions,
AdsImageProcessFunctionsPipeline,
} from '../common/lambda-functions';

export enum Worker {
Expand Down Expand Up @@ -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, {
Expand Down
Loading