Accessing AWS SSM Parameters in NodeJS Lambas

拥有回忆 提交于 2020-12-12 12:22:48

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/56283621/accessing-aws-ssm-parameters-in-nodejs-lambas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!