List AWS Resources by Tags
Posted on:
I have been working on a script for ScriptKit that will list lambda functions on my account and allow me to choose one to invoke. Though, I wanted to only list ones that I wanted to be executable by ScriptKit. I am setting these up by adding AWS Tags to each Lambda function to have a executableByScriptKit
tag set to true
. I previously was using the listFunctions command as part of the AWS Lambda client in the JS v3 SDK, but said command does not include tags. I could go one by one with the GetFunctionCommand, but that then is increasing the HTTP requests the more lambda functions on my account.
Luckily for me, AWS has an API to view & tag resources with the AWS Resource Groups Tagging API, in particular, the GetResourcesCommand. As seen in the code snippet below, I request to grab all resources that have the resource type of lambda:function
and include my executableByScriptKit
tag.
import {
GetResourcesCommand,
ResourceGroupsTaggingAPIClient,
} from "@aws-sdk/client-resource-groups-tagging-api";
const tagClient = new ResourceGroupsTaggingAPIClient({ region: "us-east-1" });
let resources = await tagClient.send(
new GetResourcesCommand({
ResourceTypeFilters: ["lambda:function"],
TagFilters: [{ Key: "executableByScriptKit", Values: ["true"] }],
})
);
const executableLambdas = resources.ResourceTagMappingList.map((resource) =>
resource.ResourceARN.split(":").pop()
);
From here, the command returns a listing of resources which includes the ARNs of each which I then can pass along to the lambda client to be invoked. As well, this endpoint is free of charge as the service this endpoint is part of, AWS Resource Explorer, is free.