LoginSignup
2
1

More than 1 year has passed since last update.

amplify-cliをインストールしてローカルでAmplifyコマンドを実行出来る様にする

Posted at

aws-cli等を経由してローカルのPCでAmplifyを利用した開発の環境を構築する方法をまとめます。
この記事ではaws-cliとamplify-cliをインストールしてローカルでaws,amplify系のコマンドを実行出来る様にするまでの手順をまとめました。

環境

名前 バージョン
Mac OS 12.2.1

aws-cliのインストール

ローカル環境でawsのサービスを利用する為にaws-cliをインストールします。

aws-cliのv2系はGUIでインストーラーをダウンロードするのが手っ取り早いです。
v1系で差支えなければpip経由でインストール出来ます。

pyenvのインストール

pipを利用する為にpyenvをインストールします。

$ git clone https://github.com/pyenv/pyenv.git ~/.pyenv

~ $ pyenv --version
pyenv 2.2.0-5-g54889eb8

~/.bash_profileの設定

# pyenv
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init --path)"

~/.bashrcの設定

# pyenv
eval "$(pyenv init -)"

読み込み

$ source ~/.bash_profile

インストールバージョンのリストの確認

$ pyenv versions
* system (set by $HOME/.pyenv/version)

$ pyenv install --list

指定したバージョンのインストール

$ pyenv install 3.9.7
$ pyenv global 3.9.7
$ pyenv local 3.9.7

$ python --version
Python 3.9.7
$ which python
$HOME/.pyenv/shims/python

pipでaws-cliのインストール

$ pip install awscli

$ aws --version
aws-cli/1.21.7 Python/3.9.7 Darwin/19.6.0 botocore/1.22.7

これでaws-cliを利用出来る様になります。

.awsの確認

*既にAWSコンソール上でプロファイルが作成済みである事想定です。

*.awsディレクトリ未作成作成の場合はディレクトリの作成とパーミッションを付与する必要があります。

$ cd ~/
$ mkdir .aws
$ chmod 766 .aws
$ cd ~/.aws
$ touch credentials

下記の様な形でプロファイルの設定を記載します。

$ cat ~/.aws/credentials
[default]
aws_access_key_id=xxxxxxxxxxxxxxxxxxxx
aws_secret_access_key=xxxxxxxxxxxxxxxxxxxx

[profile_name]
aws_access_key_id=xxxxxxxxxxxxxxxxxxxx
aws_secret_access_key=xxxxxxxxxxxxxxxxxxxx

$ cat ~/.aws/config
[default]
region=xxxxxxxxxxxxxxxxxxxx
output=json

[profile profile_name]
region=xxxxxxxxxxxxxxxxxxxx

プロファイルの確認

$ aws configure list
      Name                    Value             Type    Location
      ----                    -----             ----    --------
   profile                <not set>             None    None
access_key     ****************XXXX shared-credentials-file
secret_key     ****************XXXX shared-credentials-file
    region           xx-xxxxxxxxx-1      config-file    ~/.aws/config

プロファイル名の指定

$ aws configure list --profile profile_name
      Name                    Value             Type    Location
      ----                    -----             ----    --------
   profile            profile_name           manual    --profile
access_key     ****************XXXX shared-credentials-file
secret_key     ****************XXXX shared-credentials-file
    region           xx-xxxxxxxxx-1      config-file    ~/.aws/config

プロファイルの切り替え

複数のプロファイルを有している場合、~/.bash_profileに下記を追記する事でデフォルトのプロファイルを設定する事が出来ます。

$ vim ~/.bash_profile

# aws-cli
export AWS_PROFILE=profile_name

$ source ~/.bash_profile

オプションの指定無しでプロファイルの確認

$ aws configure list
      Name                    Value             Type    Location
      ----                    -----             ----    --------
   profile            profile_name           manual    --profile
access_key     ****************XXXX shared-credentials-file
secret_key     ****************XXXX shared-credentials-file
    region           xx-xxxxxxxxx-1      config-file    ~/.aws/config

amplify-cliの設定

ローカルでamplifyを利用する為にamplify-cliをインストールする必要があります。

amplify-cliのインストール

$ npm install -g @aws-amplify/cli

$ amplify -v
6.3.1

configureの設定(AWSアカウントの紐付け)

Amplify用のプロファイルが未作成の場合はamplify configureコマンドでAWSのマネジメントコンソールを開きつつ新しいIAMユーザーを作成することが出来ます。

下記のコマンドを実行後、マネジメントコンソールが開かれる為

$ amplify configure

ユーザー詳細の設定AWSアクセスの種類の設定アクセス権限の設定タグの設定を行います。

  • AWSアクセスの種類→プログラムによるアクセスのみを選択する。
  • アクセス権限の設定→AdministratorAccessAdministratorAccess-Amplifyを設定する。
  • タグは任意

作成後にaccess_keyなどをローカルに設定してプロファイル情報を保存します。

amplify configureコマンドを実行中の場合、引き続き対話形式でキーの設定を入力する事で.aws/config.aws/credentialsを更新する事が出来ます。

$ amplify configure
Follow these steps to set up access to your AWS account:

Sign in to your AWS administrator account:
https://console.aws.amazon.com/
Press Enter to continue

Specify the AWS Region
? region:  ap-northeast-1
Specify the username of the new IAM user:
? user name:  amplify-test-user
Complete the user creation using the AWS console
xxxxxxxxxx
Press Enter to continue

Enter the access key of the newly created user:
? accessKeyId:  ********************
? secretAccessKey:  ****************************************
This would update/create the AWS Profile in your local machine
? Profile Name:  profile_name

Successfully set up the new user.

これでローカルでawsコマンドやamplifyコマンドを実行出来る様になりました。

次回はamplifyコマンドを利用して既存のプロジェクトにamplifyのリソースを紐づけて行きます。

2
1
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
2
1