I’m trying to pass some `defaultFunctionProps` to ...
# sst
j
I’m trying to pass some
defaultFunctionProps
to a
Cron
construct, but while the deployment doesn’t break, I don’t see the environment variables created in the AWS console, and when I checked the SST documentation, this property wasn’t mentioned. This is what it looks like now, and its equivalent works OK for the
Api
construct:
Copy code
// Create a Cron job to ping each morning at 9h15 CEST
    new sst.Cron(this, "Cron", {
      defaultFunctionProps: {
        environment: {
          CALENDAR_ID: ssm.StringParameter.valueForStringParameter(this, 'CALENDAR_ID'),
          // FIXME: cannot currently use SecureStrings because: <https://github.com/aws/aws-cdk/issues/6819>
          GOOGLE_APPLICATION_CREDENTIALS_EMAIL: ssm.StringParameter.valueForStringParameter(this, 'GOOGLE_APPLICATION_CREDENTIALS_EMAIL'),
          GOOGLE_APPLICATION_CREDENTIALS_KEY: ssm.StringParameter.valueForStringParameter(this, 'GOOGLE_APPLICATION_CREDENTIALS_KEY'),
          GOOGLE_APPLICATION_CREDENTIALS_KEY_ID: ssm.StringParameter.valueForStringParameter(this, 'GOOGLE_APPLICATION_CREDENTIALS_KEY_ID'),
          SLACK_WEBHOOK_URL: ssm.StringParameter.valueForStringParameter(this, 'SLACK_WEBHOOK_URL'),
          GIPHY_APIKEY: ssm.StringParameter.valueForStringParameter(this, 'GIPHY_APIKEY'),
          SLACK_SIGNING_SECRET: ssm.StringParameter.valueForStringParameter(this, 'SLACK_SIGNING_SECRET'),
        }
      },
      schedule: "cron(15 7 ? * MON-FRI *)",
      job: "src/cronjob.handler"
    });
f
Hey @Jakob Fix, yeah
Cron
doesn’t have a
defaultFunctionProps
. Currently only APIs have
defaultFunctionProps
b/c there are likely to be a ton of routes/functions.
Cron
on the other hand only has 1 job handler.
Copy code
// Create a Cron job to ping each morning at 9h15 CEST
    new sst.Cron(this, "Cron", {
      schedule: "cron(15 7 ? * MON-FRI *)",
      job: {
        handler: "src/cronjob.handler",
        environment: {
          CALENDAR_ID: ssm.StringParameter.valueForStringParameter(this, 'CALENDAR_ID'),
          // FIXME: cannot currently use SecureStrings because: <https://github.com/aws/aws-cdk/issues/6819>
          GOOGLE_APPLICATION_CREDENTIALS_EMAIL: ssm.StringParameter.valueForStringParameter(this, 'GOOGLE_APPLICATION_CREDENTIALS_EMAIL'),
          GOOGLE_APPLICATION_CREDENTIALS_KEY: ssm.StringParameter.valueForStringParameter(this, 'GOOGLE_APPLICATION_CREDENTIALS_KEY'),
          GOOGLE_APPLICATION_CREDENTIALS_KEY_ID: ssm.StringParameter.valueForStringParameter(this, 'GOOGLE_APPLICATION_CREDENTIALS_KEY_ID'),
          SLACK_WEBHOOK_URL: ssm.StringParameter.valueForStringParameter(this, 'SLACK_WEBHOOK_URL'),
          GIPHY_APIKEY: ssm.StringParameter.valueForStringParameter(this, 'GIPHY_APIKEY'),
          SLACK_SIGNING_SECRET: ssm.StringParameter.valueForStringParameter(this, 'SLACK_SIGNING_SECRET'),
        }
      }
    });
Does this work for you?
j
Let me redeploy … 😉
👍 😄
f
btw, you might’ve noticed, in a recent release, we added app level default function props, so if this set of credentials you are setting are common other functions (ie. API, etc), u can do this in ur
lib/index.js
Copy code
app.setDefaultFunctionProps({
  timeout: 20,
  memorySize: 512,
  environment: {
    ...
  },
});
j
Ooh, nice!!! Thanks for pointing that out, I do have these env variables in several places. 😄
Hi @Frank, I tried to move my params to the
lib/index.js
as you suggested, but I seem to do something wrong. When I do this:
Copy code
export default function main(app) {
    const stack = new BdayStack(app, "bday-stack");
  
    app.setDefaultFunctionProps({
      environment: {
        CALENDAR_ID: ssm.StringParameter.valueForStringParameter(stack, "CALENDAR_ID"),
...
the value for
CALENDAR_ID
is not available in the
Cron
function. If, however, I define that env var directly within the scope of the
Cron
construct, it works, like so:
Copy code
new sst.Cron(this, "Cron", {
      schedule: "cron(15 7 ? * MON-FRI *)",
      job: {
        handler: "src/cronjob.handler",
        environment: {
          CALENDAR_ID: ssm.StringParameter.valueForStringParameter(this, "CALENDAR_ID"),
        }
...
Could it be that there is an issue with how I define the env vars inside the
App
construct? 😕
f
Move
app.setDefaultFunctionProps
before the
BdayStack
j
Sorry for being so daft, but what’s the scope for
valueForStringParameter
in that case?
stack
is not yet available.
f
Oh hmm.. that’s a good question
I’m not sure on top of my head.. let me try a couple of things out
Hey Jakob, so yeah u need to pass a ‘Stack’ object in.
There are a few ways to get around this issue. I’m going to run them across the team. Most likely, we will push something out to support this.
I will keep you posted.
j
Cool, looking forward to it. Until then I can work around it by declaring the env vars for each stack.👍
f
Hey @Jakob Fix, you can now do this in v0.13.0
Copy code
export default function main(app) {
    app.setDefaultFunctionProps((stack) => ({
      environment: {
        CALENDAR_ID: ssm.StringParameter.valueForStringParameter(stack, "CALENDAR_ID"),
      },
    }));

    const stack = new BdayStack(app, "bday-stack");
So essentially,
app.setDefaultFunctionProps
takes a callback where you will have access to the
stack
scope.
j
Yay!🤩 I'll try this later. Thanks so much, @Frank!
Works perfectly. 💯
f
AWESOME🎉