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(stepfunctions-tasks): runtime language used to evaluate expressions is ignored #30302

Merged
merged 6 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ export abstract class HandlerFrameworkClass extends ClassType {
export: true,
});

const isEvalNodejsProvider = this.fqn.includes('eval-nodejs-provider');

this.importExternalModulesInto(scope);

const uuid: PropertySpec = {
Expand All @@ -149,11 +151,29 @@ export abstract class HandlerFrameworkClass extends ClassType {
},
},
};
const properties = [uuid, lambdaPurpose];
// eval nodejs provider is a one off scenario where the provider makes its runtime property configurable - to maintain this
// functionality we need to expose it as well
if (isEvalNodejsProvider) {
const runtime: PropertySpec = {
name: 'runtime',
type: LAMBDA_MODULE.Runtime,
immutable: true,
optional: true,
docs: {
summary: 'The runtime that this Lambda will use.',
docTags: {
default: 'lambda.Runtime.NODEJS_18_X',
},
},
};
properties.push(runtime);
}
const _interface = this.getOrCreateInterface(scope, {
name: `${this.name}Props`,
export: true,
extends: [LAMBDA_MODULE.FunctionOptions],
properties: [uuid, lambdaPurpose],
properties,
docs: {
summary: `Initialization properties for ${this.name}`,
},
Expand All @@ -163,7 +183,7 @@ export abstract class HandlerFrameworkClass extends ClassType {
new Splat(expr.ident('props')),
['code', expr.directCode(`lambda.Code.fromAsset(path.join(__dirname, '${props.codeDirectory}'))`)],
['handler', expr.lit(props.handler)],
['runtime', expr.directCode(toLambdaRuntime(props.runtime))],
['runtime', expr.directCode(`${isEvalNodejsProvider ? 'props.runtime ?? ' : ''}${toLambdaRuntime(props.runtime)}`)],
]);
this.buildConstructor({
constructorPropsType: _interface.type,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class LambdaModule extends ExternalModule {
public readonly Function = Type.fromName(this, 'Function');
public readonly SingletonFunction = Type.fromName(this, 'SingletonFunction');
public readonly FunctionOptions = Type.fromName(this, 'FunctionOptions');
public readonly Runtime = Type.fromName(this, 'Runtime');

public constructor() {
super('../../../aws-lambda');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* eslint-disable prettier/prettier,max-len */
import * as path from "path";
import { Construct } from "constructs";
import * as lambda from "../../../aws-lambda";

export class TestSingletonFunction extends lambda.SingletonFunction {
public constructor(scope: Construct, id: string, props: TestSingletonFunctionProps) {
super(scope, id, {
...props,
"code": lambda.Code.fromAsset(path.join(__dirname, 'my-handler')),
"handler": "index.handler",
"runtime": props.runtime ?? lambda.Runtime.NODEJS_18_X
});
}
}

/**
* Initialization properties for TestSingletonFunction
*/
export interface TestSingletonFunctionProps extends lambda.FunctionOptions {
/**
* A unique identifier to identify this Lambda.
*
* The identifier should be unique across all custom resource providers.
* We recommend generating a UUID per provider.
*/
readonly uuid: string;

/**
* A descriptive name for the purpose of this Lambda.
*
* If the Lambda does not have a physical name, this string will be
* reflected in its generated name. The combination of lambdaPurpose
* and uuid must be unique.
*
* @default SingletonLambda
*/
readonly lambdaPurpose?: string;

/**
* The runtime that this Lambda will use.
*
* @default lambda.Runtime.NODEJS_18_X
*/
readonly runtime?: lambda.Runtime;
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,23 @@ describe('framework', () => {
const expected = fs.readFileSync(path.resolve(__dirname, 'expected', 'custom-resource-provider-core.ts'), 'utf-8');
expect(result).toContain(expected);
});

test('codegen eval-nodejs-provider with exposed runtime property', () => {
// GIVEN
const module = new HandlerFrameworkModule('cdk-testing/eval-nodejs-provider');
const component: ComponentProps = {
type: ComponentType.SINGLETON_FUNCTION, // eval-nodejs-provider is a singleton function
sourceCode,
};
const outfile = calculateOutfile(sourceCode);
module.build(component, path.dirname(outfile).split('/').pop() ?? 'dist');

// WHEN
module.renderTo(`${tmpDir}/result.ts`);

// THEN
const result = fs.readFileSync(path.resolve(tmpDir, 'result.ts'), 'utf-8');
const expected = fs.readFileSync(path.resolve(__dirname, 'expected', 'singleton-function-eval-nodejs.ts'), 'utf-8');
expect(result).toContain(expected);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -119,5 +119,6 @@ function createEvalFn(runtime: lambda.Runtime | undefined, scope: Construct) {
return new EvalNodejsSingletonFunction(scope, 'EvalFunction', {
uuid,
lambdaPurpose,
runtime,
});
}
Loading