2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

terraform import: ECS / Application Auto Scaling

Last updated at Posted at 2022-04-04

TerraformでECSのApplication Auto Scaling(ターゲット追跡スケーリングポリシー)のimportに苦戦したのでメモ
以前試みたときに格闘の末あきらめたのだが、さっき再挑戦してみたら5分でできた..
できてみるとこんなもんかという感じ

Terraformは既存のリソースからimportコマンドで簡単に定義を抽出できるが、リソースによってimportで指定する文字列がやたらと長いものがあって難儀
別にimportでやらなくてもいいのだが、できそうなのにできないのが気持ち悪いので解決したかった。ちなみにAppAutoScalingは定義が簡単なので自分で定義してもいいんだけど、リソースによっては自分で定義するのはつらそうなものがある(WAFとかCodePipeline)

import対象のApplication Auto Scaling

image.png

グレー部分はそれぞれリソース名の接頭辞が入る
※作成者のIAMのARN以外

リファレンス

Resource: aws_appautoscaling_target
Resource: aws_appautoscaling_policy

import構文

$ terraform import aws_appautoscaling_target.test-target service-namespace/resource-id/scalable-dimension
$ terraform import aws_appautoscaling_policy.test-policy service-namespace/resource-id/scalable-dimension/policy-name

結論

環境固有のリソース名部分は大文字で記載

terraform import aws_appautoscaling_target.ecs ecs/service/CLUSTER-NAME/SERVICENAME/ecs:service:DesiredCount
terraform import aws_appautoscaling_policy.ecs ecs/service/CLUSTER-NAME/SERVICENAME/ecs:service:DesiredCount/SCALING-POLICY-NAME

Windowsで実行したが、エスケープ文字(円マーク/クオーテーション/ダブルクォーテーション)などは不要

解説

service-namespace/resource-id/scalable-dimensionとも、AWSのAPIリファレンス
に説明がある(とTerraformのリファレンスにも書いてある)

ServiceNamespace
Valid Values: ecs

ResourceId
ECS service - The resource type is service and the unique identifier is the cluster name and service name. Example: service/default/sample-webapp.

ScalableDimension
ecs:service:DesiredCount - The desired task count of an ECS service.

各指定値はスラッシュ区切りだが、resource-idとポリシー用のscalable-dimension内もスラッシュ区切りなので境目が分かりづらい
TerraformのリファレンスにもECSの場合のサンプルが記載されてるので参考にする

import後の定義

terraform state showで出力後、以下のように修正する

  • aws_appautoscaling_target
    • id, role_arnをコメントアウト
      • role_arnをコメントアウトするか任意だけどappautoscaling用のは長いので管理したくない感じ
  • aws_appautoscaling_policy
    • arn, idをコメントアウト
resource "aws_appautoscaling_target" "ecs" {
  # id                 = "service/CLUSTER-NAME/SERVICE-NAME"
  max_capacity       = 4
  min_capacity       = 1
  resource_id        = "service/${aws_ecs_cluster.cluster.name}/${aws_ecs_service.service.name}"
  # role_arn           = "arn:aws:iam::123456789012:role/aws-service-role/ecs.application-autoscaling.amazonaws.com/AWSServiceRoleForApplicationAutoScaling_ECSService"
  scalable_dimension = "ecs:service:DesiredCount"
  service_namespace  = "ecs"
}

resource "aws_appautoscaling_policy" "ecs" {
  # arn                = "arn:aws:autoscaling:ap-northeast-1:123456789012:scalingPolicy:44f7e5c4-c5d5-4e54-afc1-1fe8f8df8387:resource/ecs/service/CLUSTER-NAME/SERVICE-NAME:policyName/SCALING-POLICY-NAME"
  # id                 = "SCALING-POLICY-NAME"
  name               = "SCALING-POLICY-NAME"
  policy_type        = "TargetTrackingScaling"
  resource_id        = "service/${aws_ecs_cluster.cluster.name}/${aws_ecs_service.service.name}"
  scalable_dimension = "ecs:service:DesiredCount"
  service_namespace  = "ecs"

  target_tracking_scaling_policy_configuration {
    disable_scale_in   = false
    scale_in_cooldown  = 0
    scale_out_cooldown = 0
    target_value       = 75

    predefined_metric_specification {
      predefined_metric_type = "ECSServiceAverageCPUUtilization"
    }
  }
}
2
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?