Get All AWS Lambda Functions and Their Tags and Output to CSV

My bash script runs to retrieve lambda functions and their tags.

It runs ok and does what it needed to do, however I need to get the output written to a .txt or a .csv file, which needs to be in a readable format.

Below is the script I have;

while read -r name; do
    aws lambda list-functions | jq -r ".Functions[].FunctionArn" | xargs -I {} aws lambda list-tags --resource {} --query '{"{}":Tags}' --output text

Below is what a returned value looks like after the script runs;

ARN:AWS:LAMBDA:EU-WEST-1:193399999:FUNCTION:example-lambda     EXX   dev     example-lambda   False   release-1.1.9   False   True

I need to get all the items returned and lined up neatly in a txt or csv file. Any help would be appreciated.

3 answers

  • answered 2021-07-27 11:55 Andras Igaz

    I found this tutorial helpful. Based on what you written, you could pipe the output of your command into a csv, or concotanate into an array then write it into a file with a newline character at the end of each line.

  • answered 2021-07-27 12:11 Jens

    I would recommend to use the resourcegroupstaggingapi API to solve this problem. This API allows you to get all resources of a specific type and their tags.

    To get all your Lambda functions for your default region and their tags you can run the following command:

    aws resourcegroupstaggingapi get-resources --resource-type-filters "lambda"

    The output of this command can now be parsed with jq. The great thing about jq is that you can manipulate the output to be CSV.

    To get CSV output with two columns (ARN, Tags) you can run the following command:

    aws \
        resourcegroupstaggingapi \
        get-resources \
        --resource-type-filters "lambda" \
        | jq -r '.ResourceTagMappingList[] | [.ResourceARN, ((.Tags | map([.Key, .Value] | join("="))) | join(","))] | @csv'

    The advantage of this approach is that you only have a single HTTP call making it relatively fast. The disadvantage is that you only get the ARN and the tags.

  • answered 2021-07-27 12:55 Aleix Paytuví Gallart

    As shimo mentioned in a comment to the question, a way to save the output of a command to a file is using the > operator.

    > operator replaces the existing content of the file with the output of the command. If you want to save the output of multiple commands to the same file, you should use >> operator.

    You can also use a pipe and the command tee. The output will be printed in your screen and in a file, which will be at the end of the file.

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum