Front-End Web & Mobile

New features for Amplify Functions: Scheduling and Log Streaming

AWS Amplify is excited to announce two new features to its functions offering: scheduling support with cron and natural language, and streaming execution logs. Amplify enables developers to author serverless functions using TypeScript and iterate fast by deploying business logic in seconds. To learn more about Amplify Functions, visit the AWS Amplify documentation for Functions.

Streaming Function Logs

With Amplify’s personal cloud sandboxes, developers are presented with a development environment they can use to design, build, and iterate on their application’s backend using live resources. To further decrease iteration cycles, Amplify now enables developers to stream Function execution logs directly to their terminal, granting insights to function execution without leaving the local development environment.

To get started, opt in to streaming all Function logs by specifying the --stream-function-logs option:

npx ampx sandbox --stream-function-logs

For example, if you have a collection of functions attached to your authentication resource as Amazon Cognito Lambda triggers, you can start your frontend framework’s development server, step through your authentication flow, and inspect logs from each function’s invocation — all without navigating to the AWS Management Console.

If, for instance, you have many functions and are only interested in debugging pieces of your backend’s functionality you can opt to filter the log output based on the function’s name by specifying a --logs-filter :

npx ampx sandbox --stream-function-logs --logs-filter auth

Log filters allow you to filter based on function names. Using the command example above, the convention of your trigger’s resource names would be as follows:

// amplify/auth/post-confirmation/resource.ts
import { defineFunction } from "@aws-amplify/backend"

export const postConfirmation = defineFunction({
  name: "auth-post-confirmation",
})

However, for complex filters, the --logs-filter option accepts regular expressions. Using the same example as above, let’s say you want to filter logs for only function names beginning with “auth”:

npx ampx sandbox --stream-function-logs --logs-filter "^auth"

The sandbox process will only print the logs for functions that match the corresponding regular expression.

[Sandbox] Watching for file changes...
File written: amplify_outputs.json
[auth-pre-sign-up] 3:36:34 PM INIT_START Runtime Version: nodejs:18.v30    Runtime Version ARN: arn:aws:lambda:us-east-1::runtime:f89c264158db39a1cfcbb5f9b3741413df1cfce4d550c9a475a67d923e19e2f4
[auth-pre-sign-up] 3:36:34 PM START RequestId: 685be2bd-5df1-4dd5-9eb1-24f5f6337f91 Version: $LATEST
[auth-pre-sign-up] 3:36:34 PM END RequestId: 685be2bd-5df1-4dd5-9eb1-24f5f6337f91
[auth-pre-sign-up] 3:36:34 PM REPORT RequestId: 685be2bd-5df1-4dd5-9eb1-24f5f6337f91    Duration: 4.12 ms    Billed Duration: 5 ms    Memory Size: 512 MB    Max Memory Used: 67 MB    Init Duration: 173.67 ms
[auth-post-confirmation] 3:38:40 PM INIT_START Runtime Version: nodejs:18.v30    Runtime Version ARN: arn:aws:lambda:us-east-1::runtime:f89c264158db39a1cfcbb5f9b3741413df1cfce4d550c9a475a67d923e19e2f4
[auth-post-confirmation] 3:38:40 PM START RequestId: fce69b9f-b257-4af8-8a6e-821f84a39ce7 Version: $LATEST
[auth-post-confirmation] 3:38:41 PM 2024-07-19T22:38:41.209Z    fce69b9f-b257-4af8-8a6e-821f84a39ce7    INFO    processed 412f8911-acfa-41c7-9605-fa0c40891ea9
[auth-post-confirmation] 3:38:41 PM END RequestId: fce69b9f-b257-4af8-8a6e-821f84a39ce7
[auth-post-confirmation] 3:38:41 PM REPORT RequestId: fce69b9f-b257-4af8-8a6e-821f84a39ce7    Duration: 264.38 ms    Billed Duration: 265 ms    Memory Size: 512 MB    Max Memory Used: 93 MB    Init Duration: 562.19 ms
[auth-pre-authentication] 3:38:41 PM INIT_START Runtime Version: nodejs:18.v30    Runtime Version ARN: arn:aws:lambda:us-east-1::runtime:f89c264158db39a1cfcbb5f9b3741413df1cfce4d550c9a475a67d923e19e2f4
[auth-pre-authentication] 3:38:41 PM START RequestId: 9210ca3a-1351-4826-8544-123684765710 Version: $LATEST
[auth-pre-authentication] 3:38:41 PM END RequestId: 9210ca3a-1351-4826-8544-123684765710
[auth-pre-authentication] 3:38:41 PM REPORT RequestId: 9210ca3a-1351-4826-8544-123684765710    Duration: 3.47 ms    Billed Duration: 4 ms    Memory Size: 512 MB    Max Memory Used: 67 MB    Init Duration: 180.24 ms
[auth-post-authentication] 3:38:42 PM INIT_START Runtime Version: nodejs:18.v30    Runtime Version ARN: arn:aws:lambda:us-east-1::runtime:f89c264158db39a1cfcbb5f9b3741413df1cfce4d550c9a475a67d923e19e2f4
[auth-post-authentication] 3:38:42 PM START RequestId: 60c1d680-ea24-4a8b-93de-02d085859140 Version: $LATEST
[auth-post-authentication] 3:38:42 PM END RequestId: 60c1d680-ea24-4a8b-93de-02d085859140
[auth-post-authentication] 3:38:42 PM REPORT RequestId: 60c1d680-ea24-4a8b-93de-02d085859140    Duration: 4.61 ms    Billed Duration: 5 ms    Memory Size: 512 MB    Max Memory Used: 68 MB    Init Duration: 172.66 ms

