Skip to content

Commit

Permalink
Add support for Node8.10 Lambda runtime (#187)
Browse files Browse the repository at this point in the history
Also, re-model LambdaRuntime as a class to allow customers to use
runtimes that are not yet part of the modelled list, optionally with
support for inline code.

The `InlinableLambdaRuntime` class is mostly provided to allow for a
strictly typed interface in `InlineJavaScriptLambda`.

Fixes #188
Fixes #203
  • Loading branch information
RomainMuller committed Jul 2, 2018
1 parent 507f778 commit f7a5c3b
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 35 deletions.
12 changes: 2 additions & 10 deletions packages/@aws-cdk/lambda/lib/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,8 @@ export class LambdaInlineCode extends LambdaCode {
}

public toJSON(runtime: LambdaRuntime): lambda.FunctionResource.CodeProperty {
const allowed = [
LambdaRuntime.NodeJS,
LambdaRuntime.NodeJS43,
LambdaRuntime.NodeJS610,
LambdaRuntime.Python27,
LambdaRuntime.Python36
];

if (!allowed.find(a => a === runtime)) {
throw new Error(`Inline source only allowed for: ${allowed.join(', ')}`);
if (!runtime.supportsInlineCode) {
throw new Error(`Inline source not supported for: ${runtime.name}`);
}

return {
Expand Down
17 changes: 4 additions & 13 deletions packages/@aws-cdk/lambda/lib/inline.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Construct } from '@aws-cdk/core';
import { LambdaInlineCode } from './code';
import { Lambda } from './lambda';
import { LambdaRuntime } from './runtime';
import { InlinableJavascriptLambdaRuntime, LambdaRuntime } from './runtime';

/**
* Defines the handler code for an inline JavaScript lambda function.
Expand Down Expand Up @@ -30,15 +30,6 @@ export interface IJavaScriptLambdaHandler {
fn(event: any, context: any, callback: any): void;
}

/**
* The set of runtime that support inline javascript code.
*/
export enum InlineJavaScriptLambdaRuntime {
NodeJS = LambdaRuntime.NodeJS,
NodeJS43 = LambdaRuntime.NodeJS43,
NodeJS610 = LambdaRuntime.NodeJS610,
}

export interface InlineJavaScriptLambdaProps {
/**
* The lambda handler as a javascript function.
Expand Down Expand Up @@ -75,9 +66,9 @@ export interface InlineJavaScriptLambdaProps {
* For valid values, see the Runtime property in the AWS Lambda Developer
* Guide.
*
* @default NodeJS610
* @default NodeJS810
*/
runtime?: InlineJavaScriptLambdaRuntime;
runtime?: InlinableJavascriptLambdaRuntime;

/**
* A name for the function. If you don't specify a name, AWS CloudFormation
Expand Down Expand Up @@ -117,7 +108,7 @@ export interface InlineJavaScriptLambdaProps {
export class InlineJavaScriptLambda extends Lambda {
constructor(parent: Construct, name: string, props: InlineJavaScriptLambdaProps) {
const code = new LambdaInlineCode(renderCode(props.handler));
const runtime = (props.runtime || InlineJavaScriptLambdaRuntime.NodeJS610) as any;
const runtime: InlinableJavascriptLambdaRuntime = props.runtime || LambdaRuntime.NodeJS610;
const handler = 'index.handler';
const timeout = props.timeout || 30;
super(parent, name, {
Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/lambda/lib/lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export class Lambda extends LambdaRef {
code: props.code.toJSON(props.runtime),
handler: props.handler,
timeout: props.timeout,
runtime: props.runtime,
runtime: props.runtime.name,
role: this.role.roleArn,
environment: new Token(() => this.renderEnvironment()),
memorySize: props.memorySize,
Expand Down
64 changes: 53 additions & 11 deletions packages/@aws-cdk/lambda/lib/runtime.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,57 @@
export interface LambdaRuntimeProps {
/**
* Whether the ``ZipFile`` (aka inline code) property can be used with this runtime.
* @default false
*/
readonly supportsInlineCode?: boolean;
}

/**
* Lambda function runtime environment.
*/
export enum LambdaRuntime {
NodeJS = 'nodejs',
NodeJS43 = 'nodejs4.3',
NodeJS43Edge = 'nodejs4.3-edge',
NodeJS610 = 'nodejs6.10',
Java8 = 'java8',
Python27 = 'python2.7',
Python36 = 'python3.6',
DotNetCore1 = 'dotnetcore1.0',
DotNetCore2 = 'dotnetcore2.0',
Go1x = 'go1.x'
export class LambdaRuntime {
public static readonly NodeJS = new LambdaRuntime('nodejs', { supportsInlineCode: true }) as InlinableJavascriptLambdaRuntime;
// Using ``as InlinableLambdaRuntime`` because that calss cannot be defined just yet
public static readonly NodeJS43 = new LambdaRuntime('nodejs4.3', { supportsInlineCode: true }) as InlinableJavascriptLambdaRuntime;
public static readonly NodeJS43Edge = new LambdaRuntime('nodejs4.3-edge');
// Using ``as InlinableLambdaRuntime`` because that calss cannot be defined just yet
public static readonly NodeJS610 = new LambdaRuntime('nodejs6.10', { supportsInlineCode: true }) as InlinableJavascriptLambdaRuntime;
public static readonly NodeJS810 = new LambdaRuntime('nodejs8.10');
public static readonly Java8 = new LambdaRuntime('java8');
// Using ``as InlinableLambdaRuntime`` because that calss cannot be defined just yet
public static readonly Python27 = new LambdaRuntime('python2.7', { supportsInlineCode: true }) as InlinableLambdaRuntime;
// Using ``as InlinableLambdaRuntime`` because that calss cannot be defined just yet
public static readonly Python36 = new LambdaRuntime('python3.6', { supportsInlineCode: true }) as InlinableLambdaRuntime;
public static readonly DotNetCore1 = new LambdaRuntime('dotnetcore1.0');
public static readonly DotNetCore2 = new LambdaRuntime('dotnetcore2.0');
public static readonly Go1x = new LambdaRuntime('go1.x');

/** The name of this runtime, as expected by the Lambda resource. */
public readonly name: string;
/** Whether the ``ZipFile`` (aka inline code) property can be used with this runtime. */
public readonly supportsInlineCode: boolean;

constructor(name: string, props: LambdaRuntimeProps = {}) {
this.name = name;
this.supportsInlineCode = !!props.supportsInlineCode;
}

public toString(): string {
return this.name;
}
}

/**
* A ``LambdaRuntime`` that can be used in conjunction with the ``ZipFile``
* property of the ``AWS::Lambda::Function`` resource.
*/
export interface InlinableLambdaRuntime {
readonly name: string;
readonly supportsInlineCode: true;
}

/**
* A ``LambdaRuntime`` that can be used for inlining JavaScript.
*/
// tslint:disable-next-line:no-empty-interface this is a marker to allow type-safe declarations
export interface InlinableJavascriptLambdaRuntime extends InlinableLambdaRuntime {}

0 comments on commit f7a5c3b

Please sign in to comment.