

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# `AWS-AttachIAMToInstance`
<a name="automation-aws-attachiamtoinstance"></a>

 **描述** 

将 AWS Identity and Access Management (IAM) 角色附加到托管实例。

 [运行此自动化（控制台）](https://console.aws.amazon.com/systems-manager/automation/execute/AWS-AttachIAMToInstance) 

**文档类型**

自动化

**所有者**

Amazon

**平台**

Linux、macOS、Windows

**参数**
+ AutomationAssumeRole

  类型：字符串

  描述：（可选）允许 Systems Manager Automation 代表您执行操作 AWS Identity and Access Management (IAM) 角色的 Amazon 资源名称（ARN）。如果未指定角色，Systems Manager Automation 将使用启动此运行手册的用户的权限。
+ ForceReplace

  类型：布尔值

  描述：（可选）指定是否替换现有 IAM 配置文件的标志。

  默认：True
+ InstanceId

  类型：字符串

  描述：（必需）要为其分配 IAM 角色的实例 ID。
+ RoleName

  类型：字符串

  描述：（必需）要添加到托管实例的 IAM 角色名称。

 **文档步骤** 

1.  `aws:executeAwsApi`- DescribeInstanceProfile -查找附加到 EC2 实例的 IAM 实例配置文件。

1.  `aws:branch`- CheckInstanceProfileAssociations -检查附加到 EC2 实例的 IAM 实例配置文件。

   1.  如果附加了 `ForceReplace` IAM 实例配置文件并将 设置为 `true`：

      1.  `aws:executeAwsApi`- DisassociateIamInstanceProfile -解除 IAM 实例配置文件与 EC2 实例的关联。

   1.  `aws:executeAwsApi`- ListInstanceProfilesForRole -列出所提供的 IAM 角色的实例配置文件。

   1.  `aws:branch`- CheckInstanceProfileCreated -检查提供的 IAM 角色是否有关联的实例配置文件。

      1. 如果 IAM 角色具有关联的实例配置文件：

         1.  `aws:executeAwsApi`-AttacHiam ProfileToInstance -将 IAM 实例配置文件角色附加到 EC2 实例。

      1. 如果 IAM 角色没有关联的实例配置文件：

         1.  `aws:executeAwsApi`- CreateInstanceProfileForRole -为指定的 IAM 角色创建实例配置文件角色。

         1.  `aws:executeAwsApi`- AddRoleToInstanceProfile -将实例配置文件角色附加到指定的 IAM 角色。

         1.  `aws:executeAwsApi`- GetInstanceProfile-获取指定 IAM 角色的实例配置文件数据。

         1.  `aws:executeAwsApi`-AttacHiam ProfileToInstanceWithRetry -将 IAM 实例配置文件角色附加到 EC2 实例。

 **输出** 

附上IAMProfileToInstanceWithRetry。 AssociationId

GetInstanceProfile.InstanceProfileName

GetInstanceProfile.InstanceProfileArn

附上IAMProfileToInstance。 AssociationId

ListInstanceProfilesForRole.InstanceProfileName

ListInstanceProfilesForRole.InstanceProfileArn