如何添加所有资源ARN?Lambda Golang ListTags

huangapple go评论99阅读模式
英文:

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

问题

我正在尝试列出我的 Lambda 函数的所有标签,但遇到了很多困难,请问有人可以帮忙吗?

func main() {

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

	input := &lambda.ListTagsInput{

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

我希望能够列出我的 Lambda 函数的所有标签 ARN。

英文:

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

答案1

得分: 1

你可以使用以下代码:

package main

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),
	})
}

然后,你可以使用ListLambdas方法列出所有的 Lambda 函数。然后,你可以迭代返回的切片,并调用DescribeLambda方法获取lambdaARN,然后可以调用ListTags方法。

你可以参考以下存储库,了解如何在 Golang 中使用 AWS(lambda、S3、glue 等):https://github.com/alessiosavi/GoGPUtils/tree/master/aws

英文:

You can use the following code:

package main

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

huangapple
  • 本文由 发表于 2022年11月16日 19:07:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/74459420.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定