Skip to content

Commit

Permalink
feat: adding permissions and envvar for forum thread (#328)
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonNotJson authored Sep 22, 2023
1 parent 0a813a7 commit 1fdff16
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 30 deletions.
1 change: 1 addition & 0 deletions lib/constructs/business/rest-api-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,7 @@ export class ForumThreadsApiService extends RestApiService {
{
envVars: {
TABLE_NAME: props.dataSource!,
BUCKET_NAME: 'wasedatime-thread-img',
},
},
);
Expand Down
66 changes: 36 additions & 30 deletions lib/constructs/common/lambda-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -495,9 +495,9 @@ export class ForumThreadFunctions extends Construct {
constructor(scope: Construct, id: string, props: FunctionsProps) {
super(scope, id);

const dynamoDBReadRole: iam.LazyRole = new iam.LazyRole(
const DBReadRole: iam.LazyRole = new iam.LazyRole(
this,
'dynamo-read-role',
'dynamo-s3-read-role',
{
assumedBy: new iam.ServicePrincipal(AwsServicePrincipal.LAMBDA),
description:
Expand All @@ -515,34 +515,40 @@ export class ForumThreadFunctions extends Construct {
'db-read-only',
'arn:aws:iam::aws:policy/AmazonDynamoDBReadOnlyAccess',
),
],
},
);

const dynamoDBPutRole: iam.LazyRole = new iam.LazyRole(
this,
'dynamo-put-role',
{
assumedBy: new iam.ServicePrincipal(AwsServicePrincipal.LAMBDA),
description:
'Allow lambda function to perform crud operation on dynamodb',
path: `/service-role/${AwsServicePrincipal.LAMBDA}/`,
roleName: 'dynamodb-lambda-write-thread',
managedPolicies: [
iam.ManagedPolicy.fromManagedPolicyArn(
this,
'basic-exec1',
'arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole',
),
iam.ManagedPolicy.fromManagedPolicyArn(
this,
'db-full-access',
'arn:aws:iam::aws:policy/AmazonDynamoDBFullAccess',
's3-read-only',
'arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess',
),
],
},
);

const DBPutRole: iam.LazyRole = new iam.LazyRole(this, 'dynamo-put-role', {
assumedBy: new iam.ServicePrincipal(AwsServicePrincipal.LAMBDA),
description:
'Allow lambda function to perform crud operation on dynamodb',
path: `/service-role/${AwsServicePrincipal.LAMBDA}/`,
roleName: 'dynamodb-s3-put-role',
managedPolicies: [
iam.ManagedPolicy.fromManagedPolicyArn(
this,
'basic-exec1',
'arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole',
),
iam.ManagedPolicy.fromManagedPolicyArn(
this,
'db-full-access',
'arn:aws:iam::aws:policy/AmazonDynamoDBFullAccess',
),
iam.ManagedPolicy.fromManagedPolicyArn(
this,
's3-full-access',
'arn:aws:iam::aws:policy/AmazonS3FullAccess',
),
],
});

this.getAllFunction = new lambda_py.PythonFunction(
this,
'get-all-threads',
Expand All @@ -552,9 +558,9 @@ export class ForumThreadFunctions extends Construct {
functionName: 'get-all-threads',
logRetention: logs.RetentionDays.ONE_MONTH,
memorySize: 128,
role: dynamoDBReadRole,
role: DBReadRole,
runtime: lambda.Runtime.PYTHON_3_9,
timeout: Duration.seconds(3),
timeout: Duration.seconds(10),
environment: props.envVars,
},
);
Expand All @@ -568,7 +574,7 @@ export class ForumThreadFunctions extends Construct {
functionName: 'get-user-threads',
logRetention: logs.RetentionDays.ONE_MONTH,
memorySize: 128,
role: dynamoDBReadRole,
role: DBReadRole,
runtime: lambda.Runtime.PYTHON_3_9,
timeout: Duration.seconds(3),
environment: props.envVars,
Expand All @@ -584,7 +590,7 @@ export class ForumThreadFunctions extends Construct {
functionName: 'get-single-thread',
logRetention: logs.RetentionDays.ONE_MONTH,
memorySize: 128,
role: dynamoDBPutRole,
role: DBPutRole,
runtime: lambda.Runtime.PYTHON_3_9,
timeout: Duration.seconds(3),
environment: props.envVars,
Expand All @@ -597,7 +603,7 @@ export class ForumThreadFunctions extends Construct {
functionName: 'post-forum-thread',
logRetention: logs.RetentionDays.ONE_MONTH,
memorySize: 256,
role: dynamoDBPutRole,
role: DBPutRole,
runtime: lambda.Runtime.PYTHON_3_9,
timeout: Duration.seconds(5),
environment: props.envVars,
Expand All @@ -612,7 +618,7 @@ export class ForumThreadFunctions extends Construct {
functionName: 'patch-forum-thread',
logRetention: logs.RetentionDays.ONE_MONTH,
memorySize: 256,
role: dynamoDBPutRole,
role: DBPutRole,
runtime: lambda.Runtime.PYTHON_3_9,
timeout: Duration.seconds(5),
environment: props.envVars,
Expand All @@ -627,7 +633,7 @@ export class ForumThreadFunctions extends Construct {
functionName: 'delete-forum-thread',
logRetention: logs.RetentionDays.ONE_MONTH,
memorySize: 128,
role: dynamoDBPutRole,
role: DBPutRole,
runtime: lambda.Runtime.PYTHON_3_9,
timeout: Duration.seconds(3),
environment: props.envVars,
Expand Down

0 comments on commit 1fdff16

Please sign in to comment.