-
Notifications
You must be signed in to change notification settings - Fork 522
/
lambda_function.go
200 lines (171 loc) 路 6.75 KB
/
lambda_function.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package aws
import (
"context"
"math"
"strconv"
"github.com/infracost/infracost/internal/resources"
"github.com/infracost/infracost/internal/schema"
"github.com/infracost/infracost/internal/usage"
"github.com/infracost/infracost/internal/usage/aws"
"github.com/shopspring/decimal"
)
type LambdaFunction struct {
Address string
Region string
Name string
MemorySize int64
Architecture string
StorageSize int64
RequestDurationMS *int64 `infracost_usage:"request_duration_ms"`
MonthlyRequests *int64 `infracost_usage:"monthly_requests"`
}
func (a *LambdaFunction) CoreType() string {
return "LambdaFunction"
}
func (a *LambdaFunction) UsageSchema() []*schema.UsageItem {
return []*schema.UsageItem{
{Key: "request_duration_ms", DefaultValue: 0, ValueType: schema.Int64},
{Key: "monthly_requests", DefaultValue: 0, ValueType: schema.Int64},
}
}
func (a *LambdaFunction) UsageEstimationParams() []schema.UsageParam {
return []schema.UsageParam{
{Key: "memory_size_gb", Value: decimal.NewFromInt(a.MemorySize).Div(decimal.NewFromInt(1024)).String()},
}
}
func (a *LambdaFunction) PopulateUsage(u *schema.UsageData) {
resources.PopulateArgsWithUsage(a, u)
}
func (a *LambdaFunction) BuildResource() *schema.Resource {
memorySize := decimal.NewFromInt(a.MemorySize)
storageSize := decimal.NewFromInt(a.StorageSize)
requestType := "AWS-Lambda-Requests"
durationType := "AWS-Lambda-Duration"
storageType := "AWS-Lambda-Storage-Duration"
averageRequestDuration := decimal.NewFromInt(1)
if a.RequestDurationMS != nil {
averageRequestDuration = decimal.NewFromInt(*a.RequestDurationMS)
}
if a.Architecture == "arm64" {
requestType = "AWS-Lambda-Requests-ARM"
durationType = "AWS-Lambda-Duration-ARM"
storageType = "AWS-Lambda-Storage-Duration-ARM"
}
var monthlyRequests *decimal.Decimal
var gbSeconds *decimal.Decimal
var storageGBSeconds *decimal.Decimal
var costComponents []*schema.CostComponent
if a.MonthlyRequests != nil {
monthlyRequests = decimalPtr(decimal.NewFromInt(*a.MonthlyRequests))
}
costComponents = append(costComponents, &schema.CostComponent{
Name: "Requests",
Unit: "1M requests",
UnitMultiplier: decimal.NewFromInt(1000000),
MonthlyQuantity: monthlyRequests,
ProductFilter: &schema.ProductFilter{
VendorName: strPtr("aws"),
Region: strPtr(a.Region),
Service: strPtr("AWSLambda"),
ProductFamily: strPtr("Serverless"),
AttributeFilters: []*schema.AttributeFilter{
{Key: "group", Value: strPtr(requestType)},
{Key: "usagetype", ValueRegex: strPtr("/Request/")},
},
},
UsageBased: true,
},
)
// Defaults to x86 tiers
gbRequestTiers := []int{6000000000, 9000000000, 15000000000}
displayNameList := []string{"Duration (first 6B)", "Duration (next 9B)", "Duration (over 15B)"}
if a.Architecture == "arm64" {
gbRequestTiers = []int{7500000000, 11250000000, 18750000000}
displayNameList = []string{"Duration (first 7.5B)", "Duration (next 11.25B)", "Duration (over 18.75B)"}
}
if a.MonthlyRequests != nil {
monthlyRequests = decimalPtr(decimal.NewFromInt(*a.MonthlyRequests))
gbSeconds = decimalPtr(calculateGBSeconds(memorySize, averageRequestDuration, *monthlyRequests))
storageGBSeconds = decimalPtr(calculateStorageGBSeconds(storageSize, *gbSeconds))
gbSecondQuantities := usage.CalculateTierBuckets(*gbSeconds, gbRequestTiers)
costComponents = append(costComponents, a.storageCostComponent(storageGBSeconds, storageType))
costComponents = append(costComponents, a.durationCostComponent(displayNameList[0], "0", &gbSecondQuantities[0], durationType))
if gbSecondQuantities[1].GreaterThan(decimal.NewFromInt(0)) {
costComponents = append(costComponents, a.durationCostComponent(displayNameList[1], strconv.Itoa(gbRequestTiers[0]), &gbSecondQuantities[1], durationType))
}
if gbSecondQuantities[2].GreaterThanOrEqual(decimal.NewFromInt(0)) {
costComponents = append(costComponents, a.durationCostComponent(displayNameList[2], strconv.Itoa(gbRequestTiers[2]), &gbSecondQuantities[2], durationType))
}
} else {
costComponents = append(costComponents, a.storageCostComponent(nil, storageType))
costComponents = append(costComponents, a.durationCostComponent(displayNameList[0], "0", gbSeconds, durationType))
}
estimate := func(ctx context.Context, values map[string]interface{}) error {
inv, err := aws.LambdaGetInvocations(ctx, a.Region, a.Name)
if err != nil {
return err
}
values["monthly_requests"] = int64(math.Round(inv))
dur, err := aws.LambdaGetDurationAvg(ctx, a.Region, a.Name)
if err != nil {
return err
}
values["request_duration_ms"] = int64(math.Round(dur))
return nil
}
return &schema.Resource{
Name: a.Address,
UsageSchema: a.UsageSchema(),
CostComponents: costComponents,
EstimateUsage: estimate,
}
}
func calculateGBSeconds(memorySize decimal.Decimal, averageRequestDuration decimal.Decimal, monthlyRequests decimal.Decimal) decimal.Decimal {
gb := memorySize.Div(decimal.NewFromInt(1024))
seconds := averageRequestDuration.Ceil().Div(decimal.NewFromInt(1000)) // Round up to closest 1ms and convert to seconds
return monthlyRequests.Mul(gb).Mul(seconds)
}
func calculateStorageGBSeconds(storageSize decimal.Decimal, gbSeconds decimal.Decimal) decimal.Decimal {
storage := storageSize.Sub(decimal.NewFromInt(512)).Div(decimal.NewFromInt(1024))
return storage.Mul(gbSeconds)
}
func (a *LambdaFunction) durationCostComponent(displayName string, usageTier string, quantity *decimal.Decimal, durationType string) *schema.CostComponent {
return &schema.CostComponent{
Name: displayName,
Unit: "GB-seconds",
UnitMultiplier: decimal.NewFromInt(1),
MonthlyQuantity: quantity,
ProductFilter: &schema.ProductFilter{
VendorName: strPtr("aws"),
Region: strPtr(a.Region),
Service: strPtr("AWSLambda"),
ProductFamily: strPtr("Serverless"),
AttributeFilters: []*schema.AttributeFilter{
{Key: "group", Value: strPtr(durationType)},
{Key: "usagetype", ValueRegex: strPtr("/GB-Second/")},
},
},
PriceFilter: &schema.PriceFilter{
StartUsageAmount: strPtr(usageTier),
},
UsageBased: true,
}
}
func (a *LambdaFunction) storageCostComponent(quantity *decimal.Decimal, storageType string) *schema.CostComponent {
return &schema.CostComponent{
Name: "Ephemeral storage",
Unit: "GB-seconds",
UnitMultiplier: decimal.NewFromInt(1),
MonthlyQuantity: quantity,
ProductFilter: &schema.ProductFilter{
VendorName: strPtr("aws"),
Region: strPtr(a.Region),
Service: strPtr("AWSLambda"),
ProductFamily: strPtr("Serverless"),
AttributeFilters: []*schema.AttributeFilter{
{Key: "group", Value: strPtr(storageType)},
{Key: "usagetype", ValueRegex: strPtr("/GB-Second/")},
},
},
}
}