adding multiple security group for lambda using cdk

Viewed 590

as per documentation you can pass the multiple security groups using security_groups as a list inside aws_cdk.aws_lambda.Function then it should simply worl so, I have initialized

sg_names = ['sg-a', 'sg-b', 'sg-c', 'sg-d']  

now when I try to call, lambda. Function like below:

Function(self, 'lambda_a',                                                     
        code=_lambda.Code.from_asset('lambdas'),
        handler='lambda_a.handler',
        function_name='lambda_a',
        runtime=_lambda.Runtime.PYTHON_3_8,
         role=lambda_role,
         vpc=vpc,
        security_groups=sg_names,
         timeout=cdk.Duration.seconds(30),
        )

Now it throws me error:

Expected object reference, got "sg-a"

it seems I should get the objects instead of the values. So, maybe i think I need to call SecurityGroup.from_security_group_id, but I don't see how to add multiple groups to this call. Is there any suggestion how to add multiple security groups to lambda?

1 Answers

You can use below code, I am a beginner in python, please adjust if there is some mistake :)

def security_group(id):
   return SecurityGroup.from_security_group_id(self, id, id)

sg_names = ['sg-a', 'sg-b', 'sg-c', 'sg-d']  
security_groups = map(security_group, sg_names)
Related