Learn more about streaming function logs by visiting the Amplify documentation.

Scheduling Functions

The second improvement is that developers are now able to use cron expressions and natural language to schedule functions to execute on intervals. To get started, specify an interval with the new schedule property:

// amplify/jobs/drink-some-water/resource.ts
import { defineFunction } from "@aws-amplify/backend"

export const drinkSomeWater = defineFunction({
  name: "drink-some-water",
  schedule: "every 1h",
})

Schedules are defined as intervals, and can be used for a variety of use cases such as generating a “front page” of top performing posts every hour or generating a weekly digest of top performing posts. Creating new intervals is as easy as using natural language. In the example below, we’re defining a schedule for a function to “[remind me to] drink some water every day”.

// amplify/jobs/drink-some-water/resource.ts
import { defineFunction } from "@aws-amplify/backend"

export const drinkSomeWater = defineFunction({
  name: "drink-some-water",
  schedule: [
    "every 5m",
    "every 1h",
    "every day",
    "every week",
    "every year",
  ],
})

Scheduling is further simplified by providing a strongly typed property value, which grants tab-completions and ensures schedules adhere to expectations of the system.

Schedules can be defined with complex requirements using cron expressions. For example, a reminder to take out the trash may occur only on two days at specific times:

// amplify/jobs/remind-me-to-take-the-trash-out/resource.ts
import { defineFunction } from "@aws-amplify/backend";

export const remindMe = defineFunction({
  name: "remind-me-to-take-the-trash-out",
  schedule: [
    // every tuesday at 9am
    "0 9 ? * 3 *",
    // every friday at 9am
    "0 9 ? * 6 *",
  ]
})

Under the hood, schedules are powered by Amazon EventBridge Rules, which are a way to describe how EventBridge responds to events. Here, these rules describe intervals in which the Function should be executed.

Learn more about scheduled functions by visiting the Amplify documentation.

Conclusion

We’re excited for developers to try out the two new features to the Amplify functions experience. If you have feedback we’d love to hear from you on our GitHub repository! Join our Discord community to become a member of our community of like-minded developers.