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

chore(lambda): enable 'strict' mode on lambda modules #17477

Merged
merged 11 commits into from
Nov 12, 2021
69 changes: 35 additions & 34 deletions packages/@aws-cdk/aws-lambda-destinations/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,17 @@ The following destinations are supported
Example with a SNS topic for successful invocations:

```ts
import * as lambda from '@aws-cdk/aws-lambda';
import * as destinations from '@aws-cdk/aws-lambda-destinations';
// An sns topic for successful invocations of a lambda function
import * as sns from '@aws-cdk/aws-sns';

const myTopic = new sns.Topic(this, 'Topic');

const myFn = new lambda.Function(this, 'Fn', {
// other props
onSuccess: new destinations.SnsDestination(myTopic)
runtime: lambda.Runtime.NODEJS_12_X,
handler: 'index.handler',
code: lambda.Code.fromAsset(path.join(__dirname, 'lambda-handler')),
// sns topic for successful invocations
onSuccess: new destinations.SnsDestination(myTopic),
})
```

Expand Down Expand Up @@ -71,27 +73,27 @@ In case of failure, the record contains the reason and error object:

```json
{
"version": "1.0",
"timestamp": "2019-11-24T21:52:47.333Z",
"requestContext": {
"requestId": "8ea123e4-1db7-4aca-ad10-d9ca1234c1fd",
"functionArn": "arn:aws:lambda:sa-east-1:123456678912:function:event-destinations:$LATEST",
"condition": "RetriesExhausted",
"approximateInvokeCount": 3
},
"requestPayload": {
"Success": false
},
"responseContext": {
"statusCode": 200,
"executedVersion": "$LATEST",
"functionError": "Handled"
},
"responsePayload": {
"errorMessage": "Failure from event, Success = false, I am failing!",
"errorType": "Error",
"stackTrace": [ "exports.handler (/var/task/index.js:18:18)" ]
}
"version": "1.0",
"timestamp": "2019-11-24T21:52:47.333Z",
"requestContext": {
"requestId": "8ea123e4-1db7-4aca-ad10-d9ca1234c1fd",
"functionArn": "arn:aws:lambda:sa-east-1:123456678912:function:event-destinations:$LATEST",
"condition": "RetriesExhausted",
"approximateInvokeCount": 3
},
"requestPayload": {
"Success": false
},
"responseContext": {
"statusCode": 200,
"executedVersion": "$LATEST",
"functionError": "Handled"
},
"responsePayload": {
"errorMessage": "Failure from event, Success = false, I am failing!",
"errorType": "Error",
"stackTrace": [ "exports.handler (/var/task/index.js:18:18)" ]
}
}
```

Expand All @@ -112,18 +114,17 @@ The `responseOnly` option of `LambdaDestination` allows to auto-extract the resp
invocation record:

```ts
import * as lambda from '@aws-cdk/aws-lambda';
import * as destinations from '@aws-cdk/aws-lambda-destinations';

const destinationFn = new lambda.Function(this, 'Destination', {
// props
});
// Auto-extract response payload with a lambda destination
declare const destinationFn: lambda.Function;

const sourceFn = new lambda.Function(this, 'Source', {
// other props
runtime: lambda.Runtime.NODEJS_12_X,
handler: 'index.handler',
code: lambda.Code.fromAsset(path.join(__dirname, 'lambda-handler')),
// auto-extract on success
onSuccess: new destinations.LambdaDestination(destinationFn, {
responseOnly: true // auto-extract
});
responseOnly: true,
}),
})
```

