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!

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