我想在一个新启动的实例上通过boto3运行一系列bash命令。在
从一些研究看来,要实现这一点,需要将这个新实例与SSM关联起来。在
下面有没有明显的错误或遗漏的步骤?还有没有更好的方法来实现既定目标?
import boto3
ec2c = boto3.client('ec2')
ec2r = boto3.resource('ec2')
ssmc = boto3.client('ssm')
“RoleName”具有附加的AmazonEC2RoleforSSM
策略
res = ec2c.associate_iam_instance_profile(
IamInstanceProfile={
'Arn': 'arn:aws:iam::###:instance-profile/RoleName',
'Name': 'RoleName'
},
InstanceId = instance.id
)
print(ssmc.describe_instance_information()['InstanceInformationList'])
> []
(我认为这个空列表就是下一步失败的原因)
resp = ssmc.send_command(
DocumentName = "AWS-RunShellScript",
Parameters = {'commands': [mkdir app]},
InstanceIds = instance_ids
)
> botocore.errorfactory.InvalidInstanceId: An error occurred ...
> ... (InvalidInstanceId) when calling the SendCommand operation:
您将得到InvalidInstanceId异常,因为ssm代理没有在您的实例上运行。在
相关问题 更多 >
编程相关推荐