Expand Down
9 changes: 8 additions & 1 deletion packages/@aws-cdk/aws-lambda-destinations/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@
]
}
},
"projectReferences": true
"projectReferences": true,
"metadata": {
"jsii": {
"rosetta": {
"strict": true
}
}
}
},
"repository": {
"type": "git",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Fixture with packages imported, but nothing else
import { Construct } from 'constructs';
import { Stack } from '@aws-cdk/core';
import * as lambda from '@aws-cdk/aws-lambda';
import * as destinations from '@aws-cdk/aws-lambda-destinations';
import * as path from 'path';

class Fixture extends Stack {
constructor(scope: Construct, id: string) {
super(scope, id);

/// here
}
}
73 changes: 35 additions & 38 deletions packages/@aws-cdk/aws-lambda-event-sources/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ sources regardless of the underlying mechanism they use.
The following code sets up a lambda function with an SQS queue event source -

```ts
const fn = new lambda.Function(this, 'MyFunction', { /* ... */ });
import { SqsEventSource } from '@aws-cdk/aws-lambda-event-sources';

declare const fn: lambda.Function;
const queue = new sqs.Queue(this, 'MyQueue');
const eventSource = fn.addEventSource(new SqsEventSource(queue));
const eventSource = new SqsEventSource(queue);
fn.addEventSource(eventSource);

const eventSourceId = eventSource.eventSourceId;
const eventSourceId = eventSource.eventSourceMappingId;
```

The `eventSourceId` property contains the event source id. This will be a
Expand Down Expand Up @@ -58,16 +60,15 @@ behavior:
* __enabled__: If the SQS event source mapping should be enabled. The default is true.

```ts
import * as sqs from '@aws-cdk/aws-sqs';
import { SqsEventSource } from '@aws-cdk/aws-lambda-event-sources';
import { Duration } from '@aws-cdk/core';

const queue = new sqs.Queue(this, 'MyQueue', {
visibilityTimeout: Duration.seconds(30) // default,
receiveMessageWaitTime: Duration.seconds(20) // default
visibilityTimeout: Duration.seconds(30), // default,
receiveMessageWaitTime: Duration.seconds(20), // default
});
declare const fn: lambda.Function;

lambda.addEventSource(new SqsEventSource(queue, {
fn.addEventSource(new SqsEventSource(queue, {
batchSize: 10, // default
maxBatchingWindow: Duration.minutes(5),
}));
Expand All @@ -88,11 +89,12 @@ Amazon S3 to publish and which Lambda function to invoke.
import * as s3 from '@aws-cdk/aws-s3';
import { S3EventSource } from '@aws-cdk/aws-lambda-event-sources';

const bucket = new s3.Bucket(...);
const bucket = new s3.Bucket(this, 'mybucket');
declare const fn: lambda.Function;

lambda.addEventSource(new S3EventSource(bucket, {
fn.addEventSource(new S3EventSource(bucket, {
events: [ s3.EventType.OBJECT_CREATED, s3.EventType.OBJECT_REMOVED ],
filters: [ { prefix: 'subdir/' } ] // optional
filters: [ { prefix: 'subdir/' } ], // optional
}));
```

Expand All @@ -118,12 +120,13 @@ Accounts](https://docs.aws.amazon.com/lambda/latest/dg/with-sns.html).
import * as sns from '@aws-cdk/aws-sns';
import { SnsEventSource } from '@aws-cdk/aws-lambda-event-sources';

const topic = new sns.Topic(...);
declare const topic: sns.Topic;
const deadLetterQueue = new sqs.Queue(this, 'deadLetterQueue');

lambda.addEventSource(new SnsEventSource(topic, {
filterPolicy: { ... },
deadLetterQueue: deadLetterQueue
declare const fn: lambda.Function;
fn.addEventSource(new SnsEventSource(topic, {
filterPolicy: { },
deadLetterQueue: deadLetterQueue,
}));
```

Expand Down Expand Up @@ -157,24 +160,19 @@ and add it to your Lambda function. The following parameters will impact Amazon

```ts
import * as dynamodb from '@aws-cdk/aws-dynamodb';
import * as lambda from '@aws-cdk/aws-lambda';
import * as sqs from '@aws-cdk/aws-sqs';
import { DynamoEventSource, SqsDlq } from '@aws-cdk/aws-lambda-event-sources';

const table = new dynamodb.Table(..., {
partitionKey: ...,
stream: dynamodb.StreamViewType.NEW_IMAGE // make sure stream is configured
});
declare const table: dynamodb.Table;

const deadLetterQueue = new sqs.Queue(this, 'deadLetterQueue');

const function = new lambda.Function(...);
function.addEventSource(new DynamoEventSource(table, {
declare const fn: lambda.Function;
fn.addEventSource(new DynamoEventSource(table, {
startingPosition: lambda.StartingPosition.TRIM_HORIZON,
batchSize: 5,
bisectBatchOnError: true,
onFailure: new SqsDlq(deadLetterQueue),
retryAttempts: 10
retryAttempts: 10,
}));
```

Expand Down Expand Up @@ -202,15 +200,15 @@ behavior:
* __enabled__: If the DynamoDB Streams event source mapping should be enabled. The default is true.

```ts
import * as lambda from '@aws-cdk/aws-lambda';
import * as kinesis from '@aws-cdk/aws-kinesis';
import { KinesisEventSource } from '@aws-cdk/aws-lambda-event-sources';

const stream = new kinesis.Stream(this, 'MyStream');

declare const myFunction: lambda.Function;
myFunction.addEventSource(new KinesisEventSource(stream, {
batchSize: 100, // default
startingPosition: lambda.StartingPosition.TRIM_HORIZON
startingPosition: lambda.StartingPosition.TRIM_HORIZON,
}));
```

Expand All @@ -222,53 +220,52 @@ The following code sets up Amazon MSK as an event source for a lambda function.
MSK cluster, as described in [Username/Password authentication](https://docs.aws.amazon.com/msk/latest/developerguide/msk-password.html).

```ts
import * as lambda from '@aws-cdk/aws-lambda';
import * as msk from '@aws-cdk/aws-lambda';
import { Secret } from '@aws-cdk/aws-secretmanager';
import { Secret } from '@aws-cdk/aws-secretsmanager';
import { ManagedKafkaEventSource } from '@aws-cdk/aws-lambda-event-sources';

// Your MSK cluster arn
const cluster = 'arn:aws:kafka:us-east-1:0123456789019:cluster/SalesCluster/abcd1234-abcd-cafe-abab-9876543210ab-4';
const clusterArn = 'arn:aws:kafka:us-east-1:0123456789019:cluster/SalesCluster/abcd1234-abcd-cafe-abab-9876543210ab-4';

// The Kafka topic you want to subscribe to
const topic = 'some-cool-topic'
const topic = 'some-cool-topic';

// The secret that allows access to your MSK cluster
// You still have to make sure that it is associated with your cluster as described in the documentation
const secret = new Secret(this, 'Secret', { secretName: 'AmazonMSK_KafkaSecret' });

declare const myFunction: lambda.Function;
myFunction.addEventSource(new ManagedKafkaEventSource({
clusterArn,
topic: topic,
secret: secret,
batchSize: 100, // default
startingPosition: lambda.StartingPosition.TRIM_HORIZON
startingPosition: lambda.StartingPosition.TRIM_HORIZON,
}));
```

The following code sets up a self managed Kafka cluster as an event source. Username and password based authentication
will need to be set up as described in [Managing access and permissions](https://docs.aws.amazon.com/lambda/latest/dg/smaa-permissions.html#smaa-permissions-add-secret).

```ts
import * as lambda from '@aws-cdk/aws-lambda';
import { Secret } from '@aws-cdk/aws-secretmanager';
import { Secret } from '@aws-cdk/aws-secretsmanager';
import { SelfManagedKafkaEventSource } from '@aws-cdk/aws-lambda-event-sources';

// The list of Kafka brokers
const bootstrapServers = ['kafka-broker:9092']
const bootstrapServers = ['kafka-broker:9092'];

// The Kafka topic you want to subscribe to
const topic = 'some-cool-topic'
const topic = 'some-cool-topic';

// The secret that allows access to your self hosted Kafka cluster
const secret = new Secret(this, 'Secret', { ... });
declare const secret: Secret;

declare const myFunction: lambda.Function;
myFunction.addEventSource(new SelfManagedKafkaEventSource({
bootstrapServers: bootstrapServers,
topic: topic,
secret: secret,
batchSize: 100, // default
startingPosition: lambda.StartingPosition.TRIM_HORIZON
startingPosition: lambda.StartingPosition.TRIM_HORIZON,
}));
```

Expand Down
9 changes: 8 additions & 1 deletion packages/@aws-cdk/aws-lambda-event-sources/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@
]
}
},
"projectReferences": true
"projectReferences": true,
"metadata": {
"jsii": {
"rosetta": {
"strict": true
}
}
}
},
"repository": {
"type": "git",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Fixture with packages imported, but nothing else
import { Construct } from 'constructs';
import { Duration, Stack } from '@aws-cdk/core';
import * as lambda from '@aws-cdk/aws-lambda';
import * as sqs from '@aws-cdk/aws-sqs';

class Fixture extends Stack {
constructor(scope: Construct, id: string) {
super(scope, id);

/// here
}
}
8 changes: 5 additions & 3 deletions packages/@aws-cdk/aws-lambda-go/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Define a `GoFunction`:

```ts
new lambda.GoFunction(this, 'handler', {
entry: 'app/cmd/api'
entry: 'app/cmd/api',
});
```

Expand Down Expand Up @@ -154,7 +154,7 @@ Use the `bundling.dockerImage` prop to use a custom bundling image:
new lambda.GoFunction(this, 'handler', {
entry: 'app/cmd/api',
bundling: {
dockerImage: cdk.DockerImage.fromBuild('/path/to/Dockerfile'),
dockerImage: DockerImage.fromBuild('/path/to/Dockerfile'),
},
});
```
Expand All @@ -174,7 +174,9 @@ new lambda.GoFunction(this, 'handler', {

It is possible to run additional commands by specifying the `commandHooks` prop:

```ts
```text
// This example only available in TypeScript
// Run additional commands on a GoFunction via `commandHooks` property
new lambda.GoFunction(this, 'handler', {
bundling: {
commandHooks: {
Expand Down
3 changes: 2 additions & 1 deletion packages/@aws-cdk/aws-lambda-go/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,15 @@ export interface BundlingOptions {
*
* Commands are chained with `&&`.
*
* @example
* ```text
* {
* // Run tests prior to bundling
* beforeBundling(inputDir: string, outputDir: string): string[] {
* return [`go test -mod=vendor ./...`];
* }
* // ...
* }
* ```
*/
export interface ICommandHooks {
/**
Expand Down
Loading