Skip to content

Commit 4514b1d

Browse files
committed
adding lambda functions
1 parent 4ed6eae commit 4514b1d

File tree

2 files changed

+162
-0
lines changed

2 files changed

+162
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from typing import TYPE_CHECKING, List, Optional, Union
2+
3+
from aibs_informatics_core.models.aws.core import AWSAccountId, AWSRegion
4+
from aibs_informatics_core.models.aws.lambda_ import LambdaFunctionName
5+
from botocore.exceptions import ClientError
6+
7+
from aibs_informatics_aws_utils.core import AWSService, get_client_error_code
8+
9+
if TYPE_CHECKING: # pragma: no cover
10+
from mypy_boto3_lambda.type_defs import FileSystemConfigTypeDef
11+
else:
12+
FileSystemConfigTypeDef = dict
13+
14+
15+
get_lambda_client = AWSService.LAMBDA.get_client
16+
17+
18+
def get_lambda_function_url(
19+
function_name: Union[LambdaFunctionName, str], region: AWSRegion = None
20+
) -> Optional[str]:
21+
function_name = LambdaFunctionName(function_name)
22+
23+
lambda_client = get_lambda_client(region=region)
24+
25+
try:
26+
response = lambda_client.get_function_url_config(FunctionName=function_name)
27+
except ClientError as e:
28+
if get_client_error_code(e) == "ResourceNotFoundException":
29+
return None
30+
else:
31+
raise e
32+
return response["FunctionUrl"]
33+
34+
35+
def get_lambda_function_file_systems(
36+
function_name: Union[LambdaFunctionName, str], region: AWSRegion = None
37+
) -> List[FileSystemConfigTypeDef]:
38+
function_name = LambdaFunctionName(function_name)
39+
40+
lambda_client = get_lambda_client(region=region)
41+
42+
response = lambda_client.get_function_configuration(FunctionName=function_name)
43+
44+
fs_configs = response.get("FileSystemConfigs")
45+
46+
return fs_configs
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import json
2+
from test.aibs_informatics_aws_utils.base import AwsBaseTest
3+
4+
import boto3
5+
from moto import mock_iam, mock_lambda_simple, mock_sts
6+
from pytest import raises
7+
8+
from aibs_informatics_aws_utils.lambda_ import (
9+
get_lambda_function_file_systems,
10+
get_lambda_function_url,
11+
)
12+
13+
14+
@mock_sts
15+
class LambdaTests(AwsBaseTest):
16+
def setUp(self) -> None:
17+
super().setUp()
18+
self.set_aws_credentials()
19+
20+
def get_role_arn(self) -> str:
21+
return boto3.client("iam").create_role(
22+
RoleName="foo",
23+
AssumeRolePolicyDocument=json.dumps(
24+
{
25+
"Version": "2012-10-17",
26+
"Statement": [
27+
{
28+
"Effect": "Allow",
29+
"Principal": {"Service": "lambda.amazonaws.com"},
30+
"Action": "sts:AssumeRole",
31+
}
32+
],
33+
}
34+
),
35+
)["Role"]["Arn"]
36+
37+
@mock_iam
38+
@mock_lambda_simple
39+
def test__get_lambda_function_file_systems__no_file_systems(self):
40+
# Set up lambda
41+
lambda_client = boto3.client("lambda")
42+
file_system_configs = [
43+
{
44+
"Arn": "arn:aws:elasticfilesystem:us-west-2:123456789012:access-point/fsap-1234abcd",
45+
"LocalMountPath": "/mnt/efs1",
46+
},
47+
{
48+
"Arn": "arn:aws:elasticfilesystem:us-west-2:123456789012:access-point/fsap-1234abcd2",
49+
"LocalMountPath": "/mnt/efs2",
50+
},
51+
]
52+
lambda_client.create_function(
53+
FunctionName="test",
54+
Runtime="python3.8",
55+
Handler="test",
56+
Role=self.get_role_arn(),
57+
Code={"ZipFile": b"bar"},
58+
Environment={"Variables": {"TEST": "test"}},
59+
# NOTE: FileSystemConfigs is not supported by moto yet, so this is meaningless
60+
FileSystemConfigs=file_system_configs,
61+
)
62+
# HACK: moto doesn't support FileSystemConfigs yet, so we have to patch it in
63+
# here we will fetch the actual response and then add the FileSystemConfigs
64+
response = lambda_client.get_function_configuration(FunctionName="test")
65+
with self.stub(lambda_client) as lambda_stubber:
66+
lambda_stubber.add_response(
67+
"get_function_configuration",
68+
{
69+
**response,
70+
**{"FileSystemConfigs": file_system_configs},
71+
},
72+
expected_params={"FunctionName": "test"},
73+
)
74+
self.create_patch(
75+
"aibs_informatics_aws_utils.lambda_.get_lambda_client", return_value=lambda_client
76+
)
77+
78+
actual_file_system_configs = get_lambda_function_file_systems("test")
79+
self.assertListEqual(actual_file_system_configs, file_system_configs)
80+
81+
@mock_iam
82+
@mock_lambda_simple
83+
def test__get_lambda_function_url__with_url(self):
84+
lambda_client = boto3.client("lambda")
85+
lambda_client.create_function(
86+
FunctionName="test",
87+
Runtime="python3.8",
88+
Handler="test",
89+
Role=self.get_role_arn(),
90+
Code={"ZipFile": b"bar"},
91+
Environment={"Variables": {"TEST": "test"}},
92+
)
93+
response = lambda_client.create_function_url_config(
94+
FunctionName="test", AuthType="AWS_IAM"
95+
)
96+
97+
assert get_lambda_function_url("test") == response["FunctionUrl"]
98+
99+
@mock_iam
100+
@mock_lambda_simple
101+
def test__get_lambda_function_url__handles_no_url(self):
102+
lambda_client = boto3.client("lambda")
103+
lambda_client.create_function(
104+
FunctionName="test",
105+
Runtime="python3.8",
106+
Handler="test",
107+
Role=self.get_role_arn(),
108+
Code={"ZipFile": b"bar"},
109+
Environment={"Variables": {"TEST": "test"}},
110+
)
111+
112+
@mock_iam
113+
@mock_lambda_simple
114+
def test__get_lambda_function_url__handles_invalid_function_name(self):
115+
with raises(ValueError):
116+
get_lambda_function_url("@#$#$@#$@#$")

0 commit comments

Comments
 (0)