API call from lambda function to AWS.SSM.putParameter() returns success, but leaves SSM.parameters unchanged

0

Hello all, AWS and node.js noob here. I am trying to save a code captured from URL parameters to my parameters store in the System Manager service, and later return a hashed value from various strings. My code returns the hash and I don't have any errors in the console, but when I test it with URL/?code=1234&challenge_code=5678, nothing changes in my parameter store. I already have an object named ebayUserCode in there of type string.

exports.handler = async (event) => {
    const crypto = require('crypto');
    const AWS = require('aws-sdk')
    let verificationToken = "FIXME";
    let endpoint = "FIXME";
    let code = "code";
    let challengeCode = "challenge_code";
    
    if (event.queryStringParameters && event.queryStringParameters.code) {
        console.log("Received name: " + event.queryStringParameters.code);
        code = event.queryStringParameters.code;
        var ssm = new AWS.SSM();
        var params = {
            Name: 'ebayUserCode', /* required */
            Value: code, /* required */
            Overwrite: true
            };
        ssm.putParameter(params, function(err, data) {
            if (err) console.log(err, err.stack); // an error occurred
            else     console.log(data);           // successful response
        });
    }
    if (event.queryStringParameters && event.queryStringParameters.challengeCode) {
        console.log("Received name: " + event.queryStringParameters.challengeCode);
        challengeCode = event.queryStringParameters.challengeCode;
    }
    
    const hash = crypto.createHash('sha256');
    hash.update(challengeCode);
    hash.update(verificationToken);
    hash.update(endpoint);
    const responseHash = hash.digest('hex');
    console.log(new Buffer.from(responseHash).toString());
    
    const response = {
        statusCode: 200,
        body: responseHash,
    };
    return response;
};

What am I doing wrong? Any help would be greatly appreciated. Thanks in advance!

已提問 2 年前檢視次數 443 次
2 個答案
0
已接受的答案

Ended up just rewriting my lambda function in Python, and invoking the SecretManager API. It is working excellently now, and SecretManager has turned out to be much better for my application. It seems I still have a lot to explore when it comes to AWS!

已回答 2 年前
0

Given how your function is written, you probably want to change ssm.putParameter(params, function (err, data) {}) to await ssm.putParameter(params).promise(). The way it is right now, the call to putParameter is not really guaranteed to happen before the rest of your function executes, so that could be part of your problem. If you want to avoid await for some reason, you'll have to move the second half of your function into the callback of putParameters where you currently have console.log(data).

Farski
已回答 2 年前
  • Changed to this:

        var ssm = new AWS.SSM();
        var params = {
            Name: 'ebayUserCode', /* required */
            Value: code, /* required */
            Overwrite: true
            };
        await ssm.putParameter(params).promise();
    

    Also tried this:

        ssm.putParameter(params, function(err, data) {
            if (err) console.log(err, err.stack); // an error occurred
            else {
                console.log(data);           // successful response
                    if (event.queryStringParameters && event.queryStringParameters.challengeCode) {
        console.log("Received name: " + event.queryStringParameters.challengeCode);
        challengeCode = event.queryStringParameters.challengeCode;
    }
    
    const hash = crypto.createHash('sha256');
    hash.update(challengeCode);
    hash.update(verificationToken);
    hash.update(endpoint);
    const responseHash = hash.digest('hex');
    console.log(new Buffer.from(responseHash).toString());
    
    const response = {
        statusCode: 200,
        body: responseHash,
    };
    return response;
            }
        });
    

    Still no change to my parameter in ssm. The actual call was copied verbatim from the API documentation here: https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/SSM.html#putParameter-property

    Sorry about the formatting!

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南