简体   繁体   中英

AWS-RunShellScript not invoked from Lambda function

I am using nodeJS 12.x lambda function to invoke certain commands on one of the EC2 instance. I have made sure that

  • SSM agent is installed on the EC2 instance
  • Proper roles are assigned to the lambda function, Policies are - AmazonEC2ReadOnlyAccess, AmazonSSMFullAccess, AWSLambdaExecute.

Below is the lambda code:

var AWS = require('aws-sdk');
const ssm = new AWS.SSM();
AWS.config.update({region:'ap-south-1'});

exports.handler = function(event, context) {
    var ec2 = new AWS.EC2();

    ec2.describeInstances(function(err, data) {
        if(err) {
            console.log(err, err.stack);
        }
        else {
            let instance = data.Reservations[0].Instances[0].InstanceId;
            console.log("\nInstance Id: ", instance);
            
            ssm.sendCommand({
                DocumentName: "AWS-RunShellScript",
                InstanceIds: [ instance ],
                TimeoutSeconds: 3600,
                Parameters: {
                    commands: ['ifconfig']
                }
            }, function(err, data) {
                if (err) {
                    console.log("\nError:", err);
                } else {
                    console.log("\nSuccess: ", data);
                }
                context.done(null, 'Function Finished!');
            })
        }        
    });    
};

When I invoke this function manually I am getting the status as pending . Below is the output log.

Response:
"Function Finished!"

Request ID:
"748b280a-4277-42a1-a0c3-************"

Function logs:
START RequestId: 748b280a-4277-42a1-a0c3-************ Version: $LATEST
2020-11-05T08:52:26.895Z    748b280a-4277-42a1-a0c3-************    INFO    
Inside describe instances:
2020-11-05T08:52:26.952Z    748b280a-4277-42a1-a0c3-************    INFO    
Instance Id:  i-016f4673e082a829e
2020-11-05T08:52:27.237Z    748b280a-4277-42a1-a0c3-************    INFO    
Success:  {
  Command: {
    CommandId: '8b7a3b6d-4a7a-4259-9c82-************',
    DocumentName: 'AWS-RunShellScript',
    DocumentVersion: '',
    Comment: '',
    ExpiresAfter: 2020-11-05T10:52:27.220Z,
    Parameters: { commands: [Array] },
    InstanceIds: [ 'i-****************' ],
    Targets: [],
    RequestedDateTime: 2020-11-05T08:52:27.220Z,
    Status: 'Pending',
    StatusDetails: 'Pending',
    OutputS3BucketName: '',
    OutputS3KeyPrefix: '',
    MaxConcurrency: '50',
    MaxErrors: '0',
    TargetCount: 1,
    CompletedCount: 0,
    ErrorCount: 0,
    DeliveryTimedOutCount: 0,
    ServiceRole: '',
    NotificationConfig: {
      NotificationArn: '',
      NotificationEvents: [],
      NotificationType: ''
    },
    CloudWatchOutputConfig: { CloudWatchLogGroupName: '', CloudWatchOutputEnabled: false },
    TimeoutSeconds: 3600
  }
}
END RequestId: 748b280a-4277-42a1-a0c3-************
REPORT RequestId: 748b280a-4277-42a1-a0c3-************  Duration: 677.90 ms Billed Duration: 700 ms Memory Size: 128 MB Max Memory Used: 96 MB  

Why is the status not success ? When I manually use 'RunCommand' it works properly. What am I doing wrong?

The command status is showing as pending because, it is currently in pending. Once you execute the command it goes to from pending ----> Complete.

if you take the command ID (CommandId: '8b7a3b6d-4a7a-4259-9c82-************' in above case) and look into System Manager Run Command, by the time you try to search for it, it will show successful or failed

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM