Skip to content

Commit 902caa1

Browse files
committed
Add vpc_config to awslambda.Function
This will allow to set the VPC for lambda functions
1 parent 66a2ca9 commit 902caa1

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

src/e3/aws/troposphere/awslambda/__init__.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ def __init__(
4848
environment: dict[str, str] | None = None,
4949
logging_config: awslambda.LoggingConfig | None = None,
5050
dl_config: awslambda.DeadLetterConfig | None = None,
51+
vpc_config: awslambda.VPCConfig | None = None,
5152
):
5253
"""Initialize an AWS lambda function.
5354
@@ -76,6 +77,10 @@ def __init__(
7677
:param logging_config: The function's Amazon CloudWatch Logs settings
7778
:param dl_config: The dead letter config that specifies the topic or queue where
7879
lambda sends asynchronous events when they fail processing
80+
:param vpc_config: For network connectivity to AWS resources in a VPC, specify
81+
a list of security groups and subnets in the VPC. When you connect a
82+
function to a VPC, it can access resources and the internet only
83+
through that VPC
7984
"""
8085
self.name = name
8186
self.description = description
@@ -94,6 +99,7 @@ def __init__(
9499
self.environment = environment
95100
self.logging_config = logging_config
96101
self.dl_config = dl_config
102+
self.vpc_config = vpc_config
97103

98104
def cfn_policy_document(self, stack: Stack) -> PolicyDocument:
99105
statements = [
@@ -209,6 +215,9 @@ def lambda_resources(
209215
if self.dl_config is not None:
210216
params["DeadLetterConfig"] = self.dl_config
211217

218+
if self.vpc_config is not None:
219+
params["VpcConfig"] = self.vpc_config
220+
212221
result = [awslambda.Function(name_to_id(self.name), **params)]
213222
# If retention duration is given provide a log group.
214223
# If not provided the lambda creates a log group with
@@ -392,6 +401,7 @@ def __init__(
392401
environment: dict[str, str] | None = None,
393402
logging_config: awslambda.LoggingConfig | None = None,
394403
dl_config: awslambda.DeadLetterConfig | None = None,
404+
vpc_config: awslambda.VPCConfig | None = None,
395405
):
396406
"""Initialize an AWS lambda function with a Python runtime.
397407
@@ -420,6 +430,10 @@ def __init__(
420430
:param logging_config: The function's Amazon CloudWatch Logs settings
421431
:param dl_config: The dead letter config that specifies the topic or queue where
422432
lambda sends asynchronous events when they fail processing
433+
:param vpc_config: For network connectivity to AWS resources in a VPC, specify
434+
a list of security groups and subnets in the VPC. When you connect a
435+
function to a VPC, it can access resources and the internet only
436+
through that VPC
423437
"""
424438
assert runtime.startswith("python"), "PyFunction only accept Python runtimes"
425439
super().__init__(
@@ -439,6 +453,7 @@ def __init__(
439453
environment=environment,
440454
logging_config=logging_config,
441455
dl_config=dl_config,
456+
vpc_config=vpc_config,
442457
)
443458
self.code_dir = code_dir
444459
self.requirement_file = requirement_file

tests/tests_e3_aws/troposphere/awslambda/awslambda_test.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
VersionWeight,
1717
LoggingConfig,
1818
DeadLetterConfig,
19+
VPCConfig,
1920
)
2021

2122
from e3.aws import AWSEnv
@@ -180,6 +181,52 @@
180181
},
181182
}
182183

184+
EXPECTED_PYFUNCTION_WITH_VPC_TEMPLATE = {
185+
"Mypylambda": {
186+
"Properties": {
187+
"Code": {
188+
"S3Bucket": "cfn_bucket",
189+
"S3Key": "templates/mypylambda_lambda.zip",
190+
},
191+
"Description": "this is a test with vpcconfig",
192+
"FunctionName": "mypylambda",
193+
"Handler": "app.main",
194+
"Role": "somearn",
195+
"Runtime": "python3.12",
196+
"Timeout": 3,
197+
"VpcConfig": {
198+
"SecurityGroupIds": [
199+
"sg-085912345678492fb",
200+
],
201+
"SubnetIds": [
202+
"subnet-071f712345678e7c8",
203+
"subnet-07fd123456788a036",
204+
],
205+
},
206+
"MemorySize": 128,
207+
"EphemeralStorage": {"Size": 1024},
208+
"ReservedConcurrentExecutions": 1,
209+
"Environment": {
210+
"Variables": {"env_key_1": "env_value_1", "env_key_2": "env_value2"}
211+
},
212+
"LoggingConfig": {
213+
"ApplicationLogLevel": "INFO",
214+
"LogFormat": "JSON",
215+
"SystemLogLevel": "WARN",
216+
},
217+
},
218+
"Type": "AWS::Lambda::Function",
219+
},
220+
"MypylambdaLogGroup": {
221+
"DeletionPolicy": "Retain",
222+
"Properties": {
223+
"LogGroupName": "/aws/lambda/mypylambda",
224+
"RetentionInDays": 7,
225+
},
226+
"Type": "AWS::Logs::LogGroup",
227+
},
228+
}
229+
183230
EXPECTED_PYFUNCTION_POLICY_DOCUMENT = {
184231
"Statement": [
185232
{
@@ -494,6 +541,38 @@ def test_pyfunction_with_dlconfig(stack: Stack) -> None:
494541
assert stack.export()["Resources"] == EXPECTED_PYFUNCTION_WITH_DLQ_TEMPLATE
495542

496543

544+
def test_pyfunction_with_vpcconfig(stack: Stack) -> None:
545+
stack.s3_bucket = "cfn_bucket"
546+
stack.s3_key = "templates/"
547+
stack.add(
548+
PyFunction(
549+
name="mypylambda",
550+
description="this is a test with vpcconfig",
551+
role="somearn",
552+
runtime="python3.12",
553+
code_dir="my_code_dir",
554+
handler="app.main",
555+
memory_size=128,
556+
ephemeral_storage_size=1024,
557+
logs_retention_in_days=7,
558+
reserved_concurrent_executions=1,
559+
environment={"env_key_1": "env_value_1", "env_key_2": "env_value2"},
560+
logging_config=LoggingConfig(
561+
ApplicationLogLevel="INFO",
562+
LogFormat="JSON",
563+
SystemLogLevel="WARN",
564+
),
565+
vpc_config=VPCConfig(
566+
"mypylambdavpc",
567+
SecurityGroupIds=["sg-085912345678492fb"],
568+
SubnetIds=["subnet-071f712345678e7c8", "subnet-07fd123456788a036"],
569+
),
570+
)
571+
)
572+
print(stack.export()["Resources"])
573+
assert stack.export()["Resources"] == EXPECTED_PYFUNCTION_WITH_VPC_TEMPLATE
574+
575+
497576
@pytest.mark.parametrize(
498577
"python_version, platform_list",
499578
[

0 commit comments

Comments
 (0)