概要
- こんにちわ、今回は担当案件でオンプレミスとAWS 環境を接続する構成がありまして、オンプレミスのサーバーからAWS のリソースへアクセスを行います。事前に検証した知見を記事にまとめますね。
- 先ずは、本記事では、オンプレミスのサーバーを AWS Systems Manager のハイブリッドアクティベーションで管理する方法を記載します。
オンプレミスを AWS Systems Manager で管理する方法
はじめに
- AWS Systems Manager は、AWS のEC2 インスタンスと非 EC2 の両方を管理できます。Systems Manager で管理されるマシンは、マネージドノードと呼ばれます。
- AWS Systems Manager のハイブリッドアクティベーションを使用して、マネージドノードを管理します。
- ハイブリッドアクティベーションは、IAM ユーザーのアクセスキーではなく、IAM STS の一時的な認証情報を使用するため、安全にAWS 環境の認証を提供します。
- ハイブリッドアクティベーションのインスタンスティアの設定に、スタンダード階層、アドバンスト階層があり、標準のスタンダード階層では、アカウントあたり最大1,000の非EC2 のノードを登録できます。1,000を超える非EC2 ノードを管理する場合は、アドバンスト階層に切り替えます。アドバンスト階層は、従量課金となります。
- ハイブリッドアクティベーションは、以下の流れで行います。
- IAM ロールを作成する
- マネージドノードのアクティベーションを作成する
- マネージドノード(オンプレミスのサーバー)にSSM Agent をインストールする
- Systems Managerのフリートマネージャーでマネージドノード(オンプレミスのサーバー)を管理する
- 以下にオンプレミスのサーバーを AWS Systems Manager で管理する方法を記載します。
IAM ロールを作成する
- マネージドノードに付与するAWS環境の権限を設定するため、IAM ロールを作成します。
- 先ず信頼されたエンティティタイプに、「AWS のサービス」を選択し、サービスに「Systems Manager」を選択します。
- 許可ポリシーを選択します。選択するポリシーには、AmazonSSMManagedInstanceCore が必須で、その他にCloudWatch やS3 など必要な権限を追加します。
- ロール名を入力し、ロールの作成を行います。
- IAMロールが作成されました。
マネージドノードのアクティベーションを作成する
- マネージメントコンソールからSystems Managerのサービスにアクセスし、ハイブリッドアクティベーションを選択します。
- 「アクティベーションを作成する」を選択します。
- アクティベーションの説明(オプション)、インスタンス制限を入力します。
- IAMロールに、既存のカスタム IAMロール(先ほど作成したロール)を指定します。
- アクティベーションの有効期限を指定します。指定可能な有効期限日は、最大で30日後となります。
- アクティベーションの作成を選択します。
- 以下の通り、新しいアクティベーションが作成されました。「Activation Code」、「Activation ID」を記録します。特に、「Activation Code」は再度表示できません。注意ください。
マネージドノード(オンプレミスのサーバー)にSSM Agent をインストールする
LinuxマシンにSSM Agent をインストールする
- オンプレミスのLinuxマシンから以下のコマンドを実行します。(regionは環境に応じて変更ください)
- ドキュメントは、こちらを参照。
mkdir /tmp/ssm
curl https://amazon-ssm-region.s3.region.amazonaws.com/latest/linux_amd64/ssm-setup-cli -o /tmp/ssm/ssm-setup-cli
sudo chmod +x /tmp/ssm/ssm-setup-cli
sudo /tmp/ssm/ssm-setup-cli -register -activation-code “activation-code” -activation-id “activation-id” -region “region”
- 以下実行結果です。
sh-4.2$ mkdir /tmp/ssm sh-4.2$ curl https://amazon-ssm-ap-northeast-1.s3.ap-northeast-1.amazonaws.com/latest/linux_amd64/ssm-setup-cli -o /tmp/ssm/ssm-setup-cli % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 12.4M 100 12.4M 0 0 44.0M 0 --:--:-- --:--:-- --:--:-- 44.0M sh-4.2$ sudo chmod +x /tmp/ssm/ssm-setup-cli sh-4.2$ sudo /tmp/ssm/ssm-setup-cli -register -activation-code "xxxxxxxxxxxxxxxxxxxx" -activation-id "c1a881bd-6e9f-4349-a2fd-c636b3e835e0" -region "ap-northeast-1" 2024-08-24 15:38:48 INFO ssm-setup-cli -version: 3.3.551.0 2024-08-24 15:38:48 INFO Setting proxy config 2024-08-24 15:38:48 INFO Proxy environment variables: 2024-08-24 15:38:48 INFO https_proxy: 2024-08-24 15:38:48 INFO http_proxy: 2024-08-24 15:38:48 INFO no_proxy: 2024-08-24 15:38:48 INFO Setup parameters: 2024-08-24 15:38:48 INFO env= 2024-08-24 15:38:48 INFO install=false 2024-08-24 15:38:48 INFO shutdown=false 2024-08-24 15:38:48 INFO role= 2024-08-24 15:38:48 INFO tags= 2024-08-24 15:38:48 INFO register=true 2024-08-24 15:38:48 INFO region=ap-northeast-1 2024-08-24 15:38:48 INFO override=false 2024-08-24 15:38:48 INFO version= 2024-08-24 15:38:48 INFO manifest-url= 2024-08-24 15:38:48 INFO artifactsDir= 2024-08-24 15:38:48 INFO skip-signature-validation=false 2024-08-24 15:38:48 DEBUG Package manager rpm is available, checking if agent is installed 2024-08-24 15:38:48 INFO Agent is already installed with rpm, selecting it as package manager 2024-08-24 15:38:48 INFO Selecting Systemctl as service manager 2024-08-24 15:38:48 INFO Initialize download manager 2024-08-24 15:38:48 DEBUG getting platform details 2024-08-24 15:38:48 DEBUG fetching platform details from /etc/os-release 2024-08-24 15:38:48 DEBUG Command output &{Amazon Linux 2} 2024-08-24 15:38:48 INFO Detected platform Amazon Linux 2024-08-24 15:38:48 DEBUG getting platform details 2024-08-24 15:38:48 DEBUG fetching platform details from /etc/os-release 2024-08-24 15:38:48 DEBUG Command output &{Amazon Linux 2} 2024-08-24 15:38:48 DEBUG Determining endpoint for service s3 in region ap-northeast-1 2024-08-24 15:38:48 DEBUG [DownloadManager] attempting to download as http/https download from https://s3.ap-northeast-1.amazonaws.com/amazon-ssm-ap-northeast-1/ssm-agent-manifest.json to /tmp/ssm/ssm_setup_cli_artifacts3105887933/child_4175408781 2024-08-24 15:38:48 INFO [DownloadManager] /tmp/ssm/ssm_setup_cli_artifacts3105887933/child_4175408781/2b10b61cbf1e65013d883366a1acb297c0bd4b6a with 531190 bytes downloaded 2024-08-24 15:38:48 INFO found package amazon-ssm-agent 2024-08-24 15:38:48 INFO found file amazon-ssm-agent-linux-amd64.tar.gz 2024-08-24 15:38:48 INFO [DownloadManager] Downloading SSM Setup CLI 2024-08-24 15:38:48 DEBUG [DownloadManager] attempting to download as http/https download from https://s3.ap-northeast-1.amazonaws.com/amazon-ssm-ap-northeast-1/latest/linux_amd64/ssm-setup-cli to /tmp/ssm/ssm_setup_cli_artifacts3105887933/child_4175408781 2024-08-24 15:38:49 INFO [DownloadManager] /tmp/ssm/ssm_setup_cli_artifacts3105887933/child_4175408781/008a47b127454cb9217f506eba2e19f557bcdf74 with 13084888 bytes downloaded 2024-08-24 15:38:49 INFO [DownloadManager] Downloaded SSM-Setup-CLI successfully 2024-08-24 15:38:51 INFO Installed agent version - 3.0.1124.0 2024-08-24 15:38:51 INFO Version already installed 3.0.1124.0: 2024-08-24 15:38:51 INFO Attempting to configure agent 2024-08-24 15:38:51 INFO Verifying agent is installed before attempting to register 2024-08-24 15:38:51 INFO Verified agent is installed 2024-08-24 15:38:51 INFO Agent is not registered 2024-08-24 15:38:51 INFO Stopping agent before registering 2024-08-24 15:38:51 INFO Stopping agent using Systemctl service manager 2024-08-24 15:39:01 INFO Successfully stopped agent 2024-08-24 15:39:01 INFO Registering agent 2024-08-24 15:39:02 INFO Successfully registered the agent, starting agent 2024-08-24 15:39:02 INFO Starting agent using Systemctl service manager 2024-08-24 15:39:02 INFO Agent is running 2024-08-24 15:39:02 INFO Successfully started agent, reloading registration info 2024-08-24 15:39:02 INFO Successfully registered the instance with AWS SSM using Managed instance-id: mi-0f577af595b8fa76c 2024-08-24 15:39:09 INFO Process Path: /usr/bin/amazon-ssm-agent 2024-08-24 15:39:09 INFO Agent process count: 1 2024-08-24 15:39:09 INFO Agent registration completed
WindowsマシンにSSM Agent をインストールする
- オンプレミスのWindowsマシンからPower Shell を管理者として起動し、以下のコマンドを実行します。(regionは環境に応じて変更ください)
- ドキュメントは、こちらを参照。
[System.Net.ServicePointManager]::SecurityProtocol = ‘TLS12’
$code = “activation-code”
$id = “activation-id”
$region = “us-east-1”
$dir = $env:TEMP + “\ssm”
New-Item -ItemType directory -Path $dir -Force
cd $dir
(New-Object System.Net.WebClient).DownloadFile(“https://amazon-ssm-$region.s3.$region.amazonaws.com/latest/windows_amd64/ssm-setup-cli.exe”, $dir + “\ssm-setup-cli.exe”)
./ssm-setup-cli.exe -register -activation-code=”$code” -activation-id=”$id” -region=”$region”
Get-Content ($env:ProgramData + “\Amazon\SSM\InstanceData\registration”)
Get-Service -Name “AmazonSSMAgent”
- 以下実行結果です。
Windows PowerShell Copyright (C) Microsoft Corporation. All rights reserved. Install the latest PowerShell for new features and improvements! https://aka.ms/PSWindows PS C:\Users\Administrator> [System.Net.ServicePointManager]::SecurityProtocol = 'TLS12' PS C:\Users\Administrator> $code = "xxxxxxxxxxxxxxxxxxxx" PS C:\Users\Administrator> $id = "c1a881bd-6e9f-4349-a2fd-c636b3e835e0" PS C:\Users\Administrator> $region = "ap-northeast-1" PS C:\Users\Administrator> $dir = $env:TEMP + "\ssm" PS C:\Users\Administrator> New-Item -ItemType directory -Path $dir -Force Directory: C:\Users\ADMINI~1\AppData\Local\Temp\2 Mode LastWriteTime Length Name ---- ------------- ------ ---- d----- 8/24/2024 3:49 PM ssm PS C:\Users\Administrator> cd $dir PS C:\Users\Administrator\AppData\Local\Temp\2\ssm> (New-Object System.Net.WebClient).DownloadFile("https://amazon-ssm-$region.s3.$region.amazonaws.com/latest/windows_amd64/ssm-setup-cli.exe", $dir + "\ssm-setup-cli.exe") PS C:\Users\Administrator\AppData\Local\Temp\2\ssm> ./ssm-setup-cli.exe -register -activation-code="$code" -activation-id="$id" -region="$region" 2024-08-24 15:49:34 INFO ssm-setup-cli -version: 3.3.551.0 2024-08-24 15:49:34 INFO Setting proxy config 2024-08-24 15:49:34 DEBUG Current proxy environment variables: https_proxy:;http_proxy:;no_proxy: 2024-08-24 15:49:34 INFO Getting IE proxy configuration for current user: The operation completed successfully. 2024-08-24 15:49:34 DEBUG IE proxy configuration for current user: proxy:,bypass:,enabled:false,automatically detect proxy settings:true,automatic configuration script: 2024-08-24 15:49:34 INFO Getting WinHTTP proxy default configuration: The operation completed successfully. 2024-08-24 15:49:34 DEBUG WinHTTP proxy default configuration: proxy:,bypass: 2024-08-24 15:49:34 DEBUG ParseProxySettings result: http_proxy:,https_proxy: 2024-08-24 15:49:34 INFO Proxy environment variables: 2024-08-24 15:49:34 INFO https_proxy: 2024-08-24 15:49:34 INFO http_proxy: 2024-08-24 15:49:34 INFO no_proxy: 2024-08-24 15:49:34 INFO Setup parameters: 2024-08-24 15:49:34 INFO env= 2024-08-24 15:49:34 INFO install=false 2024-08-24 15:49:34 INFO shutdown=false 2024-08-24 15:49:34 INFO role= 2024-08-24 15:49:34 INFO tags= 2024-08-24 15:49:34 INFO register=true 2024-08-24 15:49:34 INFO region=ap-northeast-1 2024-08-24 15:49:34 INFO override=false 2024-08-24 15:49:34 INFO version= 2024-08-24 15:49:34 INFO manifest-url= 2024-08-24 15:49:34 INFO artifactsDir= 2024-08-24 15:49:34 INFO skip-signature-validation=false 2024-08-24 15:49:34 DEBUG Package manager windows is available, checking if agent is installed 2024-08-24 15:49:34 INFO Agent is already installed with windows, selecting it as package manager 2024-08-24 15:49:34 INFO Selecting windows as service manager 2024-08-24 15:49:34 DEBUG getting platform details 2024-08-24 15:49:35 DEBUG Command output 8 2024-08-24 15:49:35 INFO Initialize download manager 2024-08-24 15:49:35 DEBUG getting platform details 2024-08-24 15:49:36 DEBUG Command output Microsoft Windows Server 2022 Datacenter 2024-08-24 15:49:36 DEBUG getting platform details 2024-08-24 15:49:36 DEBUG Command output 8 2024-08-24 15:49:36 INFO Detected platform Windows 2024-08-24 15:49:36 DEBUG getting platform details 2024-08-24 15:49:36 DEBUG Command output 10.0.20348 2024-08-24 15:49:36 DEBUG Determining endpoint for service s3 in region ap-northeast-1 2024-08-24 15:49:36 DEBUG [DownloadManager] attempting to download as http/https download from https://s3.ap-northeast-1.amazonaws.com/amazon-ssm-ap-northeast-1/ssm-agent-manifest.json to C:\Users\Administrator\AppData\Local\Temp\2\ssm\ssm_setup_cli_artifacts299573573\child_3976159760 2024-08-24 15:49:36 INFO [DownloadManager] C:\Users\Administrator\AppData\Local\Temp\2\ssm\ssm_setup_cli_artifacts299573573\child_3976159760\2b10b61cbf1e65013d883366a1acb297c0bd4b6a with 531190 bytes downloaded 2024-08-24 15:49:36 INFO found package amazon-ssm-agent 2024-08-24 15:49:36 INFO found file amazon-ssm-agent-windows-amd64.zip 2024-08-24 15:49:36 INFO [DownloadManager] Downloading SSM Setup CLI 2024-08-24 15:49:36 DEBUG [DownloadManager] attempting to download as http/https download from https://s3.ap-northeast-1.amazonaws.com/amazon-ssm-ap-northeast-1/latest/windows_amd64/ssm-setup-cli.exe to C:\Users\Administrator\AppData\Local\Temp\2\ssm\ssm_setup_cli_artifacts299573573\child_3976159760 2024-08-24 15:49:36 INFO [DownloadManager] C:\Users\Administrator\AppData\Local\Temp\2\ssm\ssm_setup_cli_artifacts299573573\child_3976159760\eba56f799ac4f5c7f6b046dd756cb4b9eb207904 with 11541656 bytes downloaded 2024-08-24 15:49:36 INFO [DownloadManager] Downloaded SSM-Setup-CLI successfully 2024-08-24 15:49:36 INFO Installed agent version - 3.3.551.0 2024-08-24 15:49:36 INFO Version already installed 3.3.551.0: 2024-08-24 15:49:36 INFO Attempting to configure agent 2024-08-24 15:49:36 INFO Verifying agent is installed before attempting to register 2024-08-24 15:49:36 INFO Verified agent is installed 2024-08-24 15:49:36 INFO Agent is not registered 2024-08-24 15:49:36 INFO Stopping agent before registering 2024-08-24 15:49:36 INFO Stopping agent using windows service manager 2024-08-24 15:49:47 INFO Successfully stopped agent 2024-08-24 15:49:47 INFO Registering agent 2024-08-24 15:49:50 INFO Successfully registered the agent, starting agent 2024-08-24 15:49:50 INFO Starting agent using windows service manager 2024-08-24 15:49:52 INFO Agent is running 2024-08-24 15:49:52 INFO Successfully started agent, reloading registration info 2024-08-24 15:49:52 INFO Successfully registered the instance with AWS SSM using Managed instance-id: mi-0f4b64be77f5c3d5b 2024-08-24 15:49:57 INFO Process Path: amazon-ssm-agent.exe 2024-08-24 15:49:57 INFO Agent process count: 1 2024-08-24 15:49:57 INFO Agent registration completed PS C:\Users\Administrator\AppData\Local\Temp\2\ssm> Get-Content ($env:ProgramData + "\Amazon\SSM\InstanceData\registration") {"ManagedInstanceID":"mi-0f4b64be77f5c3d5b","Region":"ap-northeast-1"} PS C:\Users\Administrator\AppData\Local\Temp\2\ssm> Get-Service -Name "AmazonSSMAgent" Status Name DisplayName ------ ---- ----------- Running AmazonSSMAgent Amazon SSM Agent PS C:\Users\Administrator\AppData\Local\Temp\2\ssm>
フリートマネージャーでマネージドノード(オンプレミスのサーバー)を管理する
- 以下は、マネージドノード登録前のアクティベーションの表示です。
- 以下は、マネージドノード登録後のアクティベーションの表示です。登録済みインスタンスが変更されています。
- Systems Manager のフリートマネージャーを選択します。マネージドインスタンスに、SSM Agent をインストールした2つのノードが追加されました。「mi」から始まるノードid となります。
次のステップ
- 関連記事として、次にオンプレミスのLinuxサーバー、WindowsサーバーのメトリクスをAWS環境のCloudWatch に出力する方法を以下にまとめています。
Hybrid Activationsでオンプレ Linux をCloudWatch Agent設定する
Hybrid Activationsでオンプレ Windows をCloudWatch Agent設定する
参考資料
- AWS Systems Managerユーザーガイド: ハイブリッド Linux ノードで SSM Agent をインストールする方法
- AWS Systems Managerユーザーガイド: ハイブリッド Windows ノードで SSM Agent をインストールする方法