Skip to content

Instantly share code, notes, and snippets.

@sam-goodwin
Last active November 17, 2021 09:11
Show Gist options
  • Save sam-goodwin/d7f1bd207897903de12f3477069663fe to your computer and use it in GitHub Desktop.
Save sam-goodwin/d7f1bd207897903de12f3477069663fe to your computer and use it in GitHub Desktop.
Lambda => SNS in punchcard
import cdk = require('@aws-cdk/core');
import { Function, Topic, string, integer, timestamp } from 'punchcard';
const app = new cdk.App();
export default app;
const stack = new cdk.Stack(app, 'my-stack');
// create a type-safe SNS Topic
const topic = new Topic(stack, 'Topic', {
type: struct({
key: string(),
count: integer(),
timestamp
})
});
new Function(stack, 'MyFunction', {
// depend on the topic
depends: topic,
handle: async (event, topic) => {
// our handle implementation is passed a type-safe pre-initialized 'topic' instance which can be interacted with
await topic.publish({
key: 'some key',
count: 1,
timestamp: new Date()
});
}
});
@DanielBoa
Copy link

I assume struct is also imported from 'punchcard'?

@sam-goodwin
Copy link
Author

sam-goodwin commented Nov 17, 2021

I assume struct is also imported from 'punchcard'?

That’s right, I think I made this gist for a medium blog a while back and it’s now out of date. It’s not called struct anymore, it’s called Type, and I changed it to use mix ins.

Better examples here https://github.com/punchcard/punchcard/blob/3f896487c004a4c6df667fdbdd4a74f63ed1715f/examples/src/hello-world.ts#L11

@DanielBoa
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment