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!

asked 2 years ago439 views
2 Answers
0
Accepted Answer

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!

answered 2 years ago
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
answered 2 years ago
  • 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!

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions