<@U01J5Q8HV5Z> <@U01JVDKASAC> Hello Guys, need an...
# help
s
@Jay @Frank Hello Guys, need an urgent blocker help from you all. I am defining a cron job inside my stack but to my surprise cannot assign an environment variable to the same Below is the code. The testprop never appears in the lambda env variable this.cronJob = new sst.Cron(this, "bitbucket-scheduler-cron", { schedule: "rate(5 minutes)", job: { function: "src/bitbucket-api/scheduler.main", environment: { testprop: this.bitbucketJobTable.dynamodbTable.tableName, }, }, }); this.cronJob.attachPermissions([ this.bitbucketScheduleJobTable, this.bitbucketJobTable, auditLogTable, errorLogTable, ]);
r
shouldn't you go with
Copy code
this.cronJob = new sst.Cron(this, "bitbucket-scheduler-cron", {
  schedule: "rate(5 minutes)",
  job: {
    function: "src/bitbucket-api/scheduler.main",
    jobProps: {
      environment: {
        testprop: this.bitbucketJobTable.dynamodbTable.tableName,
      },
    },
  },
});
?
Wait, I may be wrong with above statement - these will apply to Event Bridge target definition. Take a look at https://docs.serverless-stack.com/constructs/Function#functiondefinition - instead of passing
string
as path to the function, create new instance of
Function
and pass env there
It could be smth like
Copy code
this.cronJob = new sst.Cron(this, "bitbucket-scheduler-cron", {
  schedule: "rate(5 minutes)",
  job: {
    function: new Function(this, "SchedulerFunction", {
      handler: "src/bitbucket-api/scheduler.main",
      environment: {
        testprop: this.bitbucketJobTable.dynamodbTable.tableName,
      },
    }),
  },
});
if I got it correctly
s
Thanks @Robert Banaszak I am giving it a try now
@Robert Banaszak i used this below error while deploying:
Copy code
SyntaxError: Arg string terminates parameters early
    at new Function (<anonymous>)
this.cronJob = new sst.Cron(this, "bitbucket-scheduler-cron", { schedule: "rate(5 minutes)", job: { function: new Function(this, "bitbucket-scheduler", { handler: "src/bitbucket-api/scheduler.main", timeout: 60, environment: { bitbucketScheduleJobTableName: this.bitbucketScheduleJobTable.dynamodbTable.tableName, bitbucketScheduleJobUserIdIndexName: "scheduleuserCreatedDateIndex", scheduleJobIdIndexName: "jobIdCreatedDateIndex", bitbucketJobTableName: this.bitbucketJobTable.dynamodbTable.tableName, bitbucketJobUserIdIndexName: "userCreatedDateIndex", identifierIndexName: "identifierCreatedDateIndex", }, }), }, });
a
You’re probably complicating things by using an anonymous nested function. Go through this example - https://serverless-stack.com/examples/how-to-use-cron-jobs-in-your-serverless-app.html Should sort things out.
s
No @Ashishkumar Pandey You did not got my problem. My problem is i want to use environment variables in my cron job. I cannot use it . Hence the complication
a
change the function to :-
Copy code
new Cron(this, "Cron", {
  schedule: "rate(1 minute)",
  job: {
    function: {
        handler: 'src/lambda.js',
        environment: {
            hello: process.env.hello
        }
    },
  },
});
r
Do you import
Function
from
sst
? Alternatively:
Copy code
const bitbucketSchedulerFunction = new sst.Function(
  this,
  "bitbucket-scheduler-cron",
  {
    handler: "src/bitbucket-api/scheduler.main",
    environment: {
      testprop: this.bitbucketJobTable.dynamodbTable.tableName,
    },
  }
);

this.cronJob = new sst.Cron(this, "bitbucket-scheduler-cron", {
  schedule: "rate(5 minutes)",
  job: {
    function: bitbucketSchedulerFunction,
  },
});
a
You don’t need a new Function instance, cron will handle it itself.
r
Intuitively both should work, but in practice I'm afraid ^ is correct - https://github.com/serverless-stack/serverless-stack/blob/master/packages/resources/src/Function.ts#L506
f
@Robert Banaszak @Ashishkumar Pandey Really appreciate helping out on a Saturday ☀️ Both of these work:
Copy code
new Cron(this, "Cron", {
  schedule: "rate(1 minute)",
  job: {
    function: {
        handler: 'src/lambda.js',
        environment: {
            hello: process.env.hello
        }
    },
  },
});
And this
Copy code
const fn = new Function(this, "Fn", {
  handler: 'src/lambda.js',
  environment: {
    hello: process.env.hello
  }
});

new Cron(this, "Cron", {
  schedule: "rate(1 minute)",
  job: {
    function: fn,
  },
});
In the former case,
Cron
will create the
Function
internally.
a
so, is using a new function instance as the value for the function key a valid approach as well?
r
And in the latter, it will use the Function we've created. Now I see it, thanks for confirming 🙏
a
@Frank we must help as much as we can, can’t have a robust community without trying.
f
so, is using a new function instance as the value for the function key a valid approach as well?
Yeah it is. Mostly unnecessary, useful when sharing a function across ie. multiple Api routes.
a
Ah! makes sense.
r
Personally I like defining them separately because I can push them into arrays that I can iterate over to set specific properties, like a set of functions with a higher timeout or memory setting, or environment