Captain Jack
Captain Jack

Reputation: 140

YAML lists and variables

I am trying to deregister EC2 instances from target groups using Automation document in SSM, which I am attempting to write in YAML but I am having major issues with getting my head around YAML lists and arrays.

Here are the relevant parts of the code:

parameters:
  DeregisterInstanceId:
    type: StringList
    description: (Required) Identifies EC2 instances for patching
    default: ["i-xxx","i-yyy"]

Further down I am trying to read this DeregisterInstanceId as a list but it's not working - getting various errors regarding expected one type of variable but received another.

name: RemoveLiveInstancesFromTG
action: aws:executeAwsApi
inputs:
  Service: elbv2
  Api: DeregisterTargets
  TargetGroupArn: "{{ TargetGroup }}"
  Targets: "{{ DeregisterInstanceId }}"
isEnd: true

What Targets input really needs to look like, is like this:

Targets:
    - Id: "i-xxx"
    - Id: "i-yyy"

...but I am not sure how to pass my StringList to create the above.

I tried:

Targets:
   - Id: "{{ DeregisterInstanceId }}"

and

Targets:
   Id: "{{ DeregisterInstanceId }}"

But no go.

Upvotes: 1

Views: 1624

Answers (1)

Christopher Bonitz
Christopher Bonitz

Reputation: 856

I used to have the exact same problem although I created the document in json. Please checkout the following working script to de-register an instance from a load balancer target group

Automation document v. 74

{
  "description": "LoadBalancer deregister targets",
  "schemaVersion": "0.3",
  "assumeRole": "{{ AutomationAssumeRole }}",
  "parameters": {
    "TargetGroupArn": {
      "type": "String",
      "description": "(Required) TargetGroup of LoadBalancer"
    },
    "Target": {
      "type": "String",
      "description": "(Required) EC2 Instance(s) to deregister"
    },
    "AutomationAssumeRole": {
      "type": "String",
      "description": "(Optional) The ARN of the role that allows Automation to perform the actions on your behalf.",
      "default": ""
    }
  },
  "mainSteps": [
    {
      "name": "DeregisterTarget",
      "action": "aws:executeAwsApi",
      "inputs": {
        "Service": "elbv2",
        "Api": "DeregisterTargets",
        "TargetGroupArn": "{{ TargetGroupArn }}",
        "Targets": [
          {
            "Id": "{{ Target }}"
          }
        ]
      }
    }
  ]
}

Obviously the point of interest is the targets parameter, it needs an json array to work (forget about the cli format, it seems to need json).

It also allows for specifying multiple targets and also allows usage of ports and availability groups, but all I need it for is to choose one instance and pull it out.

Hope it might be of use for someone.

Upvotes: 2

Related Questions