Home > Enterprise >  How do I add all resource arn's? Lambda Golang ListTags
How do I add all resource arn's? Lambda Golang ListTags

Time:11-17

I am trying to list out all tags of my lambda functions, struggling a lot, please help me if anyone knows.

func main() {

    svc := lambda.New(session.New())

    input := &lambda.ListTagsInput{

        Resource: aws.String("arn:aws:lambda:us-east-1:657907747545:function-function-name"),
    

I'm expecting to list all tag arn's for my lambda functions

CodePudding user response:

You can use the following code:

package lambdautils

import (
    "context"
    awsutils "github.com/alessiosavi/GoGPUtils/aws"
    "github.com/aws/aws-sdk-go-v2/aws"
    "github.com/aws/aws-sdk-go-v2/service/lambda"
    "os"
    "sync"
)

var lambdaClient *lambda.Client = nil
var once sync.Once

func init() {
    once.Do(func() {
        cfg, err := awsutils.New()
        if err != nil {
            panic(err)
        }
        lambdaClient = lambda.New(lambda.Options{Credentials: cfg.Credentials, Region: cfg.Region})
    })
}

func ListLambdas() ([]string, error) {
    f, err := lambdaClient.ListFunctions(context.Background(), &lambda.ListFunctionsInput{})

    if err != nil {
        return nil, err
    }

    var functions = make([]string, len(f.Functions))
    for i, functionName := range f.Functions {
        functions[i] = *functionName.FunctionName
    }

    continuationToken := f.NextMarker
    for continuationToken != nil {
        f, err = lambdaClient.ListFunctions(context.Background(), &lambda.ListFunctionsInput{Marker: continuationToken})
        if err != nil {
            return nil, err
        }
        continuationToken = f.NextMarker
        for _, functionName := range f.Functions {
            functions = append(functions, *functionName.FunctionName)
        }
    }
    return functions, nil
}

func DescribeLambda(lambdaName string) (*lambda.GetFunctionOutput, error) {
    function, err := lambdaClient.GetFunction(context.Background(), &lambda.GetFunctionInput{FunctionName: aws.String(lambdaName)})
    if err != nil {
        return nil, err
    }
    return function, nil
}

func ListTags(lambdaARN string) (*lambda.ListTagsOutput, error) {
    return lambdaClient.ListTags(context.Background(), &lambda.ListTagsInput{
        Resource: aws.String(lambdaARN),
    })
}

Then you can use the ListLambdas method in order to list all your lambda. After, you can iterate the slice returned and call the DescribeLambda method in order to get the lambdaARN, then you can call the ListTags.

You can refer to the following repository in order to understand how to work with AWS (lambda, S3, glue etc etc) in Golang: https://github.com/alessiosavi/GoGPUtils/tree/master/aws

  • Related