Resource: aws_ssm_association

Associates an SSM Document to an instance or EC2 tag.

Example Usage

Create an association for a specific instance

resource "aws_ssm_association" "example" {
  name = aws_ssm_document.example.name

  targets {
    key    = "InstanceIds"
    values = [aws_instance.example.id]
  }
}

Create an association for all managed instances in an AWS account

To target all managed instances in an AWS account, set the key as "InstanceIds" with values set as ["*"]. This example also illustrates how to use an Amazon owned SSM document named AmazonCloudWatch-ManageAgent.

resource "aws_ssm_association" "example" {
  name = "AmazonCloudWatch-ManageAgent"

  targets {
    key    = "InstanceIds"
    values = ["*"]
  }
}

Create an association for a specific tag

This example shows how to target all managed instances that are assigned a tag key of Environment and value of Development.

resource "aws_ssm_association" "example" {
  name = "AmazonCloudWatch-ManageAgent"

  targets {
    key    = "tag:Environment"
    values = ["Development"]
  }
}

Create an association with a specific schedule

This example shows how to schedule an association in various ways.

resource "aws_ssm_association" "example" {
  name = aws_ssm_document.example.name

  # Cron expression example
  schedule_expression = "cron(0 2 ? * SUN *)"

  # Single-run example
  # schedule_expression = "at(2020-07-07T15:55:00)"

  # Rate expression example
  # schedule_expression = "rate(7 days)"

  targets {
    key    = "InstanceIds"
    values = [aws_instance.example.id]
  }
}

Argument Reference

This resource supports the following arguments:

Output Location (output_location) is an S3 bucket where you want to store the results of this association:

Targets specify what instance IDs or tags to apply the document to and has these keys:

Attribute Reference

This resource exports the following attributes in addition to the arguments above:

Import

In Terraform v1.5.0 and later, use an import block to import SSM associations using the association_id. For example:

import {
  to = aws_ssm_association.test-association
  id = "10abcdef-0abc-1234-5678-90abcdef123456"
}

Using terraform import, import SSM associations using the association_id. For example:

% terraform import aws_ssm_association.test-association 10abcdef-0abc-1234-5678-90abcdef123456