LoginSignup
2
2

More than 3 years have passed since last update.

CloudFormationではじめる初めてのECS(with Blue/Green Deployment)

Last updated at Posted at 2021-02-20

はじめに

先日、2018年8月に書いた「LaravelアプリケーションをローカルでもAWSでもDockerで動かす」という記事が100 LGTMに到達しました。
2021年2月現在、私が書いた記事の中では、もっともLGTMされている記事でして、今年に入ってからもLGTMされていることから関心の高い内容なのかなと思っています。
とはいえ、3年近く経過している記事ではありますので、構築方法も含めアップデートしたいと思います。
この記事では、CloudFormationを使って、いくつかコマンドを叩くことで、短いステップでECS環境を構築していきます。

構築をはじめる前にやっておくこと

構築をはじめる前にやっておくことを以下に示します。

いざ、構築

あらかじめ、CloudFormationのテンプレートと簡単なアプリケーションをimunew/ecs-blue-green-demoにて用意しています。
リポジトリをforkし、git cloneしておきます。

$ git clone git@github.com:{forkしたアカウント}/ecs-blue-green-demo.git
$ cd ecs-blue-green-demo

以下、imunew/ecs-blue-green-demoを使って構築していきます。

1. VPCSubnet、...LoadBalancerの構築

以下のように、makeコマンドを実行していきます。

$ make deploy-vpc-subnet profile=$(profile)
$ make deploy-nat-instance profile=$(profile)
$ make deploy-network profile=$(profile)
$ make deploy-security-group profile=$(profile)
$ make deploy-load-balancer profile=$(profile)

2. ECRリポジトリの作成、およびDockerイメージのプッシュ

まずは、ECRリポジトリを作成します。

$ make deploy-ecs-ecr profile=$(profile)

次に、Dockerイメージをビルドし、ECRにプッシュします。

$ make push-docker-images profile=$(profile)

3. ECSクラスターとサービスの構築

ECSクラスターとサービスを構築します。

$ make deploy-ecs-cluster profile=$(profile)
$ make deploy-ecs-service profile=$(profile)

ECSサービスが正常に構築できていれば、その時点でLoadBalancerDNSからエンドポイントにアクセス可能となるはずです。
phpinfo()してるページが表示されるはずです。)

ecs_demo_app_screen.png

4. GitHubプライベートアクセストークンをSecrets Managerに登録する

access-tokenには、事前に作成しておいたプライベートアクセストークンを指定します。

$ make deploy-secrets-github profile=$(profile) access-token=$(access-token)

5. CodeDeployアプリケーションとデプロイグループの作成

CodeDeployアプリケーションとデプロイグループを作成します。

$ make deploy-code-deploy profile=$(profile)

6. CodePipelineの作成

CodePipelineを作成します。

$ make deploy-code-pipeline profile=$(profile)

Blue/Green デプロイ

フォークしたリポジトリに、コードをプッシュすると、下記のように、CodePipelineが動き出します。

code-pipeline.png

Deployステージの詳細リンクをクリックすると、Blue/Greenデプロイの詳細を下記のように確認できます。

code-deploy-blue-green.png

おわりに

ECS環境の構築はできたでしょうか?
フォークしたリポジトリを色々カスタマイズしたりして、自分なりのテンプレートを作ってみてください。
何か詰まったところがあったり、ご意見などありましたら、この記事にコメントいただくか、imunew/ecs-blue-green-demoissuepull-requestください。

ではでは。

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