SSM sendcommand does not return any messages at lambda function

0

Hello there!

I'm a very beginner engineer and really new to AWS and web application systems. I have some questions about SSM send command functions.

I tried to run EC2 by lambda function using SSM sendcommand. But there are no errors and no response from sendcommand. Besides, it didn't send any log to CloudWatch. It worked from System Manager console so I think IAM policies are fine. The lambda uses javascript and AWS SDK for JavaScript v2. Please tell me how to solve this problem.. Thank you in advance for your kind help!

This is the program which I tried to run on EC2,

#!/bin/bash  
  
echo test >> test.txt  
echo test  

And this is index.js.

const AWS = require("aws-sdk");  
  
const SSM = new AWS.SSM({ region: process.env.REGION });  
const REMOTE_WORKING_DIR = '/home/ec2-user/environment/test';  
  
exports.handler = async (event) => {  
    try{  
        let command = 'sh test.sh';  
          
        let params = {  
            DocumentName: 'AWS-RunShellScript',  
            InstanceIds: \["**********"],  
            Parameters: {  
                commands: \\[command\],  
                workingDirectory: \\[REMOTE_WORKING_DIR\]  
            },  
              
            CloudWatchOutputConfig: {  
                CloudWatchOutputEnabled: true  
            },  
              
            TimeoutSeconds: 3600  
        };  
          
        console.log(params);  
          
        await SSM.sendCommand(params, function(err, data){  
            if(err){  
                console.log("error", err);  
            }else{  
                console.log("success", data);  
            }  
        });  
    }catch(e){  
        console.log("ERROR");  
        console.log(e);  
    }  
      
    return;  
};  

Edited by: yoshi09japan on Feb 7, 2021 6:09 PM

質問済み 3年前859ビュー
1回答
0

I found this was caused by not waiting the sendCommand.
This works fine.

        //Modified these lines  
        // await SSM.sendCommand(params, function(err, data){  
        //     if(err){  
        //         console.log("error", err);  
        //     }else{  
        //         console.log("success", data);  
        //     }  
        // });  
  
        //this works fine  
        const sendCommandResult = await SSM.sendCommand(params).promise();  
        console.log(sendCommandResult);  
回答済み 3年前

ログインしていません。 ログイン 回答を投稿する。

優れた回答とは、質問に明確に答え、建設的なフィードバックを提供し、質問者の専門分野におけるスキルの向上を促すものです。

質問に答えるためのガイドライン

関連するコンテンツ