Reputation: 237
I'm using serverless.yml ssm variables (AWS parameter store) to set lambda environment variables. They're looked up and set at Serverless deploy time. I'd like the environment variables to be up to date at lambda runtime, so that I can change them in parameter store and not re-deploy. Is there a way to achieve this with Serverless?
P.S. I know I could achieve this by looking them up in my lambda code instead of relying on Serverless to set them. I'd just like to know if Serverless has this capability.
Upvotes: 1
Views: 1496
Reputation: 40124
Call the SSM client from your code:
const SSM = require('aws-sdk/clients/ssm')
const getParameter = async (paramName) => {
const client = new SSM()
try {
const { Parameter } = await getSSMClient()
.getParameter({ Name: paramName })
.promise()
return Parameter.Value ? JSON.parse(Parameter.Value) : null
} catch (e) {
console.error(e)
}
}
You can put this outside of your default handler export which will call it only on lambda launch (which might be less than on invoke depending on traffic)
const foo = await getParameter('foo')
module.exports = (event, context) => {
console.log(foo)
...
}
But as far as redeploying - it can be extremely fast. I personally separate my stuff into multiple services and redeploying lambda is a separate process than say DynamoDB setup so it goes fast.
Upvotes: 1
Reputation: 1540
Environment variables are designed to be set at deployment because they pertain to the environment of the Lambda. If you need values that are changed regularly there are many other options; DynamoDB sticks out as the perfect solution to that issue. If all you need is a Key Value store (which is what environment variables are), then DynamoDB absolutely excels. Its super fast and way cheaper than SSM that can be used to do the same thing.
Upvotes: 0