Hello guys, Can we use async/await into SST stacks...
# help
a
Hello guys, Can we use async/await into SST stacks? I need to call AWS SDK and don’t want to do..
Copy code
ssm.getParameter(key, (err: AWS.AWSError, data: AWS.SSM.GetParameterResult)=>{
But do this instead:
Copy code
await ssm.getParameter(key).promise();
r
I don't see why not, as long as the enclosing function is async
a
Yes but, the stack implementation has to go inside the constructor?
r
you could wrap it in an async function
and call that from the constructor
a
Yeah but is not really nice to have a wrapper there.
Like a self executed function with async.
r
Personally I don't think it's bad to abstract the actual AWS function call via a wrapper. It means you can delegate any checking/logging/error handling to the wrapper function. Any how, you could use an async IIFE, not a fan, personally but would look something like
Copy code
(async () => {
  await ssm.getParameter(key).promise();
})().catch(err => {
    console.error(err);
});
a
Yes, I was trying to avoid to put that .
I mean, it’s similar to using a promise.
r
Or you could do
Copy code
ssm.getParameter(key).promise().then(result => {
  // Do your stuff
})
not as old school as callbacks, not as new school as async/await
f
hmm.. sorry @Adrián Mouly I suggested to call SSM using aws-sdk the other day, but forgot that it’s tricky to do that inside a constructor.
Thanks @Ross Coundon for the workaround! Perhaps, we should support fetching SSM values in
.env
, like:
Copy code
USERNAME=admin
PASSWORD=ssm:password/prod
The SSM is fetched by SST, and
process.env.PASSWORD
has the resolved value.
What do you guys think?
r
I think that'd be cool, I guess the tricky thing might be making sure that the process has permissions for the parameter and any KMS key used to encrypt it
a
@Frank yeah I mean, it works, but I was looking to use async/await, due the code looks cleaner. But I can use promise meanwhile.
r
Do you mean you'll use callbacks meanwhile? The 1st example you posted was the callback version of the method. As I mentioned, you can always do:
Copy code
ssm.getParameter(key).promise().then(result => {
  // Do your stuff
})
a
Yes, I’m going to use your method @Ross Coundon.
But still I don’t like it, hahaha.
r
😄
a
I mean, I need to wrap my entire stack inside it.
Because I need the SSM key to be sent to Lambdas.
That’s why I was looking for async/await, that way it looks cleaner.
My stack is not complicated, and doing this wrap it makes it uglier 😞
But well, there is no perfect world right? 🙂
f
@Ross Coundon yup yup. Calling the aws-sdk api directly would require the same SSM permission. I guess the
.env
way is just a more convenient.
a
Yeah, that is what SLS does @Frank.
In SLS I have a config YML with
ssm://
.
Like..
Copy code
${ssm:/company-common/elastic-search/url~true}
Also has the
true
for encripted.
@Adrián Mouly you mentioned you tried out the approach @Ross Coundon suggested, and it worked locally, but u couldn’t get it to deploy on SEED b/c of some AWS IAM issue
I just created an sample repo that fetches the SSM value inside the SST code and assigning that to Function’s environment https://github.com/fwang/sst-test-call-aws-cdk/blob/main/lib/index.js
Deploy fine for me on SEED.
I know u moved on to fetch SSM inside ur Lambda, but I wanted to share this repo here for reference 🙂
a
Thank you, going to check it.