1
0

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 5 years have passed since last update.

AWS EC2 AutoRecoveryをPowerShellで設定する

Last updated at Posted at 2019-01-08

#はじめに
PowerShellでDimensionの渡し方がわからなかったのでメモ。

#コード
##New-ObjectでDimension
Write-CWMetricAlarmのヘルプでは
-Dimension Amazon.CloudWatch.Model.Dimension[]
となっているのでそのとおりに作って渡す方式。

  • $Dimension.Valueを目的のインスタンスIDに変更すれば事足りますが、
    EvaluationPeriodとThresholdは要件に応じて変更しましょう。

  • 設定対象とするリージョンを変更する場合はAlarmActionにリージョン名が入っているので此処も変更します。

$Region = "ap-northeast-1"
$Dimension = New-Object 'Amazon.CloudWatch.Model.Dimension'
$Dimension.Name = 'InstanceId'
$Dimension.Value = 'i-xxxxxxxxxxxxxxxxx'

Write-CWMetricAlarm `
 -AlarmName 'AutoRecovery' `
 -AlarmAction "arn:aws:automate:${Region}:ec2:recover" `
 -AlarmDescription 'AutoRecovery Alarm' `
 -MetricName 'StatusCheckFailed_System' `
 -Namespace 'AWS/EC2' `
 -Statistic Maximum `
 -Dimension $Dimension `
 -Period 60 `
 -EvaluationPeriod 2 `
 -Threshold 1 `
 -ComparisonOperator GreaterThanOrEqualToThreshold `
 -Region $Region

##SplattingでDimension

PowerShellならではの書き方。

$InstanceId = "i-xxxxxxxxxxxxxxxxx"
$Region = "ap-northeast-1"
$Param = @{
  AlarmName          = "AutoRecovery";
  AlarmAction        = "arn:aws:automate:${Region}:ec2:recover";
  AlarmDescription   = "AutoRecovery Alarm";
  MetricName         = "StatusCheckFailed_System";
  Namespace          = "AWS/EC2";
  Statistic          = "Maximum";
  Dimension          = @(
    @{
      Name  = "InstanceId";
      Value = $InstanceId;
    }
  );
  Period             = 60;
  EvaluationPeriod   = 2;
  Threshold          = 1;
  ComparisonOperator = "GreaterThanOrEqualToThreshold";
  Region             = $Region;
}
Write-CWMetricAlarm @Param
1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?