Skip to content

Commit 6f5ddf2

Browse files
committed
Add lambda functionality
Signed-off-by: Kyle Solie <kyle.solie@gmail.com>
1 parent 161eb96 commit 6f5ddf2

8 files changed

+122
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
- support for elbv2 with basic probes and action
1515
- support for asg with basic probes
1616
- fix asg probe to support pagination
17+
- support for lambda with basic probes and action
1718

1819
### Changed
1920

chaosaws/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -181,5 +181,7 @@ def load_exported_activities() -> List[DiscoveredActivities]:
181181
activities.extend(discover_actions("chaosaws.elbv2.actions"))
182182
activities.extend(discover_probes("chaosaws.elbv2.probes"))
183183
activities.extend(discover_probes("chaosaws.asg.probes"))
184+
activities.extend(discover_actions("chaosaws.awslambda.actions"))
185+
activities.extend(discover_probes("chaosaws.awslambda.probes"))
184186

185187
return activities

chaosaws/awslambda/__init__.py

Whitespace-only changes.

chaosaws/awslambda/actions.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# -*- coding: utf-8 -*-
2+
from chaoslib.exceptions import FailedActivity
3+
from chaoslib.types import Configuration, Secrets
4+
5+
from chaosaws import aws_client
6+
from chaosaws.types import AWSResponse
7+
8+
__all__ = ["put_function_concurrency", "delete_function_concurrency"]
9+
10+
11+
def put_function_concurrency(function_name: str,
12+
concurrent_executions: int,
13+
configuration: Configuration = None,
14+
secrets: Secrets = None) -> AWSResponse:
15+
"""
16+
Throttles Lambda by setting reserved concurrency amount.
17+
"""
18+
client = aws_client("lambda", configuration, secrets)
19+
if not function_name:
20+
raise FailedActivity(
21+
"you must specify the lambda function name"
22+
)
23+
try:
24+
return client.put_function_concurrency(
25+
FunctionName=function_name,
26+
ReservedConcurrentExecutions=concurrent_executions
27+
)
28+
except Exception as x:
29+
raise FailedActivity(
30+
"failed throttling lambda function '{}': '{}'".format(
31+
function_name, str(x)))
32+
33+
34+
def delete_function_concurrency(function_name: str,
35+
configuration: Configuration = None,
36+
secrets: Secrets = None) -> AWSResponse:
37+
"""
38+
Removes concurrency limit applied to the specified Lambda
39+
"""
40+
client = aws_client("lambda", configuration, secrets)
41+
return client.delete_function_concurrency(FunctionName=function_name)

chaosaws/awslambda/probes.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# -*- coding: utf-8 -*-
2+
from chaoslib.types import Configuration, Secrets
3+
from chaosaws import aws_client
4+
5+
__all__ = ["get_function_concurrency"]
6+
7+
8+
def get_function_concurrency(function_name: str, configuration:
9+
Configuration = None,
10+
secrets: Secrets = None) -> bool:
11+
"""
12+
Get configuration information of lambda by its function name
13+
"""
14+
client = aws_client("lambda", configuration, secrets)
15+
result = client.get_function(FunctionName=function_name)
16+
return result["Concurrency"]["ReservedConcurrentExecutions"]

requirements-dev.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
coverage
2-
moto
2+
moto==1.3.4
33
pycodestyle
44
pytest>=2.8
55
pytest-cov
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# -*- coding: utf-8 -*-
2+
from unittest.mock import MagicMock, patch
3+
4+
import pytest
5+
from chaoslib.exceptions import FailedActivity
6+
7+
from chaosaws.awslambda.actions import put_function_concurrency
8+
from chaosaws.awslambda.actions import delete_function_concurrency
9+
10+
11+
@patch('chaosaws.awslambda.actions.aws_client', autospec=True)
12+
def test_aws_lambda_put_function_concurrency(aws_client):
13+
client = MagicMock()
14+
aws_client.return_value = client
15+
lambda_function_name = 'my-lambda-function'
16+
concurrency = 0
17+
put_function_concurrency(lambda_function_name, concurrency)
18+
client.put_function_concurrency.assert_called_with(FunctionName=lambda_function_name,
19+
ReservedConcurrentExecutions=concurrency)
20+
21+
@patch('chaosaws.awslambda.actions.aws_client', autospec=True)
22+
def test_aws_lambda_put_function_concurrency_empty_string(aws_client):
23+
client = MagicMock()
24+
aws_client.return_value = client
25+
lambda_function_name = ''
26+
concurrency = 0
27+
with pytest.raises(FailedActivity):
28+
put_function_concurrency(lambda_function_name, concurrency)
29+
30+
31+
@patch('chaosaws.awslambda.actions.aws_client', autospec=True)
32+
def test_aws_lambda_put_function_concurrency_failedactivity(aws_client):
33+
client = MagicMock()
34+
aws_client.return_value = client
35+
lambda_function_name = 'my-lambda-function'
36+
concurrency = 0
37+
38+
with patch.object(client, 'put_function_concurrency', FailedActivity):
39+
with pytest.raises(Exception):
40+
put_function_concurrency(lambda_function_name, concurrency)
41+
42+
43+
@patch('chaosaws.awslambda.actions.aws_client', autospec=True)
44+
def test_aws_lambda_delete_function_concurrency(aws_client):
45+
client = MagicMock()
46+
aws_client.return_value = client
47+
lambda_function_name = 'my-lambda-function'
48+
delete_function_concurrency(lambda_function_name)
49+
client.delete_function_concurrency.assert_called_with(FunctionName=lambda_function_name)
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# -*- coding: utf-8 -*-
2+
from unittest.mock import MagicMock, patch
3+
from chaosaws.awslambda.probes import get_function_concurrency
4+
5+
6+
@patch('chaosaws.awslambda.probes.aws_client', autospec=True)
7+
def test_aws_lambda_get_function_concurrency(aws_client):
8+
client = MagicMock()
9+
aws_client.return_value = client
10+
lambda_function_name = 'my-lambda-function'
11+
get_function_concurrency(lambda_function_name)
12+
client.get_function.assert_called_with(FunctionName=lambda_function_name)

0 commit comments

Comments
 (0)