Quantcast
Channel: Node.jsタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 8835

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

$
0
0
行おうとしたこと LambdaのTest実行から、EC2インスタンス上のシェルスクリプト(/home/ec2-user/hoge.sh)を実行する。 前提 Lambdaのランタイム設定 Node.js 12.x、ハンドラindex.handler ポイント 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

Viewing all articles
Browse latest Browse all 8835

Trending Articles