Skip to content

Commit

Permalink
feat(aws-lambda): multiple renames to utilize namespace semantics (#550)
Browse files Browse the repository at this point in the history
As we move to using namespace imports across the CDK, the naming we
chose for Lambda classes become cumbersome (i.e. `lambda.LambdaRuntime`).
This change renames all lambda classes to adhere to service semantics.

BREAKING CHANGE

* LambdaRef => lambda.FunctionRef
* Lambda => lambda.Function
* LambdaVersion => lambda.FunctionVersion
* LambdaRuntime => lambda.Runtime
* LambdaCode => lambda.Code (and all subclasses)
* InlineJavaScriptLambda => lambda.InlineJavaScriptFunction (soon to be deprecated)
* LambdaPermission => lambda.Permission
* SingletonLambda => SingletonFunction

Updated tests to use import-requires.

Intentionally avoided file renames to facilitate code review. Will do so
in a subsequent PR.

Fixes #430
  • Loading branch information
Elad Ben-Israel authored Aug 13, 2018
1 parent 147ea2a commit 488e65b
Show file tree
Hide file tree
Showing 35 changed files with 214 additions and 312 deletions.
4 changes: 2 additions & 2 deletions docs/src/aws-construct-lib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Least-Privilege IAM policies

IAM policies are automatically defined based on intent. For example, when
subscribing an AWS SNS :py:class:`Topic <@aws-cdk/aws-sns.Topic>` to a AWS Lambda
:py:class:`Function <@aws-cdk/aws-lambda.Lambda>`, the function's IAM permission
:py:class:`Function <@aws-cdk/aws-lambda.Function>`, the function's IAM permission
policy will automatically be modified to allow the specific topic to invoke the
function.

Expand Down Expand Up @@ -76,7 +76,7 @@ Many AWS resources emit AWS CloudWatch metrics as part of their normal operation
be used to setup :py:mod:`Alarms <@aws-cdk/aws-cloudwatch.Alarm>` or included in :py:mod:`Dashboards <@aws-cdk/aws-cloudwatch.Dashboard>`.

:py:mod:`Metric <@aws-cdk/aws-cloudwatch.Metric>` objects for AWS Constructs can be obtained
via ``metricXxx()`` methods. For example, the :py:meth:`metricDuration() <@aws-cdk/aws-lambda.LambdaRef.metricDuration>`
via ``metricXxx()`` methods. For example, the :py:meth:`metricDuration() <@aws-cdk/aws-lambda.FunctionRef.metricDuration>`
method reports the execution time of an AWS Lambda function.

For more information see the :doc:`refs/_aws-cdk_aws-cloudwatch` documentation.
Expand Down
6 changes: 3 additions & 3 deletions examples/cdk-examples-typescript/chat-app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ interface ChatAppFuncProps {
/*
* Extend Function as all of the Chat app functions have these common props.
*/
class ChatAppFunction extends lambda.Lambda {
class ChatAppFunction extends lambda.Function {
constructor(parent: cdk.Construct, name: string, props: ChatAppFuncProps) {
super(parent, name, {
code: new lambda.LambdaS3Code(props.bucket, props.zipFile),
runtime: lambda.LambdaRuntime.NodeJS610,
code: new lambda.S3Code(props.bucket, props.zipFile),
runtime: lambda.Runtime.NodeJS610,
handler: 'index.handler'
});
}
Expand Down
4 changes: 2 additions & 2 deletions packages/@aws-cdk/aws-cloudformation/lib/custom-resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ export interface CustomResourceProps {
/**
* The Lambda provider that implements this custom resource.
*
* We recommend using a SingletonLambda for this.
* We recommend using a lambda.SingletonFunction for this.
*
* Optional, exactly one of lamdaProvider or topicProvider must be set.
*/
lambdaProvider?: lambda.LambdaRef;
lambdaProvider?: lambda.FunctionRef;

/**
* The SNS Topic for the provider that implements this custom resource.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ class DemoResource extends cdk.Construct {
super(parent, name);

const resource = new cloudformation.CustomResource(this, 'Resource', {
lambdaProvider: new lambda.SingletonLambda(this, 'Singleton', {
lambdaProvider: new lambda.SingletonFunction(this, 'Singleton', {
uuid: 'f7d4f730-4ee1-11e8-9c2d-fa7ae01bbebc',
// This makes the demo only work as top-level TypeScript program, but that's fine for now
code: new lambda.LambdaInlineCode(fs.readFileSync('integ.trivial-lambda-provider.py', { encoding: 'utf-8' })),
code: new lambda.InlineCode(fs.readFileSync('integ.trivial-lambda-provider.py', { encoding: 'utf-8' })),
handler: 'index.main',
timeout: 300,
runtime: lambda.LambdaRuntime.Python27,
runtime: lambda.Runtime.Python27,
}),
properties: props
});
Expand Down
6 changes: 3 additions & 3 deletions packages/@aws-cdk/aws-cloudformation/test/test.resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ class TestCustomResource extends cdk.Construct {
constructor(parent: cdk.Construct, name: string) {
super(parent, name);

const singletonLambda = new lambda.SingletonLambda(this, 'Lambda', {
const singletonLambda = new lambda.SingletonFunction(this, 'Lambda', {
uuid: 'TestCustomResourceProvider',
code: new lambda.LambdaInlineCode('def hello(): pass'),
runtime: lambda.LambdaRuntime.Python27,
code: new lambda.InlineCode('def hello(): pass'),
runtime: lambda.Runtime.Python27,
handler: 'index.hello',
timeout: 300,
});
Expand Down
10 changes: 5 additions & 5 deletions packages/@aws-cdk/aws-cloudwatch/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ attributes. Resources that expose metrics will have functions that look
like `metricXxx()` which will return a Metric object, initialized with defaults
that make sense.

For example, `Lambda` objects have the `lambda.metricErrors()` method, which
For example, `lambda.Function` objects have the `fn.metricErrors()` method, which
represents the amount of errors reported by that Lambda function:

```ts
const errors = lambda.metricErrors();
const errors = fn.metricErrors();
```

### Aggregation
Expand All @@ -30,7 +30,7 @@ the function or the period), you can do so by passing additional parameters
to the metric function call:

```ts
const minuteErrorRate = lambda.metricErrors({
const minuteErrorRate = fn.metricErrors({
statistic: 'avg',
periodSec: 60,
label: 'Lambda failure rate'
Expand Down Expand Up @@ -66,7 +66,7 @@ object, passing the `Metric` object to set the alarm on:

```ts
new Alarm(this, 'Alarm', {
metric: lambda.metricErrors(),
metric: fn.metricErrors(),
threshold: 100,
evaluationPeriods: 2,
});
Expand All @@ -75,7 +75,7 @@ new Alarm(this, 'Alarm', {
Alternatively, you can call `metric.newAlarm()`:

```ts
lambda.metricErrors().newAlarm(this, 'Alarm', {
fn.metricErrors().newAlarm(this, 'Alarm', {
threshold: 100,
evaluationPeriods: 2,
});
Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-codebuild/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,5 @@ To define CloudWatch event rules for build projects, use one of the `onXxx` meth

```ts
const rule = project.onStateChange('BuildStateChange');
rule.addTarget(lambda);
rule.addTarget(lambdaFunction);
```
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export interface PipelineInvokeActionProps {
/**
* The lambda function to invoke.
*/
lambda: lambda.LambdaRef;
lambda: lambda.FunctionRef;

/**
* String to be used in the event data parameter passed to the Lambda
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ new codepipeline.AmazonS3Source(sourceStage, 'Source', {
bucketKey: 'key',
});

const lambdaFun = new lambda.Lambda(stack, 'LambdaFun', {
code: new lambda.LambdaInlineCode(`
const lambdaFun = new lambda.Function(stack, 'LambdaFun', {
code: new lambda.InlineCode(`
exports.handler = function () {
console.log("Hello, world!");
};
`),
handler: 'index.handler',
runtime: lambda.LambdaRuntime.NodeJS610,
runtime: lambda.Runtime.NodeJS610,
});
const lambdaStage = new codepipeline.Stage(pipeline, 'Lambda');
new lambda_codepipeline.PipelineInvokeAction(lambdaStage, 'Lambda', {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ export = {
'PipelineInvokeAction can be used to invoke lambda functions from a CodePipeline'(test: Test) {
const stack = new cdk.Stack();

const lambdaFun = new lambda.Lambda(stack, 'Function', {
code: new lambda.LambdaInlineCode('bla'),
const lambdaFun = new lambda.Function(stack, 'Function', {
code: new lambda.InlineCode('bla'),
handler: 'index.handler',
runtime: lambda.LambdaRuntime.NodeJS43,
runtime: lambda.Runtime.NodeJS43,
});

const pipeline = new codepipeline.Pipeline(stack, 'Pipeline');
Expand Down
20 changes: 11 additions & 9 deletions packages/@aws-cdk/aws-lambda/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
## AWS Lambda Construct Library

This construct library allows you to define AWS Lambda functions.
This construct library allows you to define AWS Lambda Functions.

```ts
const fn = new Lambda(this, 'MyFunction', {
runtime: LambdaRuntime.NodeJS810,
import lambda = require('@aws-cdk/aws-lambda');

const fn = new lambda.Function(this, 'MyFunction', {
runtime: lambda.Runtime.NodeJS810,
handler: 'index.handler'
code: LambdaCode.inline('exports.handler = function(event, ctx, cb) { return cb(null, "hi"); }'),
code: lambda.Code.inline('exports.handler = function(event, ctx, cb) { return cb(null, "hi"); }'),
});
```

### Handler Code

The `LambdaCode` class includes static convenience methods for various types of
The `lambda.Code` class includes static convenience methods for various types of
runtime code.

* `LambdaCode.bucket(bucket, key[, objectVersion])` - specify an S3 object that
* `lambda.Code.bucket(bucket, key[, objectVersion])` - specify an S3 object that
contains the archive of your runtime code.
* `LambdaCode.inline(code)` - inline the handle code as a string. This is
* `lambda.Code.inline(code)` - inline the handle code as a string. This is
limited to 4KB. The class `InlineJavaScriptLambda` can be used to simplify
inlining JavaScript functions.
* `LambdaCode.directory(directory)` - specify a directory in the local filesystem
* `lambda.Code.directory(directory)` - specify a directory in the local filesystem
which will be zipped and uploaded to S3 before deployment.
* `LambdaCode.file(path)` - specify a file to be used for Lambda code. This can
* `lambda.Code.file(path)` - specify a file to be used for Lambda code. This can
be, for example a JAR or a ZIP file, based on the runtime used.

The following example shows how to define a Python function and deploy the code from the
Expand Down
16 changes: 8 additions & 8 deletions packages/@aws-cdk/aws-lambda/lib/alias.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import iam = require('@aws-cdk/aws-iam');
import cdk = require('@aws-cdk/cdk');
import { FunctionName, LambdaRef } from './lambda-ref';
import { LambdaVersion } from './lambda-version';
import { FunctionName, FunctionRef } from './lambda-ref';
import { FunctionVersion } from './lambda-version';
import { cloudformation, FunctionArn } from './lambda.generated';
import { LambdaPermission } from './permission';
import { Permission } from './permission';

/**
* Properties for a new Lambda alias
Expand All @@ -21,7 +21,7 @@ export interface AliasProps {
*
* Use lambda.addVersion() to obtain a new lambda version to refer to.
*/
version: LambdaVersion;
version: FunctionVersion;

/**
* Name of this alias
Expand Down Expand Up @@ -51,7 +51,7 @@ export interface AliasProps {
/**
* A new alias to a particular version of a Lambda function.
*/
export class Alias extends LambdaRef {
export class Alias extends FunctionRef {
/**
* ARN of this alias
*
Expand All @@ -78,7 +78,7 @@ export class Alias extends LambdaRef {
/**
* The actual Lambda function object that this Alias is pointing to
*/
private readonly underlyingLambda: LambdaRef;
private readonly underlyingLambda: FunctionRef;

constructor(parent: cdk.Construct, name: string, props: AliasProps) {
super(parent, name);
Expand All @@ -97,7 +97,7 @@ export class Alias extends LambdaRef {
this.functionArn = alias.ref;
}

public addPermission(name: string, permission: LambdaPermission) {
public addPermission(name: string, permission: Permission) {
// Forward addPermission() to the underlying Lambda object
this.underlyingLambda.addPermission(name, permission);
}
Expand Down Expand Up @@ -146,7 +146,7 @@ export interface VersionWeight {
/**
* The version to route traffic to
*/
readonly version: LambdaVersion;
readonly version: FunctionVersion;

/**
* How much weight to assign to this version (0..1)
Expand Down
24 changes: 12 additions & 12 deletions packages/@aws-cdk/aws-lambda/lib/code.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import assets = require('@aws-cdk/assets');
import s3 = require('@aws-cdk/aws-s3');
import { Lambda } from './lambda';
import { Function as Func } from './lambda';
import { cloudformation } from './lambda.generated';

export abstract class LambdaCode {
export abstract class Code {
/**
* @returns `LambdaS3Code` associated with the specified S3 object.
* @param bucket The S3 bucket
* @param key The object key
* @param objectVersion Optional S3 object version
*/
public static bucket(bucket: s3.BucketRef, key: string, objectVersion?: string) {
return new LambdaS3Code(bucket, key, objectVersion);
return new S3Code(bucket, key, objectVersion);
}

/**
* @returns `LambdaInlineCode` with inline code.
* @param code The actual handler code (limited to 4KiB)
*/
public static inline(code: string) {
return new LambdaInlineCode(code);
return new InlineCode(code);
}

/**
Expand All @@ -28,15 +28,15 @@ export abstract class LambdaCode {
* @param directoryToZip The directory to zip
*/
public static directory(directoryToZip: string) {
return new LambdaAssetCode(directoryToZip, assets.AssetPackaging.ZipDirectory);
return new AssetCode(directoryToZip, assets.AssetPackaging.ZipDirectory);
}

/**
* @returns Uses a file on disk as a lambda handler's code.
* @param filePath The file path
*/
public static file(filePath: string) {
return new LambdaAssetCode(filePath, assets.AssetPackaging.File);
return new AssetCode(filePath, assets.AssetPackaging.File);
}

/**
Expand All @@ -49,15 +49,15 @@ export abstract class LambdaCode {
* Called when the lambda is initialized to allow this object to
* bind to the stack, add resources and have fun.
*/
public bind(_lambda: Lambda) {
public bind(_lambda: Func) {
return;
}
}

/**
* Lambda code from an S3 archive.
*/
export class LambdaS3Code extends LambdaCode {
export class S3Code extends Code {
private bucketName: s3.BucketName;

constructor(bucket: s3.BucketRef, private key: string, private objectVersion?: string) {
Expand All @@ -82,7 +82,7 @@ export class LambdaS3Code extends LambdaCode {
/**
* Lambda code from an inline string (limited to 4KiB).
*/
export class LambdaInlineCode extends LambdaCode {
export class InlineCode extends Code {
constructor(private code: string) {
super();

Expand All @@ -91,7 +91,7 @@ export class LambdaInlineCode extends LambdaCode {
}
}

public bind(lambda: Lambda) {
public bind(lambda: Func) {
if (!lambda.runtime.supportsInlineCode) {
throw new Error(`Inline source not allowed for ${lambda.runtime.name}`);
}
Expand All @@ -107,7 +107,7 @@ export class LambdaInlineCode extends LambdaCode {
/**
* Lambda code from a local directory.
*/
export class LambdaAssetCode extends LambdaCode {
export class AssetCode extends Code {
private asset?: assets.Asset;

/**
Expand All @@ -120,7 +120,7 @@ export class LambdaAssetCode extends LambdaCode {
super();
}

public bind(lambda: Lambda) {
public bind(lambda: Func) {
this.asset = new assets.Asset(lambda, 'Code', {
path: this.path,
packaging: this.packaging
Expand Down
Loading

0 comments on commit 488e65b

Please sign in to comment.