amplify-backend icon indicating copy to clipboard operation
amplify-backend copied to clipboard

Scheduled Lambda function not-authed to access the AppSync model

Open aneaga opened this issue 4 months ago • 2 comments

Have deployed a scheduled function as follows

import { defineFunction } from "@aws-amplify/backend";

export const scheduledTasks = defineFunction({
  name: "scheduled-tasks",
  schedule: "every week",
});
import { generateClient } from 'aws-amplify/data';
import type { EventBridgeHandler } from 'aws-lambda';
import { type Schema } from "../../data/resource";
import { Amplify } from "aws-amplify";
import { getAmplifyDataClientConfig } from '@aws-amplify/backend/function/runtime';
import outputs from "../../../amplify_outputs.json";

Amplify.configure(outputs);

const client = generateClient<Schema>({
  authMode: "???",
});

export const handler: EventBridgeHandler<'Scheduled Event', null, void> = async () => {
  try {
    const { data, errors } = await client.models.Family.list();

and the model for Family is set up as follows

.authorization((allow) => [
        allow.owner().to(['create', 'update', 'delete', 'read']),
        allow.authenticated().to(['create', 'read']),
    ]),

The app is using a Cognito pool for usual API calls.

export const auth = defineAuth({
  loginWith: {
      email: true,
      externalProviders: {
        signInWithApple: {
...

My function runs and fails to access the model (which is kinda-of expected) with NoValidAuthTokens.

I could not find any examples or instructions on how to set up auth for such a setup: backend scheduled job that needs access to the model.

Please advice. Thank you.

aneaga avatar Aug 06 '25 07:08 aneaga