你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

使用 Terraform 计划在 Amazon Elastic Compute Cloud 上部署 Amazon Linux 2 实例并将其连接到 Azure Arc

本文提供了使用提供的 Terraform 计划部署 Amazon Web Services (AWS) Amazon Elastic Compute Cloud (EC2) Linux 2 实例并将其连接为支持 Azure Arc 的服务器资源的指南。

先决条件

  1. 克隆 Azure Arc 快速入门存储库。

    git clone https://github.com/microsoft/azure_arc.git
    
  2. 要安装或更新 Azure CLI。 Azure CLI 应运行版本 2.7.0 或更高版本。 使用 az --version 检查当前安装的版本。

  3. 生成 SSH 密钥(或使用现有 SSH 密钥)

  4. 创建免费 AWS 帐户

  5. 安装 Terraform >= 0.12

  6. 创建 Azure 服务主体。

    要将 AWS 虚拟机连接到 Azure Arc,需要一个被分配了“参与者”角色的 Azure 服务主体。 要创建此角色,请登录 Azure 帐户并运行以下命令:

    az login
    az account set -s <Your Subscription ID>
    az ad sp create-for-rbac -n "<Unique SP Name>" --role contributor --scopes "/subscriptions/<Your Subscription ID>"
    

    例如:

    az ad sp create-for-rbac -n "http://AzureArcAWS" --role contributor --scopes "/subscriptions/00000000-0000-0000-0000-000000000000"
    

    输出应如下所示:

    {
      "appId": "XXXXXXXXXXXXXXXXXXXXXXXXXXXX",
      "displayName": "http://AzureArcAWS",
      "password": "XXXXXXXXXXXXXXXXXXXXXXXXXXXX",
      "tenant": "XXXXXXXXXXXXXXXXXXXXXXXXXXXX"
    }
    

    注意

    我们强烈建议你将服务主体的范围限定为特定的 Azure 订阅和资源组

创建 AWS 标识

为了让 Terraform 在 AWS 中创建资源,我们需要创建一个具有适当权限的新 AWS IAM 角色并配置 Terraform 以使用它。

  1. 登录到 AWS 管理控制台

  2. 登录后,选择左上角的“服务”下拉列表。 在“安全、标识和合规性”下面,选择“IAM”访问“标识和访问管理”页

    A screenshot of the AWS cloud console.

    A screenshot of an identity and access management AWS cloud console.

  3. 从左侧菜单中单击“用户”,然后选择“添加用户”来创建一个新的 IAM 用户。

    A screenshot of how to create a new user in an AWS cloud console.

  4. 在“添加用户”页上,将用户命名 Terraform 并选中“编程访问”复选框,然后选择“下一步”

    Second screenshot of how to create a new user in an AWS cloud console.

  5. 在“设置权限”页面上,选择“直接附加现有策略”,然后选中 AmazonEC2FullAccess 旁边的框(如屏幕截图所示),然后选择“下一步”。

    Third screenshot of how to create a new user in an AWS cloud console.

  6. 在“标记”页面上,分配一个带有 azure-arc-demo 键的标记,然后选择“下一步”继续查看页面。

    A screenshot of tags in an AWS cloud console.

  7. 验证所有内容是否正确,然后选择 " 创建用户"。

    Fourth screenshot of how to create a new user in an AWS cloud console.

  8. 创建用户后,你将看到用户的访问密钥 ID 和机密访问密钥。 复制这些值,然后再选择“关闭”。 在下一页上,你可以看到内容的示例。 拥有这些密钥后,你将能够将它们与 Terraform 一起使用来创建 AWS 资源。

    A screenshot of creating a user successfully in an AWS cloud console.

配置 Terraform

