Skip to content

How to print the value from a StringList parameter in an SSM Automation Document

0

I have a StringList parameter in a Runbook, and I want to assign it to a variable of type String. What is the best way to do this? I have tried many ways from the web, but all have failed. My problem is that in the SNS notification, the instanceIds do not come up; just the variable name gets printed.

In SNS Notifiation it looks like : Message from requester: Stopping instance(s): {{ instanceId | join(', ') }} I expect it to show the value of the parameter: Stopping instance(s): i-0d7193149e634d705

Code is shown below.

schemaVersion: '0.3'
description: Stops EC2 instance on approval
assumeRole: arn:aws:iam::123456789:role/ssm-document-role
parameters:
  instanceId:
    type: StringList
    description: The ID of the EC2 instance to stop
  ApproverSnsTopicArn:
    type: String
    description: Notification SNS ARN
    default: arn:aws:sns:us-east-1:123456789:automation-approval-topic
  Message:
    type: String
    description: Message for Approver
  Approver:
    type: String
    description: Approver ARN
    default: arn:aws:iam::123456789:user/ssm-approver
mainSteps:
  - name: Approve
    action: aws:approve
    nextStep: ChangeInstanceState
    isEnd: false
    inputs:
      NotificationArn: '{{ ApproverSnsTopicArn }}'
      Message: 'Stopping instance(s): {{ instanceId | join(', ') }}'
      Approvers:
        - arn:aws:iam::123456789:user/ssm-approver
  - name: ChangeInstanceState
    action: aws:changeInstanceState
    isEnd: true
    inputs:
      DesiredState: stopped
      InstanceIds: '{{ instanceId }}'

asked a year ago588 views

2 Answers
0

To print the value from a StringList parameter in an SSM Automation Document and assign it to a variable of type String, you can use the join filter in your Automation runbook. However, it seems there's a small issue in your current implementation.

In your SNS notification message, you're using:

Message: 'Stopping instance(s): {{ instanceId | join('', '') }}'

The join filter is being used incorrectly here. To properly join the elements of the StringList and convert it to a String, you should use:

Message: 'Stopping instance(s): {{ instanceId | join(', ') }}'

This will join the elements of the instanceId StringList with a comma and space separator.

For your specific case, where you want to display the instance IDs in the SNS notification, you can modify the Approve step as follows:

- name: Approve
  action: aws:approve
  nextStep: ChangeInstanceState
  isEnd: false
  inputs:
    NotificationArn: '{{ ApproverSnsTopicArn }}'
    Message: 'Stopping instance(s): {{ instanceId | join(', ') }}'
    Approvers:
      - '{{ Approver }}'

This should correctly display the instance IDs in your SNS notification message.

Also, make sure that when you're passing the instanceId parameter to your Automation execution, you're providing it as a proper StringList (comma-separated list of instance IDs).

If you need to use the joined string in other parts of your Automation document, you can create a variable to store it:

variables:
  instanceIdString:
    type: String
    default: '{{ instanceId | join(', ') }}'

Then you can reference this variable elsewhere in your document using {{ instanceIdString }}.

Remember that variables in Automation documents (schema version 0.3) can be updated during the execution, unlike parameters which remain static. This allows for more flexibility in handling and manipulating your data throughout the Automation workflow.
Sources
Data elements and parameters - AWS Systems Manager
Creating a Parameter Store parameter using Tools for Windows PowerShell - AWS Systems Manager

answered a year ago

  • This approach is already tried and does not work. In fact it makes the runbook content invalid. AI is also confused about the SSM Parameter store here.

0

Thank you for your query regarding StringList parameters in SSM Automation documents. The issue you're experiencing can be resolved with a small modification to your document. Here's the working solution:

schemaVersion: '0.3'
description: Stops EC2 instance on approval
assumeRole: arn:aws:iam::123456789:role/ssm-document-role
parameters:
  instanceId:
    type: StringList
    description: The ID of the EC2 instance to stop
  ApproverSnsTopicArn:
    type: String
    description: Notification SNS ARN
    default: arn:aws:sns:us-east-1:123456789:automation-approval-topic
  Message:
    type: String
    description: Message for Approver
  Approver:
    type: String
    description: Approver ARN
    default: arn:aws:iam::123456789:user/ssm-approver
mainSteps:
  - name: Approve
    action: aws:approve
    nextStep: ChangeInstanceState
    isEnd: false
    inputs:
      NotificationArn: '{{ ApproverSnsTopicArn }}'
      Message: 'Stopping instance(s): {{ instanceId | join(", ") }}'
      Approvers:
        - '{{ Approver }}'
  - name: ChangeInstanceState
    action: aws:changeInstanceState
    isEnd: true
    inputs:
      DesiredState: stopped
      InstanceIds: '{{ instanceId }}'

The key changes are:

  1. In the Message input, use the join function with {{ instanceId | join(", ") }} to properly format the instance IDs in the SNS notification
  2. In the ChangeInstanceState step, directly reference the StringList parameter with InstanceIds: '{{ instanceId }}'

This will ensure that:

  • Your SNS notification displays the instance IDs correctly (e.g., "Stopping instance(s): i-1234567890, i-0987654321")
  • The instance state change action receives the instance IDs in the correct format

Let me know if you need any clarification or have additional questions.

AWS
EXPERT

answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.