![](/img/trans.png)
[英]SSM Secure reference is not supported in: AWS::EC2::Instance/Metadata
[英]create ec2 instance with ssm enabled
我正在创建一个附加了 SSM 的 EC2 实例。
def createInstances(self):
instances = self.ec2_client.create_instances(
ImageId="ami-09e67e426f25ce0d7", # Ubuntu Server 20.04 LTS (HVM), SSD Volume Type
MinCount=1,
MaxCount=1,
InstanceType="m4.2xlarge",
KeyName="ec2-key-pair",
IamInstanceProfile={
'Arn': 'arn:aws:iam::aws:instance-profile/AmazonEC2RoleforSSM',
'Name': 'AmazonEC2RoleforSSM'
},
DryRun=True,
TagSpecifications=[
{
'ResourceType': 'instance',
'Tags': [
{
'Key': 'department',
'Value': 'dev'
},
]
},
],
)
print(instances["Instances"][0])
我收到一个错误:
botocore.exceptions.ClientError: An error occurred (InvalidParameterCombination) when calling the RunInstances operation: The parameter 'iamInstanceProfile.name' may not be used in combination with 'iamInstanceProfile.arn'
当我删除“iamInstanceProfile.name”时,出现另一个错误:
botocore.exceptions.ClientError: An error occurred (InvalidParameterValue) when calling the RunInstances operation: Value (arn:aws:iam::aws:instance-profile/AmazonEC2RoleforSSM) for parameter iamInstanceProfile.arn is invalid. Invalid IAM Instance Profile ARN
我认为有几件事正在发生。 首先,您必须在您的帐户中为 SSM 创建实例配置文件。 它不是您可以参考的由 AWS 管理的标准资源。 如果您还没有,请参阅SSM setup instructions for creating an instance profile 。 如此处所述,如果您通过 SSM 快速设置,它会为您创建实例配置文件。 它的 ARN 可能是arn:aws:iam::[your_account_number]:instance-profile/AmazonSSMRoleForInstancesQuickSetup
。 如果您尚未完成 SSM 快速设置,则需要完成或自行创建角色和实例配置文件。
请注意,如果您通过控制台创建角色,控制台会为您创建一个与角色同名的实例配置文件(如果该角色与 EC2 相关联)。 如果您使用 CLI、API 或 CDK 创建角色,则需要单独创建实例配置文件。 无论哪种方式,您都需要为角色分配正确的 IAM 策略。
其次,尽管有名称,但AmazonEC2RoleforSSM
是一个 IAM Policy ,而不是一个角色......并且它已被弃用。 它已被一组策略所取代,这些策略对 SSM 权限提供更细粒度的控制。 有关详细信息,请参阅有关托管实例策略最佳实践的AWS 管理和治理博客。 因此,当您设置角色时,您需要为其分配适当的 SSM 策略。
试试IamInstanceProfile = { 'Name': 'AmazonEC2RoleforSSM' }
。
您的实例配置文件 ARN 确实无效。 它应该是arn:aws:iam::XXXXXXXXXXXX:instanceprofile/AmazonEC2RoleforSSM
,其中 XXXXXXXXXXXX 代表您的 AWS 帐号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.