在 Boto3 中使用 SSM send_command

Using the SSM send_command in Boto3

我正在尝试创建一个 lambda 函数,它将关闭 EC2 实例上的系统服务 运行ning。我认为使用 boto3 模块中的 ssm 客户端可能是最佳选择,我考虑使用的特定命令是 send_command()。理想情况下,我想使用 Ansible 来关闭 systemd 服务。所以我正在尝试使用“AWS-ApplyAnsiblePlaybooks”,我在这里被卡住了,似乎 boto3 ssm 客户端需要一些参数,我已经尝试遵循 boto3 文档 here, but really isn't clear on how it wants me to present the parameters, I found the parameters it's looking for inside the "AWS-ApplyAnsiblePlaybooks" document - but when I include them in my code, it tells me that the parameters are invalid. I also tried going to AWS' GitHub repository because I know they sometime have examples of code but they didn't have anything for the send_command(). I've upload a gist 以防人们对我到目前为止所写的内容,我肯定有兴趣了解其他人如何通过 boto3 python 脚本使用 ssm 运行 获得他们的 Ansible 剧本。

据我所知 documentation for that SSM document and the code you shared in the gist。您需要添加 "SourceType":["S3"] 并且您需要在 Source Info 中有一个路径,例如:

{
   "path":"https://s3.amazonaws.com/path_to_directory_or_playbook_to_download"
}

所以你需要调整你的全局变量S3_DEVOPS_ANSIBLE_PLAYBOOKS。

查看文档 link 中的 CLI 示例,它应该会让您了解如何重新构建参数:

aws ssm create-association --name "AWS-ApplyAnsiblePlaybooks" \
--targets Key=tag:TagKey,Values=TagValue \
--parameters '{"SourceType":["S3"],"SourceInfo":["{\"path\":\"https://s3.amazonaws.com/path_to_Zip_file,_directory,_or_playbook_to_download\"}"],"InstallDependencies":["True_or_False"],"PlaybookFile":["file_name.yml"],"ExtraVariables":["key/value_pairs_separated_by_a_space"],"Check":["True_or_False"],"Verbose":["-v,-vv,-vvv, or -vvvv"]}' \
--association-name "name" --schedule-expression "cron_or_rate_expression"