4

I am able to retrieve data from the AWS SSM Parameter Store locally in NodeJS but am unable to when I move my code to Lambdas.

I've hunted and not found many examples of setting up Lambdas with NodeJS that aren't using the "Serverless" framework.

I know I'm missing something simple. I just don't know what yet.

I've given my lambda's IAM policy these permissions:

"Effect": "Allow",
"Action": [
    "ssm:PutParameter",
    "ssm:GetParameter"
],
"Resource": [
    "arn:aws:ssm:region:account-id:parameter/Name/Of/Parameter"
]
AWS.config.update({region: 'my-region'})
const ssm = new AWS.SSM()
ssm.getParameter({
  Name: '/Name/Of/Parameter',
  WithDecryption: false
}, (err, data) => {
  if (err) {
    reject(err);
  }
  if (data.Parameter !== undefined) {
     resolve(data.Parameter.Value);
  }
  reject(new Error('No Parameter'));
});

Locally data is defined. In my lambda I get the error: "{ TypeError: Cannot read property 'Parameter' of null" meaning 'data' is empty as is err.

Any insight is appreciated.

Josh Rodarte
  • 45
  • 1
  • 1
  • 3

1 Answers1

6

Try the promise syntax below--it works for me. Note that I am also getting multiple parameters, so the call is different, as well.

"Action": [
    "ssm:GetParameters",
    "ssm:GetParameter"
],

Then...

ssm.getParameters({
      Names: [`/path/${environmentStage}/more/path/${name}`],
      WithDecryption: true,
    }).promise()).then(data => data.Parameters.length ? data.Parameters[0].Value : Promise.reject(new Error(`SSM Parameter ${name} is not set.`)));

Ben Hulan
  • 537
  • 2
  • 7
  • 1
    `return new Promise(async (resolve, reject) => { const ssm = new AWS.SSM() ssm.getParameter( { Name: '/Name/Of/Parameter', WithDecryption: false } ).promise().then((data) => { if(data !== undefined) { if(data.Parameter !== undefined) { resolve(data.Parameter.Value as string) } } reject('not defined') }).catch((e) => { reject('caught something') }) })` Well, I don't get the error. I'll have to trace further to see if my issue is resolved however. – Josh Rodarte May 24 '19 at 15:28
  • That did work. My code also updates the same parameter with putParameter using my original syntax and I didn't need to change it. Go figure. I dislike it but I am unblocked, so thank you. – Josh Rodarte May 24 '19 at 15:46