■
Chapter4
ハンズオンの実行
Chapter1で行った環境構築に加え、使用するソースコードをgithubからダウンロードする。
$ git clone https://github.com/tomomano/learn-aws-by-coding-source-code.git
を実行
ただし、Docker imageですでにダウンロードされているので今回はスルーできる
Docker imageからハンズオンを使用する場合
$ docker run -it tomomano/labc:latest
を実行するとインタラクティブシェルが"root@aws-handson"に変わる
SSHコマンドにふれる
$ ssh <user name>@<host name>
$ ssh -i Ec2SecretKey.pem <user name>@<host name>
のような形で使う
プログラムの実行
まずPythonの依存ライブラリをインストールする
$ cd handson/ec2-get-started
↑でディレクトリに移動し、venvから仮想環境を作成とインストールを行う
python3 -m venv .env source .env/bin/activate pip install -r requirements.txt
上記を実行することでPythonの環境構築が完了
EC2仮想サーバーの起動
$ export KEY_NAME="HirakeGoma" ←キーの名前は自由 $ aws ec2 create-key-pair --key-name $(KEY_NAME) --query 'KeyMaterial' --output text > ${KEY_NAME}.pem
を実行することでディレクトリ内にHirakeGoma.pemというファイルを生成
$ mv HirakeGoma.pem ~/.ssh/
$ chmod 400 -/.ssh/HirakeGoma.pem
上記のコマンドでファイルのアクセス権限を限定が可(400は所有者のみ操作可能)
AWSにアプリをデプロイ
$ cdk deploy -c key_name="HirakeGoma"
上記を実行することでHirakeGomaを鍵に指定
$ ssh -i ~/.ssh/HirakeGoma.pem ec2-user@<IP address>
-iで生成した秘密鍵を指定しEC2インスタンスにデフォルトで存在するec2-userを使用
画面が表示されればOK
スタックの削除
$ cdk destroy
上記のコマンドを実行するとスタックの削除が始まる
また、SSHの鍵も不要となるので
$ aws ec2 delete-key-pair --key-name "HirakeGoma" $ rm -f -/.ssh/HirakeGoma.pem
を実行することでec2から鍵を削除できる。
■
Chapter3
AWSの試運転
AWSのS3(無料で可)内に格納領域(バケット)を作成、以下のコマンドを出力
$ bucketName="mybucket-$(openss1 rand -hex 12)" $ echo $bucketName $ aws s3 mb "s3://${bucketname}" ←バケットの生成
(bucketnameは変数としての使用)
$ aws s3 ls
で作成したバケットの存在を確認
バケット内にファイルをアップデートするために
$ echo "Hello world" > hello_world.txt $ aws s3 cp hello_world.txt "s3://${bucketname}/hello_world.txt"
を実行
先程と同様にlsを使用してファイルの存在を確認
$ aws s3 rb "s3://${bucketname}" --force
■
Chapter2
ここはクラウドの概要となる
一般的にクラウドであると定義されるのは
- On‐demand self‐service:利用者のリクエストに応じた計算資源の自動割り当て
- Broad network access:ネットワークを通じたクラウドへのアクセス
- Resource pooling:計算資源の分割により複数の利用者へ資源を割り当て
- Rapid elasticity:リクエストに応じた計算資源の拡大, 縮小
- Measured service:計算資源利用量の計測,監視
以上5項目を満たしたハードウェア,ソフトウェアのことを指す
クラウドサービスの形態は次の3つ
- Software as a Service(SaaS):Google DriveやSlack
- Platform as a Service(PaaS):Google App EngineやHeroku
- Infrastructure as a Service(IaaS):AWS EC2など
■
Chapter1
chapter1は下準備の開発環境を整える回であった。
以下の7項目のインストールを行う。
・UNIX系コンソール
・Docker
・Node.js
・AWS CDK
・AWS認証鍵の設定
UNIX系コンソールのインストール
WindowsPowershellを管理者で起動
以下のコマンドをそれぞれ実行
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
成功と出力されるのを確認しPCを再起動
(Winndowsの「Windowsシステムツール」→「コントロールパネル」→「プログラム」→「Windowsの機能の有効化または無効化」から「Linux用Windowsサブシステム」と「仮想マシンプラットフォーム」にチェックを入れることでも可)
下のリンクからLinux kernel update packageをダウンロード
https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi
ダウンロードしたファイルを実行しダイアログにしたがってインストール
再びWindowsPowershellを起動
wsl --set-default-version 2
を実行しWSLのバージョンを指定する
Microsoftstoreを起動し、Ubuntu20.04.4をインストール
初回のみユーザー名とパスワードを設定
Dockerのインストール
Ubuntuを開き以下のコマンドを実行
$ curl -fsSL https://get.docker.com -o get-docker.sh $ sudo sh get-docker.sh $ sudo groupadd docker $ sudo usermod -aG docker $USER
問題なく実行できれば最後に
$ docker run hello-world
を実行し実行できればOK
Docker imageに存在するコマンド
$ docker run -it tomomano/labc:latest
を実行することで、これら3つを含めたプログラムをインストール
$ curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
を実行しファイルをダウンロード
$ unzip awscliv2.zip $ sudo ./aws/install
上記2コマンドを実行し無事に実行できたかの確認のため
$aws --version
を実行しバージョンが出力されればOK
$ aws configure
で初期設定を開始
その際にAWSのシークレットキーの情報が必要となるので、自身のAWSアカウントから「セキュリティ認証情報」→「新しいキーの作成」よりキーを作成
取得したキーの情報を入力
region nameはap-northeast-3, output formatはjsonを入力