aws cloudwatch event rule ssm automation

466 views
Skip to first unread message

Joey Jojo

unread,
Apr 26, 2019, 6:03:54 PM4/26/19
to Terraform
hello fellow Terraformers lol

i am trying to setup a rule like so:

aws_cloudwatch_event_rule_ssm_automation.png




























I wanna use SSM Automation and then it's own built in Document called AWS-StartEC2Instance or AWS-StopEC2Instance which seems pretty straight forward but for some reason I can't seem to set it up for the life of me. Here is my Terraform config:

provider "aws" {
  region  = "${var.aws_region}"
  profile = "${var.aws_profile}"
}

resource "aws_cloudwatch_event_rule" "on-rule" {
  name = "start-${var.hostname}"
  description = "start aws-mtl-jenkins every weekday at 7 am pacific time"
  schedule_expression = "cron(00 14 ? * MON-FRI *)"
  is_enabled = true
  
}

resource "aws_cloudwatch_event_target" "start-instances" {
  target_id = "AWS-StartEC2Instance"
  arn = "arn:aws:ssm:${var.aws_region}::automation-execution/AWS-StartEC2Instance"
  role_arn = "arn:aws:iam::213479268921:role/EC2-Stop-Reboot-Instances"

 run_command_targets {
   key = "InstanceIds"
   values = ["i-036349d05f15d81ce"]
 }
}

When I run my the Terraform Apply I don't get any errors but it switches it to use SSM Run Command like so:

aws_cloudwatch_event_rule_ssm_automation_2.png























What do I need to change so that my Terraform file will create the rule using SSM Automation and using the Document AWS-StartEC2Instance or AWS-StopEC2Instance.

Thank you very much




















Joey Jojo

unread,
May 3, 2019, 3:07:22 PM5/3/19
to Terraform
is there anybody out there??????? anyone at all???

K. Adam Wolfe

unread,
May 4, 2019, 12:14:58 AM5/4/19
to Terraform
I can't answer your question, per se, as I'm in the middle of my own headache, however when I run into stuff like this I find `terraform import` to be immensely helpful.  So have a go at creating it manually (as you've already done) then see the docs on importing that resource type.  Once you have it imported successfully, do a `terraform state show <resource_type.name` and it will dump all the args and attributes returned by the API.  From there it's just a matter of copypaste, trimming and prettifying.

Joey Jojo

unread,
May 10, 2019, 6:31:12 PM5/10/19
to Terraform
Thanks Adam! Definitely helps me go in the right direction :)

Joey Jojo

unread,
May 10, 2019, 7:47:27 PM5/10/19
to Terraform
Thanks Adam! That almost worked but then I got this error: 

resource aws_cloudwatch_event_target doesn't support import

So I guess there is no way I can do this :/ that really sucks


On Friday, May 3, 2019 at 9:14:58 PM UTC-7, K. Adam Wolfe wrote:
Reply all
Reply to author
Forward
0 new messages