I'm trying to get all the thing names that are part of a specific thing group in AWS IoT Core using a Python Lambda function.
I checked the Boto3 documentation looking for a function that retrieves the names of things inside a specific thing group, but I couldn't find anything that does exactly that.
Is there a way to fetch all the thing names from a thing group at once and store them in a list?
You can use the BOTO3 client to retrieve IoT things in a thing group. Here is the Python code. You need to use this Python code in an AWS Lambda function to address your use case.
For additional AWS code examples, refer to the AWS Code Library -- where you will find thousands of examples in various SDKs, CLI, etc.
import boto3
def list_things_in_group(group_name, region='us-east-1'):
client = boto3.client('iot', region_name=region)
try:
response = client.list_things_in_thing_group(
thingGroupName=group_name,
recursive=False # Set to True if you want to include child groups
)
things = response.get('things', [])
if not things:
print(f"No things found in group: {group_name}")
else:
print(f"Things in group '{group_name}':")
for thing_name in things:
print(f"- {thing_name}")
describe_thing(client, thing_name)
except client.exceptions.ResourceNotFoundException:
print(f"Thing group '{group_name}' not found.")
except Exception as e:
print(f"Error: {e}")
def describe_thing(client, thing_name):
response = client.describe_thing(thingName=thing_name)
print(f" Thing Name: {response.get('thingName')}")
print(f" Thing ARN: {response.get('thingArn')}")
print()
# Example usage:
if __name__ == "__main__":
list_things_in_group("YourThingGroupName")