0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

AWS LambdaでAWS Systems Manager(SSM)のAWS-RunShellScriptを実行する(Node.js)

Last updated at Posted at 2021-07-04

行おうとしたこと

LambdaのTest実行から、EC2インスタンス上のシェルスクリプト(/home/ec2-user/hoge.sh)を実行する。

前提

Lambdaのランタイム設定 Node.js 12.x、ハンドラindex.handler。
SSM 高速セットアップ実行をして、EC2にSSMから操作されるロールが設定済み。

ポイント

Lambdaのhandlerはasyncにしない、asyncだとLambdaでエラーがでていないのにindex.jsのssm.sendCommandが実行されていないような状態になる。

Lambdaの実行ロールにAmazonSSMMaintenanceWindowRoleを設定する、AmazonSSMFullAccessではindex.jsのssm.sendCommandで権限エラーがでました。

Lambdaのindex.js

const AWS = require('aws-sdk')
 
exports.handler = (event) => {
    console.log('始まってます');
    const ssm = new AWS.SSM();
    ssm.sendCommand({
        InstanceIds: ["{インスタンスID}"],
        DocumentName: "AWS-RunShellScript",
        Parameters: {
            commands: ["/home/ec2-user/hoge.sh"]
        }
    }, (err, data) => {
          if (err) {
              console.log('エラーです');
              console.log(err, err.stack);
              return err
          } else {
              console.log('成功です');
              console.log(data);
              return data.CommandId;
          }
    });
    console.log('終了');
};

Execution results

Test Event Name
hoge

Response
null

Function Logs
START RequestId: 9a3d4c84-7fba-4dc3-9310-4f9025632940 Version: 1
2021-07-04T12:05:10.289Z	9a3d4c84-7fba-4dc3-9310-4f9025632940	INFO	始まってます
2021-07-04T12:05:10.570Z	9a3d4c84-7fba-4dc3-9310-4f9025632940	INFO	終了
2021-07-04T12:05:10.820Z	9a3d4c84-7fba-4dc3-9310-4f9025632940	INFO	成功です
2021-07-04T12:05:10.820Z	9a3d4c84-7fba-4dc3-9310-4f9025632940	INFO	{
  Command: {
    CommandId: '51e98cb1-7a46-4cac-92c2-39468407a837',
    DocumentName: 'AWS-RunShellScript',
    DocumentVersion: '$DEFAULT',
    Comment: '',
    ExpiresAfter: 2021-07-04T14:05:10.791Z,
    Parameters: { commands: [Array] },
    InstanceIds: [ 'i-0e7108f51709dc4c0' ],
    Targets: [],
    RequestedDateTime: 2021-07-04T12:05:10.791Z,
    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: 9a3d4c84-7fba-4dc3-9310-4f9025632940
REPORT RequestId: 9a3d4c84-7fba-4dc3-9310-4f9025632940	Duration: 694.41 ms	Billed Duration: 695 ms	Memory Size: 128 MB	Max Memory Used: 92 MB

Request ID
9a3d4c84-7fba-4dc3-9310-4f9025632940
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?