はじめに
皆さんこんにちは、奥平です。
今回はECS環境を簡単に構築できるという「AWS Copilot」を触ってみたのでQiita記事に上げます。
環境
・AWS Cloud9(AWS Copilot実行用として)
- インスタンスタイプ:t3.large (※処理が重いので少し高めのスペック)
- OS:Amazon Linux 2
- デフォルトのVPC、Subnetで大丈夫です
- アタッチするEC2ロール作成
・AmazonSSMFullAccess
・AWSCloudFormationFullAccess
・AWSCodeCommitFullAccess
・AWSCloudMapFullAccess
・AmazonEC2FullAccess
・AmazonECS_FullAccess
・AWSLambdaFullAccess
・AmazonEC2ContainerRegistryFullAccess
・AWSCodePipelineFullAccess
・SecretsManagerReadWrite
・※インラインポリシー1
・※インラインポリシー2
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": "sts:AssumeRole",
"Resource": "*"
}
]
}
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": [
"iam:GetRole",
"iam:TagRole",
"iam:CreateRole",
"iam:DeleteRole",
"iam:AttachRolePolicy",
"iam:PutRolePolicy",
"iam:PassRole",
"sts:AssumeRole",
"iam:DetachRolePolicy",
"iam:CreateServiceLinkedRole",
"iam:DeleteRolePolicy",
"codebuild:DeleteProject",
"sts:GetCallerIdentity",
"iam:GetRolePolicy"
],
"Resource": "*"
}
]
}
結論
AWS Copilotでは下記図のようなAWS構成をコマンドで作成する事が出来ます。
VPC、ECS、CI/CDまで作成してくれる優れ物です。
メリット/デメリット
メリットも多いですが、やはりある程度運用を意識したり、知識等が必要だと思いました。
AWS Copilot | |
---|---|
メリット | ・VPC環境を簡単に構築出来る ・ECS環境(ECR、Fargate、ALB)を簡単に構築出来る ・CI/CD環境を簡単に構築出来る |
デメリット | ・運用面について懸念点・考慮点が多い(※調査中) ・場合によって、CI/CD設定知識をある程度要する(CodeBuild、CodeDeploy、CodePipeline環境の調整が必要) |
AWS Copilot用語
-
Application(app)
最初に、AWS Copilotの本体「app」を作成
※コマンド「Copilot init」、または「Copilot app init」
主に、サイトのサービス名となる(例:soumu-app、keiri-app等) -
Environment(env)
・AWS Copilotで作成した「app」が動くVPCや、CI/CDの環境
【VPC構築】
「env」には『prod』、『dev』、『stg』、『test』の4つが使用できる
※コマンド【copilot env init --name prod】 → prod用のVPC構築
※コマンド【copilot env init --name dev】 → dev用のVPC構築
※「既存のVPC選択」や、「VPC作成→CIDR指定」も可能です
-
Service(svc)
・「env」にデプロイするECS環境(Webサービス、Batch処理等) -
pipeline
【CI/CD構築】
上記で用意した「env」、「svc」に対し、CI/CDを作成する
それでは作っていきましょう
1.Cloud9設定
Cloud9設定 EC2ロールを読み込ませるため、「AWS managed temporary credentials」を「x」にする。
・AWSコンフィグ設定
vim ~/.aws/config
[default]
region = ap-northeast-1
output = json
role_arn = <※今回作成したEC2ロールのARN>
credential_source = Ec2InstanceMetadata
2.AWS Copilotをインストール
$ sudo su -
# curl -Lo /usr/local/bin/copilot https://github.com/aws/copilot-cli/releases/latest/download/copilot-linux && chmod +x /usr/local/bin/copilot && copilot --version
copilot version: v1.7.1 ※2021年6月時点
# exit
3.サンプルソースをgit cloneする
$ git clone https://github.com/aws-samples/aws-copilot-sample-service example
$ cd example
4.app作成
※「oku-app」の「oku」は、奥平のokuです。
$ copilot app init
What would you like to name your application? [? for help] oku-app
5.env作成
$ copilot env init --name dev
Which credentials would you like to use to create test? [Use arrows to move, type to filter, ? for more help]
> [profile default] ※ロールを読み込ませる
[Use arrows to move, type to filter] ※devとしての「env」環境を作成
> Yes, use default. ※新規でVPC作成、今回は新規でVPCを作成する
Yes, but I'd like configure the default resources (CIDR ranges). ※新規でVPCを作成するが、CIDRを指定できる
No, I'd like to import existing resources (VPC, subnets). ※既存のVPCを指定できる
6.svc作成
$ copilot svc init
Which service type best represents your service's architecture? [Use arrows to move, type to filter, ? for more help]
> Load Balanced Web Service ※今回はALBでFargateをデプロイする構成設定
What do you want to name this Load Balanced Web Service? [? for help] oku-svc-web ※サービス名を入力
Which Dockerfile would you like to use for oku-svc-web? [Use arrows to move, type to filter, ? for more help]
> ./Dockerfile ※直下のDockerfileを選択
※manifest.ymlファイルが作成されます。
$ copilot svc deploy
※「env」環境の(dev)へデプロイ開始~
※もし複数「env」環境がある場合は下記のように選択するようになります。
Select an environment [Use arrows to move, type to filter]
> dev ※dev環境へデプロイ
prod
stg
7.ECS構築完了
✔ Deployed oku-svc-web, you can access it at http://oku-a-Publi-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.ap-northeast-1.elb.amazonaws.com.
※ECS構築完了
8.CI/CD作成
※事前に、CodeCommitでリポジトリ作成します ※名前(例):copilot-test-commit
※先程「3.サンプルソースをgit cloneする」でcloneしたリポジトリがセットされているので、CodeCommitリポジトリに変更する。
$ git remote -v
origin https://github.com/aws-samples/aws-copilot-sample-service (fetch)
origin https://github.com/aws-samples/aws-copilot-sample-service (push)
※先程のサンプルソースのURL
$ git remote set-url origin https://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/copilot-test-commit
$ git remote -v
origin https://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/copilot-test-commit (fetch)
origin https://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/copilot-test-commit (push)
※ヘルパー認証
$ git config --global credential.helper '!aws codecommit credential-helper $@'
$ git config --global credential.UseHttpPath true
9.CI/CD構築完了
$ copilot pipeline init
Which environment would you like to add to your pipeline? [Use arrows to move, type to filter, ? for more help]
> dev ※devの「env」環境を選択
[No additional environments]
Which repository would you like to use for your pipeline? [Use arrows to move, type to filter, ? for more help]
> https://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/copilot-test-commit
※buildspec.ymlが作成される
※pipeline.ymlが作成される
ソースをCodeCommitへプッシュします。
$ git add .
$ git commit -m "test"
$ git push -u origin master
$ copilot pipeline update
今後はpushするだけで、自動CI/CDが実行されます。
10.環境確認
$ copilot app ls
oku-app
$ copilot env ls
dev
$ copilot svc ls
oku-svc-web
$ copilot pipeline ls
pipeline-oku-app-copilot-test-commit
以上で、サンプルソースを使用して簡単なVPC、ECS、CI/CD構築を終了となります。
11.削除してお片付け
ALBとかFargateでお金が課金されますので、使用後は削除も忘れずに。
$ copilot pipeline delete
Are you sure you want to delete pipeline pipeline-oku-app-copilot-test-commit from application oku-app? Yes
✔ Deleted pipeline pipeline-oku-app-copilot-test-commit from application oku-app.
$ copilot svc delete
Only found one service, defaulting to: oku-svc-web
Are you sure you want to delete oku-svc-web from application oku-app? Yes
✔ Deleted service oku-svc-web from environment dev.
✔ Deleted resources of service oku-svc-web from application oku-app.
✔ Deleted service oku-svc-web from application oku-app.
Recommended follow-up actions:
- Run `copilot pipeline update` to update the corresponding pipeline if it exists.
$ copilot env delete
Only found one environment, defaulting to: dev
Are you sure you want to delete environment dev from application oku-app? Yes
✔ Deleted environment dev from application oku-app.
$ copilot app delete
Are you sure you want to delete application oku-app? Yes
✔ Cleaned up deployment resources.
✔ Deleted pipeline pipeline-oku-app-copilot-test-commit from application oku-app.
✔ Deleted application resources.
✔ Deleted application configuration.
おわりに
環境構築まではサンプルプログラム等を動かしてスムーズにいけましたが、やはり実運用で使っていくにはもっと調査が必要だと思います。
AWS Copilotは使いこなせたら絶対便利で構築・運用が楽になると信じていますので、引き続き実運用で使えるよう頑張ります。