在NodeJS Lambas中访问AWS SSM参数 [英] Accessing AWS SSM Parameters in NodeJS Lambas
问题描述
我能够从NodeJS中的AWS SSM参数存储本地检索数据,但是当我将代码移至Lambdas时却无法.
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.
我已经寻找并没有找到许多不使用无服务器"框架的使用NodeJS设置Lambda的示例.
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.
我已将我的lambda的IAM策略授予这些权限:
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'));
});
在本地定义数据.在我的lambda中,我得到以下错误:"{TypeError:无法读取属性'Parameter'为null",意味着'data'是空的,就像err一样.
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.
推荐答案
尝试使用下面的promise语法-它对我有用.请注意,我也获得了多个参数,因此调用也有所不同.
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"
],
然后...
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.`)));
这篇关于在NodeJS Lambas中访问AWS SSM参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!