-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enabling quotas for lambda functions
- Loading branch information
1 parent
a13a61b
commit 95ee580
Showing
3 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package servicequotas | ||
|
||
import ( | ||
"github.com/aws/aws-sdk-go/service/lambda" | ||
"github.com/aws/aws-sdk-go/service/lambda/lambdaiface" | ||
) | ||
|
||
var ( | ||
lambdaConcurrentExecutionsLimitName = "lambda_concurrent_executions_limit" | ||
lambdaConcurrentExecutionsLimitDesc = "Measures the maximum number of concurrent executions allowed for an AWS Lambda function." | ||
|
||
lambdaCodeSizeUnzippedLimitBytesName = "lambda_code_size_unzipped_limit_bytes" | ||
lambdaCodeSizeUnzippedLimitBytesDesc = "Measures the maximum size limit (in bytes) for the unzipped AWS Lambda function code." | ||
) | ||
|
||
// LambdaConcurrentExecutionsLimitCheck implements the UsageCheck interface | ||
// for limits for lambda functions | ||
type LambdaConcurrentExecutionsLimitCheck struct { | ||
client lambdaiface.LambdaAPI | ||
} | ||
|
||
// Usage returns the usage and quouta for the lambda concurrent executions and | ||
// lambda code size unzipped limits | ||
func (c *LambdaConcurrentExecutionsLimitCheck) Usage() ([]QuotaUsage, error) { | ||
param := &lambda.GetAccountSettingsInput{} | ||
var usages []QuotaUsage | ||
|
||
output, err := c.client.GetAccountSettings(param) | ||
if err != nil { | ||
return usages, err | ||
} | ||
|
||
usages = []QuotaUsage{ | ||
{ | ||
Name: lambdaConcurrentExecutionsLimitName, | ||
Description: lambdaConcurrentExecutionsLimitDesc, | ||
Quota: float64(*output.AccountLimit.ConcurrentExecutions), | ||
Usage: float64(*output.AccountUsage.FunctionCount), | ||
}, | ||
{ | ||
Name: lambdaCodeSizeUnzippedLimitBytesName, | ||
Description: lambdaCodeSizeUnzippedLimitBytesDesc, | ||
Quota: float64(*output.AccountLimit.CodeSizeUnzipped), | ||
Usage: float64(*output.AccountUsage.TotalCodeSize), | ||
}, | ||
} | ||
|
||
return usages, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package servicequotas | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go/service/lambda" | ||
"github.com/aws/aws-sdk-go/service/lambda/lambdaiface" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type mockedLambdaClient struct { | ||
lambdaiface.LambdaAPI | ||
Output *lambda.GetAccountSettingsOutput | ||
Err error | ||
} | ||
|
||
func (m *mockedLambdaClient) GetAccountSettings(input *lambda.GetAccountSettingsInput) (*lambda.GetAccountSettingsOutput, error) { | ||
return m.Output, m.Err | ||
} | ||
|
||
func TestLambdaConcurrentExecutionsLimitCheck_Usage(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
client lambdaiface.LambdaAPI | ||
output *lambda.GetAccountSettingsOutput | ||
err error | ||
expectedUsage []QuotaUsage | ||
expectedErr error | ||
}{ | ||
{ | ||
name: "success", | ||
client: &mockedLambdaClient{ | ||
Output: &lambda.GetAccountSettingsOutput{ | ||
AccountLimit: &lambda.AccountLimit{ | ||
ConcurrentExecutions: int64p(100), | ||
CodeSizeUnzipped: int64p(1000000), | ||
CodeSizeZipped: int64p(500000), | ||
}, | ||
AccountUsage: &lambda.AccountUsage{ | ||
FunctionCount: int64p(50), | ||
TotalCodeSize: int64p(500000), | ||
}, | ||
}, | ||
Err: nil, | ||
}, | ||
expectedUsage: []QuotaUsage{ | ||
{ | ||
Name: "lambda_concurrent_executions_limit", | ||
Description: "Measures the maximum number of concurrent executions allowed for an AWS Lambda function.", | ||
Quota: 100, | ||
Usage: 50, | ||
}, | ||
{ | ||
Name: "lambda_code_size_unzipped_limit_bytes", | ||
Description: "Measures the maximum size limit (in bytes) for the unzipped AWS Lambda function code.", | ||
Quota: 1000000, | ||
Usage: 500000, | ||
}, | ||
}, | ||
expectedErr: nil, | ||
}, | ||
{ | ||
name: "error", | ||
client: &mockedLambdaClient{ | ||
Output: nil, | ||
Err: errors.New("some error occurred"), | ||
}, | ||
expectedUsage: nil, | ||
expectedErr: errors.New("some error occurred"), | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
c := &LambdaConcurrentExecutionsLimitCheck{ | ||
client: test.client, | ||
} | ||
|
||
usages, err := c.Usage() | ||
|
||
assert.Equal(t, test.expectedErr, err) | ||
assert.Equal(t, test.expectedUsage, usages) | ||
}) | ||
} | ||
} | ||
|
||
func int64p(i int64) *int64 { | ||
return &i | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters