使用 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 Jumpstart 存放庫。

    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. 建立用戶之後,您會看到使用者的存取金鑰標識碼和秘密存取密鑰。 選取 [關閉] 之前,請先將這些值複製下來。 在下一個頁面上,您可以看到這看起來應該像的範例。 擁有這些金鑰之後,您就可以搭配 Terraform 使用這些金鑰來建立 AWS 資源。

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

設定 Terraform

在執行 Terraform 方案之前,您必須匯出計劃將使用的環境變數。 這些變數取決於您的 Azure 訂用帳戶和租使用者、Azure 服務主體,以及您剛才建立的 AWS IAM 使用者和金鑰。

  1. 使用 命令擷取您的 Azure 訂用帳戶標識碼和租用戶 az account list 標識碼。

  2. Terraform 方案會在 Microsoft Azure 和 AWS 中建立資源。 接著,它會在 AWS EC2 虛擬機上執行腳本,以安裝 Azure Arc 代理程式和所有必要的成品。 此腳本需要 AWS 和 Azure 環境的特定資訊。 使用適當的值編輯 scripts/vars.sh 和更新每個變數。

    • TF_VAR_subscription_id= 您的 Azure 訂用帳戶識別碼
    • TF_VAR_client_id= 您的 Azure 服務主體應用程式識別碼
    • TF_VAR_client_secret = 您的 Azure 服務主體密碼
    • TF_VAR_tenant_id= 您的 Azure 租使用者識別碼
    • AWS_ACCESS_KEY_ID = AWS 存取金鑰
    • AWS_SECRET_ACCESS_KEY = AWS 秘密金鑰
  3. 從 Azure CLI 瀏覽至 azure_arc_servers_jumpstart/aws/al2/terraform 複製存放庫的目錄。

  4. 使用source命令執行 scripts/vars.sh 來匯出您編輯的環境變數,如下所示。 Terraform 需要設定這些項目,計劃才能正確執行。 請注意,此腳本也會在 Terraform 部署中從遠端在 AWS 虛擬機上自動執行。

    source ./scripts/vars.sh
    
  5. 請確定您的 SSH 金鑰可在 與中 ~/.ssh 取得,並命名 id_rsa.pub 為 與 id_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.xxSSH 連線到 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.