Skip to content
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
22 changes: 15 additions & 7 deletions src/cdk/create-lambda-function.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { LambdaRoute, StackConfig } from '../parse-stack-config.js';
import type { Stack } from 'aws-cdk-lib';
import { type Stack, RemovalPolicy } from 'aws-cdk-lib';

import { getDomainName } from '../utils/get-domain-name.js';
import { getHash } from '../utils/get-hash.js';
Expand Down Expand Up @@ -55,25 +55,27 @@ export function createLambdaFunction(

const { monitoring } = stackConfig;

const uniqueFunctionNameHash = getHash(uniqueFunctionName);

const filesystemProps = filesystem
? {
vpc: aws_ec2.Vpc.fromLookup(stack, `Vpc${getHash(uniqueFunctionName)}`, {
vpc: aws_ec2.Vpc.fromLookup(stack, `Vpc${uniqueFunctionNameHash}`, {
vpcId: filesystem.vpcId,
}),
filesystem: aws_lambda.FileSystem.fromEfsAccessPoint(
aws_efs.AccessPoint.fromAccessPointAttributes(
stack,
`AccessPoint${getHash(uniqueFunctionName)}`,
`AccessPoint${uniqueFunctionNameHash}`,
{
accessPointId: filesystem.accessPointId,
fileSystem: aws_efs.FileSystem.fromFileSystemAttributes(
stack,
`FileSystem${getHash(uniqueFunctionName)}`,
`FileSystem${uniqueFunctionNameHash}`,
{
fileSystemId: filesystem.fileSystemId,
securityGroup: aws_ec2.SecurityGroup.fromSecurityGroupId(
stack,
`SecurityGroup${getHash(uniqueFunctionName)}`,
`SecurityGroup${uniqueFunctionNameHash}`,
filesystem.securityGroupId,
),
},
Expand All @@ -85,7 +87,13 @@ export function createLambdaFunction(
}
: undefined;

const fn = new aws_lambda.Function(stack, `Function${getHash(uniqueFunctionName)}`, {
const logGroup = new aws_logs.LogGroup(stack, `LogGroup${uniqueFunctionNameHash}`, {
retention: aws_logs.RetentionDays.TWO_WEEKS,
logGroupName: `/aws/lambda/${uniqueFunctionName}`,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could add
removalPolicy:RemovalPolicy.DESTROY

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removalPolicy: RemovalPolicy.DESTROY was added to lambda and authorizer log groups

removalPolicy: RemovalPolicy.DESTROY,
});

const fn = new aws_lambda.Function(stack, `Function${uniqueFunctionNameHash}`, {
functionName: uniqueFunctionName,
code: aws_lambda.Code.fromAsset(dirname(path)),
handler: `${basename(path, extname(path))}.handler`,
Expand All @@ -99,7 +107,7 @@ export function createLambdaFunction(
monitoring === true || monitoring?.lambdaInsightsEnabled
? aws_lambda.LambdaInsightsVersion.VERSION_1_0_229_0
: undefined,
logRetention: aws_logs.RetentionDays.TWO_WEEKS,
logGroup,
role: lambdaServiceRole,
...filesystemProps,
});
Expand Down
13 changes: 10 additions & 3 deletions src/cdk/create-request-authorizer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { StackConfig } from '../parse-stack-config.js';
import type { Stack } from 'aws-cdk-lib';
import { type Stack, RemovalPolicy } from 'aws-cdk-lib';

import { getDomainName } from '../utils/get-domain-name.js';
import { getHash } from '../utils/get-hash.js';
Expand All @@ -19,9 +19,16 @@ export function createRequestAuthorizer(

const domainName = getDomainName(stackConfig);
const functionName = `aws-simple-request-authorizer-${getHash(domainName)}`;
const functionNameHash = getHash(functionName);

const logGroup = new aws_logs.LogGroup(stack, `LogGroup${functionNameHash}`, {
retention: aws_logs.RetentionDays.TWO_WEEKS,
logGroupName: `/aws/lambda/${functionName}`,
removalPolicy: RemovalPolicy.DESTROY,
});

return new aws_apigateway.RequestAuthorizer(stack, `RequestAuthorizer`, {
handler: new aws_lambda.Function(stack, `Function${getHash(functionName)}`, {
handler: new aws_lambda.Function(stack, `Function${functionNameHash}`, {
functionName,
code: aws_lambda.Code.fromAsset(
join(dirname(fileURLToPath(import.meta.url)), `request-authorizer`),
Expand All @@ -34,7 +41,7 @@ export function createRequestAuthorizer(
},
runtime: aws_lambda.Runtime.NODEJS_22_X,
tracing: aws_lambda.Tracing.PASS_THROUGH,
logRetention: aws_logs.RetentionDays.TWO_WEEKS,
logGroup,
}),
identitySources: [aws_apigateway.IdentitySource.header(`Authorization`)],
resultsCacheTtl: Duration.seconds(authentication.cacheTtlInSeconds ?? 300),
Expand Down