AWS Tools for PowerShell インストールをRun Commandで省力化する

Run Command を活用した AWS Tools for PowerShell のインストールを紹介
2019.02.12

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

こんにちは。 ご機嫌いかがでしょうか。 "No human labor is no human error" が大好きな吉井です。

AWS Tools for PowerShell をお使いでしょうか? EC2 上の Windows Server で S3 などの AWS サービスを利用したい場合に導入することがあるかもしれません。

インストール自体は簡単なのですが、台数が増えてくるとやはり面倒です。 Systems Manager Run Command を使って省力化してみたいと思います。

前提

SSM セットアップ

この処理は SSM を使います。 SSM のセットアップを行ってください。 手順は本エントリでは割愛します。公式ドキュメントを参照ください。

https://docs.aws.amazon.com/ja_jp/systems-manager/latest/userguide/systems-manager-setting-up.html

Run Command セットアップ

Run Command のセットアップも割愛します。公式ドキュメントを参照ください。

https://docs.aws.amazon.com/ja_jp/systems-manager/latest/userguide/sysman-rc-setting-up.html

実装

それでは実装します。 マネジメントコンソールへログインし Systems Manager を開きます。 左側メニューから ドキュメント をクリックします。 <ドキュメント>画面が表示されたら、ドキュメントの作成 をクリックします。

<ドキュメントの作成>画面では、判別しやすい名前を入力します。

コンテンツの YAML にチェックを入れ、コンテンツは以下をコピー&ペーストします。

---
schemaVersion: "2.2"
description: "Install AWS Tools for Powershell"
parameters:
  executionTimeout:
    type: "String"
    default: "3600"
    description: "(Optional) The time in seconds for a command to complete before it is considered to have failed. Default is 3600 (1 hour). Maximum is 172800 (48 hours)."
    allowedPattern: "([1-9][0-9]{0,4})|(1[0-6][0-9]{4})|(17[0-1][0-9]{3})|(172[0-7][0-9]{2})|(172800)"
mainSteps:
- name: installToolsForPowerShell
  action: aws:runPowerShellScript
  inputs:
    runCommand: 
      - $progressPreference = 'SilentlyContinue'
      - Write-Host '---------------'
      - Write-Host 'Downloading AWS Tools'
      - Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force
      - Install-Module -Name AWSPowerShell -Force
      - Write-Host '---------------'
      - Write-Host 'Change ExecutionPolicy'
      - Set-ExecutionPolicy RemoteSigned -Force -Scope Process
      - Get-ExecutionPolicy
      - Write-Host '---------------'
      - Write-Host 'Import Tools for Powershell'
      - Import-Module AWSPowerShell
      - Get-AWSPowerShellVersion
      - Get-AWSPowerShellVersion -ListServiceVersionInfo
    timeoutSeconds: "{{executionTimeout}}"

コンテンツの貼り付けが終わったらページ下部の ドキュメントの作成 をクリックします。

オペレーション

作成したドキュメントを実行します。 左側メニューから ランコマンド をクリックします。

<コマンドの実行>画面で コマンドの実行 をクリックします。 前の手順で作成したドキュメントを探し、ラジオボタンにチェックを入れます。

ターゲットインスタンス から対象のインスタンスを選択します。

出力オプション で任意の S3 バケットを選択します。 Run Command の出力はコンソールでは2500文字しか表示されず、日本語が文字化けします。 エラー時の解析を容易にするため、及び、実行証跡を残すために必ず S3 バケットを出力するようにします。

実行 ボタンをクリックすると処理が実行されます。 実行の詳細が表示されるので成功することを確認します。

結果

指定した S3 バケットの保存されている実行ログファイルを確認してみます。 正しくインストールされているようです。

~~略~~
Import Tools for Powershell

AWS Tools for Windows PowerShell
Version 3.3.450.0
Copyright 2012-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.

Amazon Web Services SDK for .NET
Core Runtime Version 3.3.31.7
Copyright 2009-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.

Release notes: https://aws.amazon.com/releasenotes/PowerShell

This software includes third party software subject to the following copyrights:
- Logging from log4net, Apache License
[http://logging.apache.org/log4net/license.html]
~~略~~

さいごに

Run Command を活用した AWS Tools for PowerShell のインストール方法を紹介しました。 わずか2~3行のコマンドを打つとしても台数が増えればヒューマンエラーの可能性は上がっていきます。 極力人手は減らしていきたいと思います。

参考

AWS Tools for PowerShell AWS Tools for Windows PowerShell または AWS Tools for PowerShell Core のセットアップ