1. Answers
  2. Creating an AWS SSM Association with Pulumi (via Terraform)

How do I build an AWS SSM association?

To build an AWS Systems Manager (SSM) association, you’ll need to create an association between a Systems Manager document and your target instance(s). This process allows you to centrally manage and automate the tasks that you want to perform on your instances.

We’ll go through the following steps to set this up:

  1. Create an AWS SSM Document that specifies the actions you want to run on your instances.
  2. Create an AWS SSM Association that links the document to the EC2 instance(s).

Detailed Breakdown of What We Will Do:

  1. Create an SSM Document: This document will define what actions should be taken on the instance(s).
  2. Create an SSM Association: This will bind the document to the specified instances, ensuring the actions are executed as needed.
  3. Define Output: Export relevant information that may be needed for validation or further usage.

Code Explanation:

Here is how you can achieve this:

import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";

// Create an SSM Document
const example = new aws.ssm.Document("example", {
    name: "example-doc",
    documentType: "Command",
    content: `{
  "schemaVersion": "2.2",
  "description": "Check disk space on instance",
  "mainSteps": [
    {
      "action": "aws:runShellScript",
      "name": "runShellScript",
      "inputs": {
        "runCommand": ["df -h"]
      }
    }
  ]
}
`,
});
// Create an SSM Association
const exampleAssociation = new aws.ssm.Association("example", {
    name: example.name,
    targets: [{
        key: "InstanceIds",
        values: ["i-1234567890abcdef0"],
    }],
});
export const ssmDocumentName = example.name;
export const ssmAssociationId = exampleAssociation.id;

Key Points:

  1. AWS Provider Configuration: This is necessary to specify the region where you want the resources to be created.
  2. SSM Document: Defines the actions or commands to run on your instances.
  3. SSM Association: Binds the document to one or more instances.
  4. Outputs: Provides the SSM document name and association ID for reference.

Summary:

In this example, we demonstrated how to create an AWS SSM document and associate it with an EC2 instance. This setup allows you to automate and manage tasks on your instances by associating predefined documents with them. This is particularly useful for maintaining consistent configurations and performing routine tasks efficiently.

Deploy this code

Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.

Sign up

New to Pulumi?

Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.

Sign up