我正在尝试从 AWS Lambda 调用 SSM 文档。这是相同的 lambda 函数:
const AWS = require('aws-sdk');
const ssm = new AWS.SSM();
const SSM_DOCUMENT_NAME = process.env.SSM_DOCUMENT_NAME;
AWS.config.update({region: 'ap-south-1'});
const sendCommand = (instance_id) => {
var params = {
DocumentName: SSM_DOCUMENT_NAME,
InstanceIds: [instance_id],
TimeoutSeconds: 300
};
console.log("before call:"+params.InstanceIds);
return ssm.sendCommand(params).promise();
}
exports.handler = async (event) => {
console.log('event ', JSON.stringify(event));
try {
const instance_id = event.detail.EC2InstanceId;
console.log("processing autoscaling event for "+instance_id);
await sendCommand(instance_id);
console.log('sent command');
} catch (error) {
throw error;
}
}
这在记录“调用前:”行后引发以下异常。该实例已经存在很长时间并且不是最近创建的,因此没有一致性问题。
Invoke Error
{
"errorType": "InvalidInstanceId",
"errorMessage": null,
"code": "InvalidInstanceId",
"message": null,
"time": "2020-06-19T09:43:10.348Z",
"requestId": "150decf1-be1d-407f-9a86-a4f986c04807",
"statusCode": 400,
"retryable": false,
"retryDelay": 20.23844834854607,
"stack": [
"InvalidInstanceId: null",
" at Request.extractError (/var/runtime/node_modules/aws-sdk/lib/protocol/json.js:51:27)",
" at Request.callListeners (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:106:20)",
" at Request.emit (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:78:10)",
" at Request.emit (/var/runtime/node_modules/aws-sdk/lib/request.js:683:14)",
" at Request.transition (/var/runtime/node_modules/aws-sdk/lib/request.js:22:10)",
" at AcceptorStateMachine.runTo (/var/runtime/node_modules/aws-sdk/lib/state_machine.js:14:12)",
" at /var/runtime/node_modules/aws-sdk/lib/state_machine.js:26:10",
" at Request.<anonymous> (/var/runtime/node_modules/aws-sdk/lib/request.js:38:9)",
" at Request.<anonymous> (/var/runtime/node_modules/aws-sdk/lib/request.js:685:12)",
" at Request.callListeners (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:116:18)"
]
}
该函数在从 lambda 测试时工作正常。运行完整设置时发生错误。这是我想要实现的流程:
实例终止 > 生命周期挂钩 > Cloudwatch 事件 > Lambda > SSM 运行命令。根据我的理解,从 lambda 进行测试与实时设置之间的唯一区别是,在测试实例时,实例处于运行状态,最终运行处于终止:等待状态。我还将等待时间增加到 600 秒,但没有运气。
附加观察:在 Auto Scaling 组 > 活动中 - 我可以看到一条消息说正在等待生命周期事件。但是,一旦我终止实例,它就会从 Systems Manager 选项卡中的管理实例列表中消失。因此,在触发 lambda 时,instanceid 对 Systems Manager 不可用。
观察/解决方案 2:我测试错了。仅当扩展策略认为实例运行状况不佳时,生命周期挂钩才起作用。手动终止实例不起作用。现在我把它写下来似乎很明显。