AWSAWS Systems Manageraws_ssm_maintenance_window_taskPro

How to Import an AWS SSM Maintenance Window Task into Terraform

To import an existing AWS SSM Maintenance Window Task into Terraform, scan it with Terraback and run terraback aws import --method bulk. Terraback writes the matching aws_ssm_maintenance_window_task resource block and the Terraform 1.5+ import block for you, so you do not have to run terraform import by hand. The import ID is the maintenance window ID and task ID joined by a slash, WINDOW-ID/TASK-ID (for example, mw-0c50858d01EXAMPLE/4f9adf6e-cd0a-4eb8-9d4d-3c3cEXAMPLE).

Import AWS SSM Maintenance Window Task with Terraback (recommended)

Terraback reverse-engineers your live infrastructure: it reads the AWS Systems Manager resource with read-only credentials, generates the HCL, and produces the exact import block. Two commands take you from a live AWS SSM Maintenance Window Task to managed Terraform.

1

Scan your AWS account

terraback scan all aws --region us-east-1
2

Generate import blocks and import into state

terraback aws import --method bulk

The Terraform import block

Terraback emits a Terraform 1.5+ import block like the one below. Because the block lives in your configuration, the import is reviewable in a pull request and repeatable across environments.

import {
  to = aws_ssm_maintenance_window_task.example
  id = "mw-0c50858d01EXAMPLE/4f9adf6e-cd0a-4eb8-9d4d-3c3cEXAMPLE"
}

Example aws_ssm_maintenance_window_task configuration

Here is a realistic AWS SSM Maintenance Window Task block. Terraback generates a fuller version from your actual resource attributes; this is a minimal, valid starting point.

resource "aws_ssm_maintenance_window_task" "example" {
  window_id        = "mw-0c50858d01EXAMPLE"
  task_type        = "RUN_COMMAND"
  task_arn         = "AWS-RunShellScript"
  priority         = 1
  service_role_arn = "arn:aws:iam::123456789012:role/MaintenanceWindowRole"
  max_concurrency  = "2"
  max_errors       = "1"

  targets {
    key    = "WindowTargetIds"
    values = ["23639a0b-ddbc-4bca-9e72-3c3cEXAMPLE"]
  }
}

Gotchas when importing a AWS SSM Maintenance Window Task

  • A task is a child of an aws_ssm_maintenance_window; the import ID joins the window ID and the task ID with a slash, and the parent window must already exist.
  • task_invocation_parameters is a deeply nested block keyed by task_type (RUN_COMMAND, AUTOMATION, LAMBDA, STEP_FUNCTIONS); it is not fully returned on read, so expect to fill it in to match the live task.
  • targets can reference window target IDs via the WindowTargetIds key or select resources directly; mismatching them with the registered targets causes the task to run against nothing.
  • max_concurrency and max_errors are strings (they may be counts or percentages like \"50%\"); supplying integers causes a type error on apply.

Doing it manually with terraform import

The native approach is to write the aws_ssm_maintenance_window_task block by hand, then run terraform import aws_ssm_maintenance_window_task.example <import-id> for every resource, one at a time. That works for a handful of resources, but it does not scale: you author all the HCL yourself and repeat the command for each item. Terraback generates the HCL and the import blocks for your whole account in one pass.

Import your whole AWS account in minutes

Terraback scans 80+ AWS resource types and emits clean Terraform plus import blocks, running locally with read-only credentials. $499 once, no SaaS.