Creating Functions

Creating a Lambda.Function is super simple - just instantiate it and implement handle:

new Lambda.Function(stack, 'MyFunction', {},
  async() => console.log('Hello, World!')

It supports all the same properties as the AWS CDK’s @aws-cdk/aws-lambda.Function, so you can configure things such as memory:

new Lambda.Function(stack, 'MyFunction', {
  memorySize: 512
}, async() => console.log('Hello, World!'),

Request and Response Shape

By default, the type of the request and response is any, but you can also explicitly define the types:

new Lambda.Function<string, number>(stack, 'MyFunction', {}, 
  async(str) => str.length

These types will be serialized to and from JSON based on their JS types (using JSON.stringify). To explicitly control the serialization, explicitly provide a Shape for the request and response properties.

// Lambda.Function<string, number>
new Lambda.Function(stack, 'MyFunction', {
  request: string,
  response: integer,
}, async(str) => str.length);

Now, the Punchcard framework will validate and serialzie the request and response according to their “Shape”. (See Part 4 - Shapes: Type-Safe Schemas).

Scheduled Functions

You can schedule a new Lambda.Function to do some work:

Lambda.schedule(stack, 'MyFunction', {
  schedule: Schedule.rate(Duration.minutes(1)),
}, async(request: CloudWatch.Event) => console.log('Hello, World!'));

Note: how the the type of request is a CloudWatch.Event, as it is regularly triggered by a scheduled CloudWatch Event


Next: we’ll explore how to interact with other Constructs from your Function by declaring Runtime Dependencies