在执行 Terraform 计划之前,必须导出计划将使用的环境变量。 这些变量基于你的 Azure 订阅和租户、Azure 服务主体以及你刚刚创建的 AWS IAM 用户和密钥。

  1. 使用 az account list 命令检索你的 Azure 订阅 ID 和租户 ID。

  2. Terraform 计划在 Microsoft Azure 和 AWS 中创建资源。 然后它在 AWS EC2 虚拟机上执行脚本以安装 Azure Arc 代理和所有必要的工件。 此脚本需要有关你的 AWS 和 Azure 环境的某些信息。 使用适当的值编辑 scripts/vars.sh 并更新每个变量。

    • TF_VAR_subscription_id = Azure 订阅 ID
    • TF_VAR_client_id= 你的 Azure 服务主体应用程序 ID
    • TF_VAR_client_secret = 你的 Azure 服务主体密码
    • TF_VAR_tenant_id= 你的 Azure 租户 ID
    • AWS_ACCESS_KEY_ID = AWS 访问密钥
    • AWS_SECRET_ACCESS_KEY = AWS 密钥
  3. 在 Azure CLI 中,导航到克隆的存储库的 azure_arc_servers_jumpstart/aws/al2/terraform 目录。

  4. 使用源命令运行 scripts/vars.sh,导出编辑的环境变量,如下所示。 Terraform 需要进行这些设置才能使计划正确执行。 请注意,此脚本也将作为 Terraform 部署的一部分在 AWS 虚拟机上远程自动执行。

    source ./scripts/vars.sh
    
  5. 请确保 SSH 密钥在 ~/.ssh 中可用,并且名为 id_rsa.pubid_rsa。 如果你按照上面的 ssh-keygen 指南创建密钥,那么这应该已经正确设置。 如果没有,你可能需要修改 main.tf 以使用具有不同路径的密钥。

  6. 运行 terraform init 命令,该命令将下载 Terraform AzureRM 提供程序。

    A screenshot of the terraform init command.

部署

  1. 运行 terraform apply --auto-approve 命令并等待计划完成。 完成后,你将拥有一个部署好的 AWS Amazon Linux 2 EC2 实例,并已作为新资源组中的启用了 Azure Arc 的新服务器建立连接。

  2. 打开 Azure 门户并导航到 arc-servers-demo 资源组。 在 AWS 中创建的虚拟机将显示为资源。

    A screenshot showing an Azure Arc-enabled server in the Azure portal.

半自动部署(可选)

你可能已经注意到,运行的最后一步是将 VM 注册为启用了 Azure Arc 的新服务器资源。

A screenshot of the azcmagent connect command.

如果要演示/控制实际的注册过程,请执行以下操作:

  1. install_arc_agent.sh.tmpl 脚本模板中,注释掉 run connect command 部分并保存文件。

    A screenshot of the azcmagent connect command commented out.

  2. 通过运行 terraform output 获取 AWS VM 的公共 IP。

    A screenshot of output from Terraform.

  3. 使用 ssh ec2-user@xx.xx.xx.xx 以 SSH 方式连接到 VM,其中 xx.xx.xx.xx 是主机 IP。

    A screenshot of an SSH key connecting to an EC2 server.

  4. 导出 vars.sh 中的所有环境变量

    A screenshot of exported environment variables in var.sh.

  5. 运行以下命令:

    azcmagent connect --service-principal-id $TF_VAR_client_id --service-principal-secret $TF_VAR_client_secret --resource-group "Arc-Servers-Demo" --tenant-id $TF_VAR_tenant_id --location "westus2" --subscription-id $TF_VAR_subscription_id
    

    Another screenshot of the azcmagent connect command.

  6. 完成后,你的 VM 将在 Azure Arc 中注册并通过 Azure 门户在资源组中可见。

删除部署

要删除你在此演示中创建的所有资源,请使用 terraform destroy --auto-approve 命令,如下所示。 A screenshot of the terraform destroy command.

或者,你可以通过从 AWS 控制台终止 AWS EC2 实例来直接删除该实例。 A screenshot of how to terminate an instance in the AWS console.