is there a SST way of automatically exporting envi...
# general
a
is there a SST way of automatically exporting environment variables to local
.env
for testing?
For my existing sls projects, I am using
serverless-export-env
plugin
t
We should expand sst-env to facilitate this but I personally avoid this problem by storing everything in
SSM
. Then my application can load it, whether it's a lambda or a test running locally
a
do you also save userPoolId in
SSM
?
t
Yeah I do
a
is it a manual process or you have a way to auto add new item to
SSM
?
t
Copy code
new ssm.StringParameter(this, "ParamUserPoolId", {
      parameterName: "/" + this.stage + "/ship/USER_POOL_ID",
      stringValue: pool.userPoolId,
    })
    new ssm.StringParameter(this, "ParamUserPoolClientId", {
      parameterName: "/" + this.stage + "/ship/USER_POOL_CLIENT_ID",
      stringValue: client.userPoolClientId,
    })
Example from my stack code
(my app is called ship)
a
i see. this looks promising 👍
t
I pass in SSM_PATH as an env variable into my lambdas. It uses that to with ssm.getParametersByPath to get all config values for the stage
a
if I understand correctly, you are creating a dedicated SSM stack at the end so that you can store all variables(table, auth)?
i guess it doesn’t matter. Will try out this approach. thanks @thdxr!
t
I actually am adding the SSM values progressively
but you can do that too
a
sorry one last question - so for testing, you still need to somehow pass the SSM_PATH to tests. How do you manage to accomplish that?
t
I just pass it with
SSM_PATH=whatever jest
s
This is a great pattern!
a
@thdxr I still have trouble accessing SSM from tests. do you mind writing an example I think will benefit the community? 🙏
t
yeah we have this on our todo haven't gotten to it yet
what issue are you running into?
a
I am making a call like this:
Copy code
const result = await ssm.getParametersByPath("/dev/meet-and-donate-app");
and the actual call is:
Copy code
getParametersByPath: async (path) =>
    SSM.getParametersByPath({
      Path: path,
      Recursive: true,
    }).promise(),
but I am hitting this error:
Copy code
● Test Stack

    ConfigError: Missing region in config

       7 |       Path: path,
       8 |       Recursive: true,
    >  9 |     }).promise(),
         |        ^
      10 | };
also I am wondering how come our tests can have access to SSM or DynamoDB directly?
t
Ah you need to pass in
AWS_PROFILE
as an env variable so it uses your local config but also
AWS_SDK_LOAD_CONFIG=1
Forgot about that
a
any thought?
t
Did passing in those env variables not help?
a
now it works with
AWS_REGION=‘us-east-1’ SSM_PATH=‘/dev/meet-and-donate-app’ sst test
😇
t
sweet
a
does it make sense for SST to pass AWS_REGION as a default env variable?
t
well, we shouldn't be interfering with the aws-sdk standards. I think AWS_SDK_LOAD_CONFIG would make it read properly from your
~/.aws
config
a
t
my region is set in my
~/.aws
config
a
yeah i just added the region to my
~/.aws/config
but i still have to pass in ``AWS_SDK_LOAD_CONFIG=true` to my test script in order to have the region loaded
t
yeah that's right - I actually just use
jest
directly not through sst so I was thinking there's nothing sst can do to make that work. I just have it in my package.json
a
@thdxr I followed your pattern and created my own ssm util methods. It serves my need quite well. I thought it might be useful for the community for future reference: https://gist.github.com/zhuangdaz/68979605ab4e908a71f51051b89d6e61
t
nice! We have been meaning to write up this pattern