0
0

More than 3 years have passed since last update.

PowerShell から amazon/aws-cli の Docker イメージを起動して AWS に接続する

Last updated at Posted at 2020-04-06

AWS CLI の Docker イメージが公開されたので、Docker for Windows + PowerShell という環境から、これを利用する手順について確認しました。

参考:

動作環境は以下の通りです。

PS > docker --version
Docker version 19.03.6, build 369ce74a3c

PS > $PSVersionTable

Name                           Value
----                           -----
PSVersion                      6.2.4
PSEdition                      Core
GitCommitId                    6.2.4
OS                             Microsoft Windows 10.0.18363
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Docker イメージの参照

まずは Docker イメージを pull します。

PS > docker pull amazon/aws-cli
Using default tag: latest
latest: Pulling from amazon/aws-cli
a7583ef20c9d: Pull complete 
7d4c8ec0c058: Pull complete 
cd590779e8ea: Pull complete 
a37a98af08a9: Pull complete 
0a765160c849: Pull complete 
Digest: sha256:7a27c26c2937a3d0b84171675709df1dc09aa331e86cad90f74ada6df7b59c89
Status: Downloaded newer image for amazon/aws-cli:latest
docker.io/amazon/aws-cli:latest

docker run により、CLI ツールのバージョンが返ってくることを確認します。

PS > docker run --rm -ti amazon/aws-cli --version
aws-cli/2.0.6 Python/3.7.3 Linux/4.15.0-91-generic botocore/2.0.0dev10

PowerShell 関数として定義

以降のコマンドを簡略化するため、コマンドを関数として定義します。まずは PowerShell プロファイルのパスを確認します。

PS > echo $PROFILE
C:\Users\__USER_NAME__\Documents\PowerShell\Microsoft.PowerShell_profile.ps1

プロファイルを編集し、以下の記述を追加します。

function aws {
   $location = Get-Location
   docker run --rm -v ${HOME}/.aws:/root/.aws -v ${location}:/aws amazon/aws-cli $Args
}

PowerShell を再起動し、関数が利用できることを確認します。

PS > aws --version
aws-cli/2.0.6 Python/3.7.3 Linux/4.15.0-91-generic botocore/2.0.0dev10

AWS への接続設定

次に、AWS 接続用の設定ファイルを生成します。

PS > aws configure
AWS Access Key ID [None]: AKIA****************
AWS Secret Access Key [None]: ****************************************
Default region name [None]: ap-northeast-1
Default output format [None]: json

これにより、以下の設定ファイルが生成されたことを確認できます。

PS > ls $HOME\.aws


    Directory: C:\Users\__USER_NAME__\.aws

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        2020/04/06    16:59             48 config
-a----        2020/04/06    16:59            116 credentials

チュートリアルに合わせ、S3 上のディレクトリを参照してみます。

PS > aws s3 ls
2020-03-31 00:47:53 test

S3 上にバケットが存在していれば、その一覧が応答で返ってきます。これにより、AWS サービスとの接続が確認できました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0