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

Feature: allow all optional props for the NodejsFunction CDK construct to be passed via defineFunction in @aws-amplify/backend-function #1971

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions .changeset/brave-pugs-develop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@aws-amplify/backend-function': minor
---

Allow all optional props for the `NodejsFunction` CDK construct to be passed via `defineFunction`
16 changes: 16 additions & 0 deletions packages/backend-function/src/factory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -513,4 +513,20 @@ void describe('AmplifyFunctionFactory', () => {
'function-Lambda'
);
});

void it('allows passing retryAttempts prop to NodejsFunction', () => {
const functionFactory = defineFunction({
entry: './test-assets/default-lambda/handler.ts',
name: 'myCoolNodejsFunctionLambda',
retryAttempts: 2,
});
const lambda = functionFactory.getInstance(getInstanceProps);
const stack = Stack.of(lambda.resources.lambda);
const template = Template.fromStack(stack);
template.resourceCountIs('AWS::Lambda::Function', 1);
template.resourceCountIs('AWS::Lambda::EventInvokeConfig', 1);
template.hasResourceProperties('AWS::Lambda::EventInvokeConfig', {
MaximumRetryAttempts: 2,
});
});
});
46 changes: 38 additions & 8 deletions packages/backend-function/src/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ import {
SsmEnvironmentEntry,
} from '@aws-amplify/plugin-types';
import { Construct } from 'constructs';
import { NodejsFunction, OutputFormat } from 'aws-cdk-lib/aws-lambda-nodejs';
import {
type NodejsFunctionProps as CDKNodejsFunctionProps,
NodejsFunction,
OutputFormat,
} from 'aws-cdk-lib/aws-lambda-nodejs';
import * as path from 'path';
import { Duration, Stack, Tags } from 'aws-cdk-lib';
import { CfnFunction, Runtime } from 'aws-cdk-lib/aws-lambda';
Expand Down Expand Up @@ -60,7 +64,7 @@ export type FunctionSchedule = TimeInterval | CronSchedule;
* Entry point for defining a function in the Amplify ecosystem
*/
export const defineFunction = (
props: FunctionProps = {}
props: FunctionPropsWithOptional = {}
): ConstructFactory<
ResourceProvider<FunctionResources> &
ResourceAccessAcceptorFactory &
Expand Down Expand Up @@ -126,6 +130,12 @@ export type FunctionProps = {
schedule?: FunctionSchedule | FunctionSchedule[];
};

export type FunctionPropsWithOptional = Omit<
Partial<CDKNodejsFunctionProps>,
keyof FunctionProps
> &
FunctionProps;

/**
* Create Lambda functions in the context of an Amplify backend definition
*/
Expand All @@ -135,7 +145,7 @@ class FunctionFactory implements ConstructFactory<AmplifyFunction> {
* Create a new AmplifyFunctionFactory
*/
constructor(
private readonly props: FunctionProps,
private readonly props: FunctionPropsWithOptional,
private readonly callerStack?: string
) {}

Expand All @@ -161,7 +171,7 @@ class FunctionFactory implements ConstructFactory<AmplifyFunction> {
): HydratedFunctionProps => {
const name = this.resolveName();
resourceNameValidator?.validate(name);
return {
const hydratedProps = {
name,
entry: this.resolveEntry(),
timeoutSeconds: this.resolveTimeout(),
Expand All @@ -170,6 +180,10 @@ class FunctionFactory implements ConstructFactory<AmplifyFunction> {
runtime: this.resolveRuntime(),
schedule: this.resolveSchedule(),
};
return {
...this.props,
...hydratedProps,
};
};

private resolveName = () => {
Expand Down Expand Up @@ -277,12 +291,14 @@ class FunctionFactory implements ConstructFactory<AmplifyFunction> {
}

type HydratedFunctionProps = Required<FunctionProps>;
type HydratedFunctionPropsWithOptional = FunctionPropsWithOptional &
HydratedFunctionProps;

class FunctionGenerator implements ConstructContainerEntryGenerator {
readonly resourceGroupName = 'function';

constructor(
private readonly props: HydratedFunctionProps,
private readonly props: HydratedFunctionPropsWithOptional,
private readonly outputStorageStrategy: BackendOutputStorageStrategy<FunctionOutput>
) {}

Expand Down Expand Up @@ -312,7 +328,7 @@ class AmplifyFunction
constructor(
scope: Construct,
id: string,
props: HydratedFunctionProps,
props: HydratedFunctionPropsWithOptional,
backendSecretResolver: BackendSecretResolver,
outputStorageStrategy: BackendOutputStorageStrategy<FunctionOutput>
) {
Expand Down Expand Up @@ -355,7 +371,7 @@ class AmplifyFunction

let functionLambda: NodejsFunction;
try {
functionLambda = new NodejsFunction(scope, `${id}-lambda`, {
const nodejsFunctionFunctionProps: CDKNodejsFunctionProps = {
entry: props.entry,
timeout: Duration.seconds(props.timeoutSeconds),
memorySize: props.memoryMB,
Expand All @@ -371,7 +387,21 @@ class AmplifyFunction
minify: true,
sourceMap: true,
},
});
};

// Copy all props to optionalProps to allow for deletion of keys
const optionalProps: Partial<FunctionPropsWithOptional> = { ...props };
// Remove hydrated props from optional props
for (const key of Object.keys(nodejsFunctionFunctionProps)) {
delete optionalProps[key as keyof FunctionPropsWithOptional];
}
// as CDKNodejsFunctionProps is necessary because of a type conflict with runtime
const functionProps = {
...optionalProps,
...nodejsFunctionFunctionProps,
} as CDKNodejsFunctionProps;

functionLambda = new NodejsFunction(scope, `${id}-lambda`, functionProps);
} catch (error) {
throw new AmplifyUserError(
'NodeJSFunctionConstructInitializationError',
Expand Down