Sunday, October 9, 2022

[SOLVED] How to add two or more Target id under 'Id' : instance id in aws lambda function with python

Issue

I want to register/deregister two ec2 instance which is i-26377gdhdhj and i-9876277sgshj in aws alb target group using lambda function python script.

I want to know how to add both instance id under Targets id simultaneously.Please help.

This is my current script.

import boto3

clients=boto3.client('elbv2')

response_tg = clients.register_targets(
    TargetGroupArn='arn:aws:elasticloadbalancing:us-east-1:123456789123:targetgroup/target-demo/c64e6bfc00b4658f',
    Targets=[
       {
        'Id': 'i-26377gdhdhj',
       },
    ]
)

Solution

response_tg = clients.register_targets(
    TargetGroupArn='arn:aws:elasticloadbalancing:us-east-1:123456789123:targetgroup/target-demo/c64e6bfc00b4658f',
    Targets=[
       {
        'Id': 'i-26377gdhdhj',
       },
       {
        'Id': 'i-9876277sgshj',
       }
    ]
)

Since Targets is a list, you can pass them both in.



Answered By - Shawn
Answer Checked By - Marilyn (WPSolving Volunteer)