-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Lamba: fix unhandled error when SubnetIds is invalid #12293
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1264,6 +1264,112 @@ def test_vpc_config( | |
"delete_vpcconfig_get_function_response", delete_vpcconfig_get_function_response | ||
) | ||
|
||
@markers.aws.validated | ||
def test_invalid_vpc_config_subnet( | ||
self, create_lambda_function, lambda_su_role, snapshot, aws_client, clean 8000 ups | ||
): | ||
""" | ||
Test invalid "VpcConfig.SubnetIds" Property on the Lambda Function | ||
""" | ||
non_existent_subnet_id = f"subnet-{short_uid()}" | ||
wrong_format_subnet_id = f"bad-format-{short_uid()}" | ||
|
||
# AWS validates the Security Group first, so we need a valid one to test SubnetsIds | ||
security_groups = aws_client.ec2.describe_security_groups(MaxResults=5)["SecurityGroups"] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess this just assumes that there is at least one valid security group (probably a sensible assumption) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I think I've seen other tests doing this assumption with the default VPC, so figured I could as it'd be easier than to create the full VPC. Hopefully this is "safe" to do 😅 |
||
security_group_id = security_groups[0]["GroupId"] | ||
|
||
snapshot.add_transformer(snapshot.transform.regex(non_existent_subnet_id, "<subnet_id_1>")) | ||
snapshot.add_transformer(snapshot.transform.regex(wrong_format_subnet_id, "<subnet_id_2>")) | ||
|
||
zip_file_bytes = create_lambda_archive(load_file(TEST_LAMBDA_PYTHON_ECHO), get_content=True) | ||
|
||
with pytest.raises(ClientError) as e: | ||
aws_client.lambda_.create_function( | ||
FunctionName=f"fn-{short_uid()}", | ||
Handler="index.handler", | ||
Code={"ZipFile": zip_file_bytes}, | ||
PackageType="Zip", | ||
Role=lambda_su_role, | ||
Runtime=Runtime.python3_12, | ||
VpcConfig={ | ||
"SubnetIds": [non_existent_subnet_id], | ||
"SecurityGroupIds": [security_group_id], | ||
}, | ||
) | ||
|
||
snapshot.match("create-response-non-existent-subnet-id", e.value.response) | ||
|
||
with pytest.raises(ClientError) as e: | ||
aws_client.lambda_.create_function( | ||
FunctionName=f"fn-{short_uid()}", | ||
Handler="index.handler", | ||
Code={"ZipFile": zip_file_bytes}, | ||
PackageType="Zip", | ||
Role=lambda_su_role, | ||
Runtime=Runtime.python3_12, | ||
VpcConfig={ | ||
"SubnetIds": [wrong_format_subnet_id], | ||
"SecurityGroupIds": [security_group_id], | ||
}, | ||
) | ||
|
||
snapshot.match("create-response-invalid-format-subnet-id", e.value.response) | ||
|
||
@markers.aws.validated | ||
@pytest.mark.skipif(reason="Not yet implemented", condition=not is_aws_cloud()) | ||
def test_invalid_vpc_config_security_group( | ||
self, create_lambda_function, lambda_su_role, snapshot, aws_client, cleanups | ||
): | ||
""" | ||
Test invalid "VpcConfig.SecurityGroup 6D40 Ids" Property on the Lambda Function | ||
""" | ||
# TODO: maybe add validation of security group id, not currently validated in LocalStack | ||
non_existent_sg_id = f"sg-{short_uid()}" | ||
wrong_format_sg_id = f"bad-format-{short_uid()}" | ||
# this way, we assert that SecurityGroups existence is validated before SubnetIds | ||
subnet_id = f"subnet-{short_uid()}" | ||
|
||
snapshot.add_transformer( | ||
snapshot.transform.regex(non_existent_sg_id, "<security_group_id_1>") | ||
) | ||
snapshot.add_transformer( | ||
snapshot.transform.regex(wrong_format_sg_id, "<security_group_id_2>") | ||
) | ||
|
||
zip_file_bytes = create_lambda_archive(load_file(TEST_LAMBDA_PYTHON_ECHO), get_content=True) | ||
|
||
with pytest.raises(ClientError) as e: | ||
aws_client.lambda_.create_function( | ||
FunctionName=f"fn-{short_uid()}", | ||
Handler="index.handler", | ||
Code={"ZipFile": zip_file_bytes}, | ||
PackageType="Zip", | ||
Role=lambda_su_role, | ||
Runtime=Runtime.python3_12, | ||
VpcConfig={ | ||
"SubnetIds": [subnet_id], | ||
"SecurityGroupIds": [non_existent_sg_id], | ||
}, | ||
) | ||
|
||
snapshot.match("create-response-non-existent-security-group", e.value.response) | ||
|
||
with pytest.raises(ClientError) as e: | ||
aws_client.lambda_.create_function( | ||
FunctionName=f"fn-{short_uid()}", | ||
Handler="index.handler", | ||
Code={"ZipFile": zip_file_bytes}, | ||
PackageType="Zip", | ||
Role=lambda_su_role, | ||
Runtime=Runtime.python3_12, | ||
VpcConfig={ | ||
"SubnetIds": [subnet_id], | ||
"SecurityGroupIds": [wrong_format_sg_id], | ||
}, | ||
) | ||
|
||
snapshot.match("create-response-invalid-format-security-group", e.value.response) | ||
|
||
@ 9E88 markers.aws.validated | ||
def test_invalid_invoke(self, aws_client, snapshot): | ||
region_name = aws_client.lambda_.meta.region_name | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: create_lambda_function fixture unused
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, will update 👍
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh no I'm so sorry, I thought I had pushed the changes but I forgot! 😭and I merged thinking the pipeline was already green... there was
create_lambda_function
andcleanups
being unused...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No worries at all. It was just a tiny nit☺️