golang抓取AWS月度账单


import (
	"cloud-manager/common"
	"fmt"
	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/credentials"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/costexplorer"
	"strings"
)

type stubProvider struct {
	creds          credentials.Value
	retrievedCount int
	expired        bool
	err            error
}

func (s *stubProvider) Retrieve() (credentials.Value, error) {
	s.retrievedCount++
	s.expired = false
	s.creds.ProviderName = "stubProvider"
	return s.creds, s.err
}
func (s *stubProvider) IsExpired() bool {
	return s.expired
}
func GetCostAndUsageWithResources() {
	c := credentials.NewCredentials(&stubProvider{
		creds: credentials.Value{
			AccessKeyID:     "xx",
			SecretAccessKey: "xx",
			SessionToken:    "",
		},
		expired: true,
	})
	mySession := session.Must(session.NewSession())
	// Create a CostExplorer client from just a session.
	//svc := costexplorer.New(mySession)
	start := "2023-03-01" //开始时间只能是14天内
	end := "2023-03-19"
	svc := costexplorer.New(mySession, aws.NewConfig().WithCredentials(c))

	data, err := svc.GetCostAndUsageWithResources(&costexplorer.GetCostAndUsageWithResourcesInput{
		Granularity: common.String("MONTHLY"),
		Metrics: []*string{common.String("UnblendedCost"),
			common.String("BlendedCost"),
			common.String("AmortizedCost"),
			common.String("NetAmortizedCost"),
			common.String("NetUnblendedCost"),
			common.String("NormalizedUsageAmount"),
		},
		TimePeriod: &costexplorer.DateInterval{
			Start: &start,
			End:   &end,
		},
		Filter: &costexplorer.Expression{
			Dimensions: &costexplorer.DimensionValues{ //必须过滤,否则会报错
				Key:          common.String("SERVICE"),
				Values:       []*string{common.String("Amazon Elastic Compute Cloud - Compute")},
				MatchOptions: []*string{common.String("EQUALS")},
			},
		},
		GroupBy: []*costexplorer.GroupDefinition{
			{
				Type: common.String("DIMENSION"),
				Key:  common.String("SERVICE"),
			},
			{
				Type: common.String("DIMENSION"),
				Key:  common.String("RESOURCE_ID"),
			},
		},
	})
	if err != nil {
		fmt.Println("失败", err.Error())
	}

	fmt.Println(data.ResultsByTime)

}
func GetCostAndUsage() {
	c := credentials.NewCredentials(&stubProvider{
		creds: credentials.Value{
			AccessKeyID:     "xx",
			SecretAccessKey: "xxx",
			SessionToken:    "",
		},
		expired: true,
	})
	mySession := session.Must(session.NewSession())
	start := "2023-03-01"
	end := "2023-03-19"
	svc := costexplorer.New(mySession, aws.NewConfig().WithCredentials(c))

	data, err := svc.GetCostAndUsage(&costexplorer.GetCostAndUsageInput{
		Granularity: common.String("MONTHLY"),
		Metrics: []*string{common.String("UnblendedCost"),
			common.String("BlendedCost"),
			common.String("AmortizedCost"),
			common.String("NetAmortizedCost"),
			common.String("NetUnblendedCost"),
			common.String("NormalizedUsageAmount"),
		},
		TimePeriod: &costexplorer.DateInterval{
			Start: &start,
			End:   &end,
		},
		//Filter: &costexplorer.Expression{
		//	Dimensions: &costexplorer.DimensionValues{
		//		Key:          common.String("SERVICE"),
		//		Values:       []*string{common.String("Amazon Elastic Compute Cloud - Compute")},
		//		MatchOptions: []*string{common.String("EQUALS")},
		//	},
		//},
		GroupBy: []*costexplorer.GroupDefinition{
			{
				Type: common.String("DIMENSION"),
				Key:  common.String("SERVICE"),
			},
			{
				Type: common.String("DIMENSION"),
				Key:  common.String("RESOURCE_ID"),
			},
		},
	})
	if err != nil {
		fmt.Println("失败", err.Error())
	}

	fmt.Println(data.ResultsByTime)

}
//这里只写2个简单demo,具体查询条件和参数可根据官方文档进行修改

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值