I don't know if this is expected behavior, but whe...
# help
a
I don't know if this is expected behavior, but when I set environment variables in defaultFunctionProps I don't get them in functions I set with the "function" option. Example:
_const_ api = new sst.Api(this, 'Api', {
defaultFunctionProps: {
srcPath: 'src/',
environment: {
SOMETHING: props.something,
},
},
routes: {
'POST /some-function': {
function: {
handler: 'someFunction.handler',
}
},
'POST /some-other-function': 'someOtherFunction.handler',
}
}
It looks like someOtherFunction is able to access the SOMETHING env variable, but not someFunction.
j
This might be expected behavior, let me check with @Frank.
f
both functions should be able to access the
SOMETHING
for each route, SST merges the
defaultFunctionProps
with the route’s function
In your case, both functions (someFunction and someOtherFunction) will inherit
srcPath
and
environment
defined in the
defaultFunctionProps
@Andreas can you send me the CloudFormation template generated for this stack? You can find it under
.build/cdk.out/xxxx.template.js
a
Oh, I also see that I set the environment in someFunction. Maybe that overrides the environment set in defaultFunctionProps?
f
Yeah.. the environment in the route replaces the environment in defaultFunctionProps, not merged.
I opened an issue here to track if more people find this behavior confusing - https://github.com/serverless-stack/serverless-stack/issues/162
A solution here would be to merge route environment and default environment.
a
Ah, ok :) Well, I think it makes sense to merge them. That way, you could have global variables, and it's easy to set specific variables for a route.
f
Yeah.. that makes a lot of sense actually.
Btw, do you have other resources in the Stack, like Topic or Queue? What I’m trying to get to is that does it make sense to have global variables for all Lambda functions in a stack?
a
No, I have a dedicated stack for the api. No other resources there.
f
Hey @Andreas Just a heads up, starting v0.10.8 on,
environment
inside
defaultFunctionProps
will be merged with the one defined at the function level.