Skip to content

Commit

Permalink
fix(cdk): Changed logic for getting the Lambda Typescript file paths
Browse files Browse the repository at this point in the history
When switching to ESM, CDK has a known issue with locating the function handler file, even when it's using the default naming. The previous fix worked, but broke builds on windows. This should resolve both
  • Loading branch information
flamingquaks committed Jul 25, 2024
1 parent eff57e0 commit 06a930a
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 78 deletions.
13 changes: 7 additions & 6 deletions lib/authentication/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,18 @@ import {
Tracing
} from 'aws-cdk-lib/aws-lambda'
import { NagSuppressions } from 'cdk-nag'
import path, { dirname } from 'path'
import { fileURLToPath } from 'url'

interface AuthenticationProps {
userPoolId?: string
userPoolArn?: string
userPoolClientId?: string
}

const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)

export class Authentication extends Construct {
public readonly userPool: IUserPool
private readonly identityPool: IIdentityPool
Expand Down Expand Up @@ -95,18 +100,14 @@ export class Authentication extends Construct {
'arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole'
)
)

const preTokenGenerationHookFunction = new NodejsFunction(
this,
'pre-token-generation-hook',
{
description:
"Post Authentication, Pre-Token Generation Hook that creates a user's accountId",
entry: new URL(
import.meta.url.replace(
/(.*)(\..+)/,
'$1.' + 'pre-token-generation-hook' + '$2'
)
).pathname,
entry: path.join(__dirname, 'index.pre-token-generation-hook.ts'),
handler: 'handler',
role: preTokenGenerationHookFunctionRole,
architecture: Architecture.ARM_64,
Expand Down
21 changes: 3 additions & 18 deletions lib/authorization/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,7 @@ export class Authorization extends Construct {
description:
'Function responsible for checking if requests are authorized to create items using Amazon Verified Permissions',
handler: 'handler',
entry: new URL(
import.meta.url.replace(
/(.*)(\..+)/,
'$1.' + 'action-authorization' + '$2'
)
).pathname,
entry: path.join(__dirname, 'index.action-authorization.ts'),
architecture: Architecture.ARM_64,
runtime: Runtime.NODEJS_20_X,
tracing: Tracing.ACTIVE,
Expand All @@ -157,12 +152,7 @@ export class Authorization extends Construct {
description:
'Function responsible for checking if requests are authorized to read/view data items using Amazon Verified Permissions',
handler: 'handler',
entry: new URL(
import.meta.url.replace(
/(.*)(\..+)/,
'$1.' + 'read-authorization' + '$2'
)
).pathname,
entry: path.join(__dirname, 'index.read-authorization.ts'),
architecture: Architecture.ARM_64,
runtime: Runtime.NODEJS_20_X,
tracing: Tracing.ACTIVE,
Expand All @@ -188,12 +178,7 @@ export class Authorization extends Construct {
description:
'Function responsible for checking if requested resources are authorized for viewing data and filtering out unauthorized data from the list.',
handler: 'handler',
entry: new URL(
import.meta.url.replace(
/(.*)(\..+)/,
'$1.' + 'list-filter-authorization' + '$2'
)
).pathname,
entry: path.join(__dirname, 'index.list-filter-authorization.ts'),
architecture: Architecture.ARM_64,
runtime: Runtime.NODEJS_20_X,
tracing: Tracing.ACTIVE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ import {
import { SfnStateMachine as StateMachineTarget } from 'aws-cdk-lib/aws-events-targets'
import { Construct } from 'constructs'
import { NagSuppressions } from 'cdk-nag'
import { fileURLToPath } from 'url'
import path, { dirname } from 'path'

const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)


interface DataFeedPollStepFunctionProps extends StackProps {
dataFeedTable: Table
Expand All @@ -52,9 +58,7 @@ export class DataFeedPollStepFunction extends Construct {
description:
'Function responsible for getting all enabled data feeds to poll',
handler: 'handler',
entry: new URL(
import.meta.url.replace(/(.*)(\..+)/, '$1.' + 'get-data-feeds' + '$2')
).pathname,
entry: path.join(__dirname, 'data-feed-pool-step-function.get-data-feeds.ts'),
architecture: Architecture.ARM_64,
runtime: Runtime.NODEJS_20_X,
tracing: Tracing.ACTIVE,
Expand Down
9 changes: 6 additions & 3 deletions lib/data-feed-ingestion/rss-atom-ingestion/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ import {
Tracing
} from 'aws-cdk-lib/aws-lambda'
import { NagSuppressions } from 'cdk-nag'
import { fileURLToPath } from 'url'
import path, { dirname } from 'path'

const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)

interface RssAtomFeedProps {
dataFeedTable: Table
Expand Down Expand Up @@ -71,9 +76,7 @@ export class RssAtomFeedConstruct extends Construct {
description:
'Function responsible for subscribing to a specified RSS/ATOM feed',
handler: 'handler',
entry: new URL(
import.meta.url.replace(/(.*)(\..+)/, '$1.' + 'feed-subscriber' + '$2')
).pathname,
entry: path.join(__dirname, 'index.feed-subscriber.ts'),
architecture: Architecture.ARM_64,
runtime: Runtime.NODEJS_20_X,
tracing: Tracing.ACTIVE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ import {
} from 'aws-cdk-lib/aws-stepfunctions-tasks'
import { NagSuppressions } from 'cdk-nag'
import { Construct } from 'constructs'
import path, { dirname } from 'path'
import { fileURLToPath } from 'url'

const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)

interface IngestionStepFunctionProps extends StackProps {
dataFeedTable: Table
Expand All @@ -49,9 +54,7 @@ export class IngestionStepFunction extends Construct {
description:
'Function responsible for reading feeds and return the articles for ingestion',
handler: 'handler',
entry: new URL(
import.meta.url.replace(/(.*)(\..+)/, '$1.' + 'feed-reader' + '$2')
).pathname,
entry: path.join(__dirname, 'ingestion-step-function.feed-reader.ts'),
architecture: Architecture.ARM_64,
runtime: Runtime.NODEJS_20_X,
tracing: Tracing.ACTIVE,
Expand All @@ -71,12 +74,7 @@ export class IngestionStepFunction extends Construct {
description:
'Function responsible for filtering out already ingested articles',
handler: 'handler',
entry: new URL(
import.meta.url.replace(
/(.*)(\..+)/,
'$1.' + 'filter-ingested-articles' + '$2'
)
).pathname,
entry: path.join(__dirname, 'ingestion-step-function.filter-ingested-articles.ts'),
runtime: Runtime.NODEJS_20_X,
architecture: Architecture.ARM_64,
tracing: Tracing.ACTIVE,
Expand All @@ -98,12 +96,7 @@ export class IngestionStepFunction extends Construct {
description:
"Function responsible for ingesting each article's content, summarizing it, and storing the data in DDB",
handler: 'handler',
entry: new URL(
import.meta.url.replace(
/(.*)(\..+)/,
'$1.' + 'article-ingestor' + '$2'
)
).pathname,
entry: path.join(__dirname, 'ingestion-step-function.article-ingestor.ts'),
runtime: Runtime.NODEJS_20_X,
tracing: Tracing.ACTIVE,
architecture: Architecture.ARM_64,
Expand Down Expand Up @@ -134,12 +127,7 @@ export class IngestionStepFunction extends Construct {
description:
'Function responsible for filtering out using a user provided prompt and Amazon Bedrock.',
handler: 'handler',
entry: new URL(
import.meta.url.replace(
/(.*)(\..+)/,
'$1.' + 'filter-articles-with-bedrock' + '$2'
)
).pathname,
entry: path.join(__dirname, 'ingestion-step-function.filter-articles-with-bedrock.ts'),
runtime: Runtime.NODEJS_20_X,
architecture: Architecture.ARM_64,
tracing: Tracing.ACTIVE,
Expand Down
38 changes: 11 additions & 27 deletions lib/newsletter-generator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ import { PinpointApp } from './pinpoint-app'
import { type IUserPool } from 'aws-cdk-lib/aws-cognito'
import { type UIConfig } from '../shared'
import { NagSuppressions } from 'cdk-nag'
import { fileURLToPath } from 'url'
import path, { dirname } from 'path'

const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)

interface NewsletterGeneratorProps {
dataFeedTable: Table
Expand Down Expand Up @@ -126,12 +131,7 @@ export class NewsletterGenerator extends Construct {
description:
'Function responsible for creating the newsletter campaigns for each unique email',
handler: 'handler',
entry: new URL(
import.meta.url.replace(
/(.*)(\..+)/,
'$1.' + 'newsletter-campaign-creator' + '$2'
)
).pathname,
entry: path.join(__dirname, 'index.newsletter-campaign-creator.ts'),
architecture: Architecture.ARM_64,
runtime: Runtime.NODEJS_20_X,
tracing: Tracing.ACTIVE,
Expand Down Expand Up @@ -161,9 +161,7 @@ export class NewsletterGenerator extends Construct {
description:
'Function responsible for generating the newsletter HTML & Plain Text emails',
handler: 'handler',
entry: new URL(
import.meta.url.replace(/(.*)(\..+)/, '$1.' + 'email-generator' + '$2')
).pathname,
entry: path.join(__dirname, 'index.email-generator.ts'),
architecture: Architecture.ARM_64,
runtime: Runtime.NODEJS_20_X,
tracing: Tracing.ACTIVE,
Expand Down Expand Up @@ -221,12 +219,7 @@ export class NewsletterGenerator extends Construct {
description:
'Function responsible for creating and scheduling the newsletter',
handler: 'handler',
entry: new URL(
import.meta.url.replace(
/(.*)(\..+)/,
'$1.' + 'newsletter-creator' + '$2'
)
).pathname,
entry: path.join(__dirname, 'index.newsletter-creator.ts'),
architecture: Architecture.ARM_64,
runtime: Runtime.NODEJS_20_X,
tracing: Tracing.ACTIVE,
Expand Down Expand Up @@ -268,9 +261,7 @@ export class NewsletterGenerator extends Construct {
description:
'Function responsible for getting looking up a Newsletter and its associated details',
handler: 'handler',
entry: new URL(
import.meta.url.replace(/(.*)(\..+)/, '$1.' + 'get-newsletter' + '$2')
).pathname,
entry: path.join(__dirname, 'index.get-newsletter.ts'),
architecture: Architecture.ARM_64,
runtime: Runtime.NODEJS_20_X,
tracing: Tracing.ACTIVE,
Expand All @@ -293,9 +284,7 @@ export class NewsletterGenerator extends Construct {
description:
'Function responsible for subscribing a user to the newsletter',
handler: 'handler',
entry: new URL(
import.meta.url.replace(/(.*)(\..+)/, '$1.' + 'user-subscriber' + '$2')
).pathname,
entry: path.join(__dirname, 'index.user-subscriber.ts'),
architecture: Architecture.ARM_64,
runtime: Runtime.NODEJS_20_X,
tracing: Tracing.ACTIVE,
Expand Down Expand Up @@ -335,12 +324,7 @@ export class NewsletterGenerator extends Construct {
description:
'Function responsible for unsubscribing a user from the newsletter',
handler: 'handler',
entry: new URL(
import.meta.url.replace(
/(.*)(\..+)/,
'$1.' + 'user-unsubscriber' + '$2'
)
).pathname,
entry: path.join(__dirname, 'index.user-unsubscriber.ts'),
architecture: Architecture.ARM_64,
runtime: Runtime.NODEJS_20_X,
tracing: Tracing.ACTIVE,
Expand Down

0 comments on commit 06a930a

Please sign in to comment.