Search code examples
node.jsaws-lambdaaws-sdk-nodejsaws-ssm

Accessing AWS SSM Parameters in NodeJS Lambas


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.


Solution

  • 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.`)));