Jakob Fix
04/19/2021, 9:31 PMdefaultFunctionProps
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:
// 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"
});
Frank
Cron
doesn’t have a defaultFunctionProps
. Currently only APIs have defaultFunctionProps
b/c there are likely to be a ton of routes/functions.Frank
Cron
on the other hand only has 1 job handler.Frank
// 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'),
}
}
});
Frank
Jakob Fix
04/19/2021, 9:34 PMJakob Fix
04/19/2021, 9:38 PMFrank
lib/index.js
app.setDefaultFunctionProps({
timeout: 20,
memorySize: 512,
environment: {
...
},
});
Jakob Fix
04/19/2021, 9:43 PMJakob Fix
04/20/2021, 8:44 PMlib/index.js
as you suggested, but I seem to do something wrong.
When I do this:
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:
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? 😕Frank
app.setDefaultFunctionProps
before the BdayStack
Jakob Fix
04/20/2021, 8:49 PMvalueForStringParameter
in that case? stack
is not yet available.Frank
Frank
Frank
Frank
Frank
Jakob Fix
04/21/2021, 6:03 AMFrank
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.Frank
Jakob Fix
04/22/2021, 5:32 PMJakob Fix
04/22/2021, 10:21 PMFrank