Search code examples
pythonamazon-web-servicesamazon-ec2lambdadevops

Ec2 register target using lamda but getting error


i am facing a chalenge if one of the given ec2 instance id is wrong then test run under lambda is not working and error ( i-9876277sgshj) is not in running state or not exists. So if an ec2 instance id was wrong why didn't it registere the correct ec2 instance(i-26377gdhdhj) .please help if any ec2 instance id is wrong it will skip it and register correct ec2 (i-26377gdhdhj) . And how can i get the result of the script when it executed.

Here is the lambda function code python code.

Import boto3

response_tg = clients.register_targets( TargetGroupArn='arn:aws:elasticloadbalancing:us-east-1:123456789123:targetgroup/target-demo/c64e6bfc00b4658f',

Targets=[
   {
    'Id': 'i-26377gdhdhj',
   },
   {
    'Id': 'i-9876277sgshj',
   }
]

)


Solution

  • One of the solution might be, add a feature into your python script wihch verifies/gets the instance id of ec2-instance on the fly. For this, first of all assign a unique tag to all ec2-instance which you are planning to register in TG. Then use ec2 list & describe function of boto3 for getting all instance ids, and then register it into the TG.