今年もADACHIN SERVER LABOをよろしくお願いします!!
前回のブログで、ECSでFargateを使ってSSHコンテナを作りましたが、その際のクラスター、サービス、タスク定義など手動で設定しました。今回はTerraformで全てコード化したので参考にしてみてください。
■aws_ecs_cluster/aws_ecs_task_definition/aws_ecs_service
https://www.terraform.io/docs/providers/aws/r/ecs_cluster.html
https://www.terraform.io/docs/providers/aws/r/ecs_task_definition.html
https://www.terraform.io/docs/providers/aws/r/ecs_service.html
とりあえず手動で設定した箇所を目grepして作ればOK。今回はロードバランサーなど特殊な設定がないので、プライベートIPさえ設定されていれば完了となります。Fargateを使うにあたって、 IAM
, CloudWatch
も必要なので忘れずに。
■Terraform
- aws_ecs.tf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
## Cluster resource "aws_ecs_cluster" "adachin-devops" { name = "adachin-devops" } resource "aws_ecs_task_definition" "adachin-devops-task" { family = "adachin-devops" requires_compatibilities = ["FARGATE"] network_mode = "awsvpc" task_role_arn = "arn:aws:iam::${var.aws_account_id}:role/ecsTaskExecutionRole" execution_role_arn = "arn:aws:iam::${var.aws_account_id}:role/ecsTaskExecutionRole" cpu = 512 memory = 1024 container_definitions = "${file("files/task-definitions/devops.json")}" } resource "aws_ecs_service" "adachin-devops-service" { cluster = "${aws_ecs_cluster.adachin-devops.id}" deployment_minimum_healthy_percent = 50 deployment_maximum_percent = 200 desired_count = "${var.aws_ecs_service_desired_count_devops}" launch_type = "FARGATE" name = "adachin-devops-service" deployment_circuit_breaker { enable = true rollback = true } lifecycle { ignore_changes = [ "desired_count", "task_definition", ] } network_configuration { subnets = [ "${aws_subnet.adachin-app-1a.id}", ] security_groups = [ "${aws_security_group.adachin-devops.id}", ] } task_definition = "${aws_ecs_task_definition.adachin-devops-task.arn}" } |
- files/task-definitions/devops.json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
[ { "image": "xxxxxxxxx.dkr.ecr.ap-northeast-1.amazonaws.com/adachin-devops-pre", "logConfiguration": { "logDriver": "awslogs", "options": { "awslogs-group": "/ecs/app", "awslogs-region": "ap-northeast-1", "awslogs-stream-prefix": "ecs" } }, "cpu": 512, "memory": 1024, "mountPoints": [], "environment": [ { "name" : "RAILS_ENV", "value" : "pre" } ], "networkMode": "awsvpc", "name": "adachin-devops", "essential": true, "portMappings": [ { "hostPort": 22, "containerPort": 22, "protocol": "tcp" } ], "command": [ "/usr/bin/supervisord" ] } ] |
- aws_variables.tf
1 2 3 4 5 6 7 8 9 |
## common variable "aws_account_id" { default = "xxxxxxxxxxxx" } ## ECS variable "aws_ecs_service_desired_count_devops" { default = 1 } |
- aws_cloudwatch.tf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
resource "aws_cloudwatch_log_group" "ecs_devops" { name = "/ecs/devops" tags = { Environment = "pre" Application = "devops" } } resource "aws_cloudwatch_log_group" "ecs_app" { name = "/ecs/app" tags = { Environment = "pre" Application = "app" } } |
- aws_iam.tf
1 2 3 4 5 6 7 8 9 |
resource "aws_iam_role" "adachin-ecstaskexecution-role" { name = "ecsTaskExecutionRole" assume_role_policy = "${file("files/assume_role_policy/ecs-task.json")}" } resource "aws_iam_role_policy_attachment" "adachin-ecstaskexecutionrole-attach" { role = "${aws_iam_role.adachin-ecstaskexecution-role.name}" policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy" } |
- files/assume_role_policy/ecs-task.json
1 2 3 4 5 6 7 8 9 10 11 12 13 |
{ "Version": "2008-10-17", "Statement": [ { "Sid": "", "Effect": "Allow", "Principal": { "Service": "ecs-tasks.amazonaws.com" }, "Action": "sts:AssumeRole" } ] } |
■確認
1 2 3 4 5 6 7 8 9 |
$ ssh devops __ ____/ /__ _ ______ ____ _____ / __ / _ \ | / / __ \/ __ \/ ___/ / /_/ / __/ |/ / /_/ / /_/ (__ ) \__,_/\___/|___/\____/ .___/____/ /_/ Alpine Linux v3.11 container adachin@devops:~$ |
!!!
■まとめ
次はecs-deployについてブログします!
0件のコメント