[Osaka/Yokohama] Looking for infrastructure/server side engineers!

[Osaka/Yokohama] Looking for infrastructure/server side engineers!

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Successor to CentOS] AlmaLinux OS server construction/migration service

[Successor to CentOS] AlmaLinux OS server construction/migration service

[For WordPress only] Cloud server “Web Speed”

[For WordPress only] Cloud server “Web Speed”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Reservation system development] EDISONE customization development service

[Reservation system development] EDISONE customization development service

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[Global exclusive service] Beyond's MSP in North America and China

[Global exclusive service] Beyond's MSP in North America and China

[YouTube] Beyond official channel “Biyomaru Channel”

[YouTube] Beyond official channel “Biyomaru Channel”

CloudFormationでVPC環境を作ってみよう(実行編)

皆さんこんにちは
システムソリューション部SREチーム所属の岡崎です。

前回に引き続き「CloudFormation」の簡単な使い方を紹介したいと思います。
今回は前回記入したテンプレートファイルから実際にVPCを構築してみたいと思います。

スタックを作成する

まず各自の開発サーバにログインして以下のようにCloudFormationを実行するユーザーのAPIキー登録をしてください。

[root@localhost ~]# aws configure
AWS Access Key ID [****************XXXX]:
AWS Secret Access Key [****************XXXX]:
Default region name [ap-northeast-1]:
Default output format [XXXX]:

もしawsコマンドがない場合は以下のようにインストールしてください。

[root@localhost ~]# yum install epel-release
[root@localhost ~]# yum install python-pip
[root@localhost ~]# pip install awscli

前回作成したvpc.ymlがあることを確認してから以下のコマンドで早速VPCを構築してみましょう。

[root@localhost ~]# ls -l
total 4
-rw-r--r--. 1 root root 1713 Mar 22 06:20 vpc.yml

[root@localhost ~]# aws cloudformation create-stack \
> --stack-name vpc \
> --region ap-northeast-1 \
> --template-body file://./vpc.yml

実行後、以下のようにエラーなく表示されれば作成処理が進行します。

arn:aws:cloudformation:ap-northeast-1:189461266018:stack/vpc/7b29dce0-4c70-11e9-8b3c-0ee87e6fb924

状況ついては以下のコマンドにて確認できます。
表示が「CREATE_COMPLETE」となれば、リソースが完成しています。

aws cloudformation  describe-stacks --stack-name vpc
STACKS  2019-03-28T02:12:32.683Z        False   False   arn:aws:cloudformation:ap-northeast-1:189461266018:stack/vpc/f23007a0-50fe-11e9-88b0-0e819627e6da       vpc     CREATE_COMPLETE
DRIFTINFORMATION        NOT_CHECKED
PARAMETERS      PublicSubnetCider       10.31.0.0/24
PARAMETERS      ProjectCode     test
PARAMETERS      VPCCider        10.31.0.0/16

それぞれのリソースの構築が完了されたかどうか確認してみましょう。

 

 

 

 

 

 

 

 

 

 

 

問題なく指定した識別子のVPCとサブネットが作成されてました。

リソースを削除する

では今回作成したリソースを一括で削除したいと思います。
手動で削除する場合はそれぞれのリソース画面に遷移してリソースごとに削除する必要がありますが、
CloudFormationで作成したリソースは単一のコンソールから削除できます。
開発サーバに戻り以下のコマンドを発行してください。

[root@localhost ~]# aws cloudformation delete-stack --stack-name vpc

さて削除の進捗を確認しましょう
以下のコマンドを実行し、「DELETE_IN_PROGRESS」となっていれば削除中ということになります。

[root@localhost ~]# aws cloudformation  describe-stacks --stack-name vpc
STACKS  2019-03-28T02:12:32.683Z        2019-03-28T02:20:53.902Z        False   False   arn:aws:cloudformation:ap-northeast-1:189461266018:stack/vpc/f23007a0-50fe-11e9-88b0-0e819627e6da       vpc     DELETE_IN_PROGRESS
DRIFTINFORMATION        NOT_CHECKED
PARAMETERS      PublicSubnetCider       10.31.0.0/24
PARAMETERS      ProjectCode     test
PARAMETERS      VPCCider        10.31.0.0/16

スタックの削除が完了すれば以下のようなエラー出力がでます。

[root@localhost ~]# aws cloudformation  describe-stacks --stack-name vpc

An error occurred (ValidationError) when calling the DescribeStacks operation: Stack with id vpc does not exist

では実際に削除が完了したかを確認しましょう。
VPCが消えていることが確認できました。

 

 

 

 

 

 

解説

今回のコマンドを1つ1つ解説すると以下のようになります。

# aws cloudformation create-stack \
  ↑ aws cloudformationでcloudformationを利用することを宣言
   create-stackにてスタック作成を命令、削除の場合はdelete-stack

> --stack-name vpc \
  ↑ stack-name [名前] スタックの名前を設定

> --region ap-northeast-1 \
  ↑ --region [リージョン] リソースを作成したいリージョンを設定

> --template-body file://./vpc.yml
  ↑ --template-body [ファイルURL] テンプレートファイルのURLを指定

このように最小限のコマンドでVPCを作成できます。
またこの他に前回設定したParametersの値を変更したい場合は以下のように書けます。

# aws cloudformation create-stack \
> --stack-name vpc \
> --region ap-northeast-1 \
> --template-body file://./vpc.yml
> --parameters \
> ParameterKey=ProjectCode,ParameterValue=test-beyondjapan \
> ParameterKey=VPCCider,ParameterValue="10.23.0.0/16" \
> ParameterKey=PublicSubnetCider,ParameterValue="10.23.0.0/24"

parametersにてそれぞれのパラメーターを使うことを宣言しParameterKeyにてパラメーターのID、ParameterValueにて値を指定できます。

まとめ

今回スタック、各リソースの作成と削除がコンソール上から実施できました。
このようにCloudFormationを利用すると簡単にAWSリソースの作成と削除ができます
前回と今回ではVPCの作成のみですが、次回EC2の作成など実践的な内容を紹介しますのでみなさんも利用してみてください。

この記事がお役に立てば【 いいね 】のご協力をお願いいたします!
0
読み込み中...
0 票, 平均: 0.00 / 10
538
X facebook はてなブックマーク pocket
[2024.6.30 CentOS support ended] CentOS server migration solution

[2024.6.30 CentOS support ended] CentOS server migration solution

[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[Osaka/Yokohama] Actively recruiting infrastructure engineers and server side engineers!

[Osaka/Yokohama] Actively recruiting infrastructure engineers and server side engineers!

The person who wrote this article

About the author

Junichiro Okazaki

Extensive experience in relocating and operating smartphone games.

He handles multi-cloud operations, server construction and relocation on a daily basis. As the number of cases has increased, I am considering how to improve the efficiency of my work. We often consider methods for relocating servers based on the merits of each cloud.

While we were relocating between clouds and from physical to cloud, we achieved two consecutive victories in a competition held by the Japan MSP Association.