【问题标题】:create ec2 instance with ssm enabled创建启用 ssm 的 ec2 实例
【发布时间】:2021-10-21 05:41:14
【问题描述】:

我正在创建一个附有 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

【问题讨论】:

    标签: python-3.x amazon-web-services amazon-ec2 boto3 aws-ssm


    【解决方案1】:

    我认为有几件事正在发生。首先,您必须在您的账户中为 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 权限的更细粒度控制的策略所取代。有关详细信息,请参阅托管实例策略最佳实践中的 this AWS Management and Governance Blog。因此,当您设置角色时,您需要为其分配适当的 SSM 策略。

    【讨论】:

      【解决方案2】:

      试试IamInstanceProfile = { 'Name': 'AmazonEC2RoleforSSM' }。

      您的实例配置文件 ARN 确实无效。应该是 arn:aws:iam::XXXXXXXXXXXX:instanceprofile/AmazonEC2RoleforSSM,其中 XXXXXXXXXXXX 代表您的 AWS 帐号。

      【讨论】:

      • 所以您收到错误消息说没有运气?
      • 哦不 :) 我的意思是我们仍然收到错误作为无效名称
      • 您正在创建实例配置文件吗?看起来你不是。
      猜你喜欢
      • 2021-06-09
      • 1970-01-01
      • 1970-01-01
      • 2021-04-16
      • 2021-10-12
      • 1970-01-01
      • 2018-07-17
      • 1970-01-01
      • 2017-07-05
      相关资源
      最近更新 更多