LoginSignup
0
0

More than 5 years have passed since last update.

terraformを使ってAWSのauto recoveryを設定する

Posted at

バージョン

  • Terraform v0.7.0
  • AWSのドキュメントは 2016/08/11時点

コード

aws.tf
variable "aws_access_key" {}
variable "aws_secret_key" {}
variable "aws_default_region" {}
variable "subnet_id" {}
variable "security_groups" {}
variable "key_name" {}

variable "const" {
  default = {
    count = 2
  }
}
provider "aws" {
  access_key    = "${var.aws_access_key}"
  secret_key    = "${var.aws_secret_key}"
  region        = "${var.aws_default_region}"
}


resource "aws_instance" "web" {
  count         = "${lookup(var.const, "count")}"
  ami           = "ami-6869aa05"
  instance_type = "m3.medium"
  subnet_id     = "${var.subnet_id}"
  security_groups =  ["${split(",", var.security_groups)}"]
  monitoring = true
  key_name      = "${var.key_name}"
  tags {
    Name = "${format("web%02d", count.index + 1)}"
  }
}

# Cloud watch
# 1分ごとに2回失敗するとリカバリが走る
# http://docs.aws.amazon.com/ja_jp/AmazonCloudWatch/latest/monitoring/UsingAlarmActions.html
# namespace: https://docs.aws.amazon.com/ja_jp/AmazonCloudWatch/latest/DeveloperGuide/aws-namespaces.html
# dimenstion: http://docs.aws.amazon.com/ja_jp/AmazonCloudWatch/latest/monitoring/ec2-metricscollected.html#ec2-metric-dimensions
resource "aws_cloudwatch_metric_alarm" "recovery" {
  alarm_name            = "recovery ${format("web%02d", count.index + 1)}"
  comparison_operator   = "GreaterThanOrEqualToThreshold"
  evaluation_periods    = 1
  metric_name           = "StatusCheckFailed_System"
  namespace             = "AWS/EC2"
  period                = 60
  statistic             = "Minimum"
  threshold             = 2
  alarm_actions         = ["arn:aws:automate:${var.aws_default_region}:ec2:recover"]
  dimensions = {
    InstanceId = "${element(aws_instance.web.*.id, count.index)}"
  }
  count                 = "${lookup(var.const, "count")}"
  # テスト用
  insufficient_data_actions = ["arn:aws:automate:${var.aws_default_region}:ec2:recover"]
  # webに依存させる
  depends_on            = ["aws_instance.web"]
}

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