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

aws-cdk-kinesis: Implement Kinesis Stream L2 construct. #86

Merged
merged 4 commits into from
Jun 18, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions packages/aws-cdk-all/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"aws-cdk-ec2": "^0.6.0",
"aws-cdk-events": "^0.6.0",
"aws-cdk-iam": "^0.6.0",
"aws-cdk-kinesis": "^0.6.0",
"aws-cdk-kms": "^0.6.0",
"aws-cdk-lambda": "^0.6.0",
"aws-cdk-neptune": "^0.6.0",
Expand Down
6 changes: 6 additions & 0 deletions packages/aws-cdk-kinesis/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*.js
*.js.map
*.d.ts
node_modules
dist
tsconfig.json
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add tslint.json (new)

6 changes: 6 additions & 0 deletions packages/aws-cdk-kinesis/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Don't include original .ts files when doing `npm pack`
*.ts
!*.d.ts
coverage
.nyc_output
*.tgz
101 changes: 101 additions & 0 deletions packages/aws-cdk-kinesis/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
## AWS Kinesis Construct Library

Define an unencrypted Kinesis stream.

```ts
new Stream(this, 'MyFirstStream');
```

### Encryption

Define a KMS-encrypted stream:

```ts
const stream = newStream(this, 'MyEncryptedStream', {
encryption: StreamEncryption.Kms
});

// you can access the encryption key:
assert(stream.encryptionKey instanceof kms.EncryptionKey);
```

You can also supply your own key:

```ts
const myKmsKey = new kms.EncryptionKey(this, 'MyKey');

const stream = new Stream(this, 'MyEncryptedStream', {
encryption: StreamEncryption.Kms,
encryptionKey: myKmsKey
});

assert(stream.encryptionKey === myKmsKey);
```

### Importing and Exporting Streams
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be redundant to describe this idiom in every README.

@Doug-AWS can you create a doc topic on "importing and exporting constructs across stacks" and have that content there?


You can create a `Stream` construct that represents an external/existing/unowned stream by using the `Stream.import` factory method.

This method accepts an object that adheres to `StreamRef`.

This means that you can define a `StreamRef` using token literals:

```ts
const stream = Stream.import(this, {
streamArn: new StreamArn('arn:aws:kinesis:us-east-1:123456789012:stream/example-stream-name')
});

// now you can just call methods on the stream
stream.grantReadWrite(user);
```

The `stream.export()` method can be used to "export" the stream from the current stack. It returns a `StreamRef` object that can later be used in a call to `Stream.import` in another stack.

Here's an example.

Let's define a stack with a Kinesis stream and export it using `stream.export()`.

```ts
class Producer extends Stack {
public readonly myStreamRef: StreamRef;

constructor(parent: App, name: string) {
super(parent, name);

const stream = new Stream(this, 'MyStream');
this.myStreamRef = stream.export();
}
}
```

Now let's define a stack that requires a StreamRef as an input and uses `Stream.import` to create a `Stream` object that represents this external stream. Grant a user principal created within this consuming stack read/write permissions to the stream.

```ts
interface ConsumerProps {
public userStreamRef: StreamRef;
}

class Consumer extends Stack {
constructor(parent: App, name: string, props: ConsumerProps) {
super(parent, name);

const user = new User(this, 'MyUser');
const userStream = Stream.import(this, props.userStreamRef);
userStream.grantReadWrite(user);
}
}
```

Now, let's define our CDK app to bind these together:

```ts
const app = new App(process.argv);

const producer = new Producer(app, 'produce');

new Consumer(app, 'consume', {
userStreamRef: producer.myStreamRef
});

process.stdout.write(app.run());
```
1 change: 1 addition & 0 deletions packages/aws-cdk-kinesis/lib/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './stream';
Loading