banner

KuchBhiLearning - A free website to learn and code

This is a good learning site. This contains details of cloud computing, AWS, AWS-CDK, AWS-SDK codes and examples including S3, Redis, lambda, api-gateway, cloudfront, cloudformation.

 Find security group based on name AWS-CDK


We can create security group from CDK. But there are chances that the security group has been created and we just need to import using CDK.


CDK does provide the methods where we can easily import security groups and use it in our application.


Here in our example we will go through a scenario where we have kept the security group name value in parameter store, So first pick the security group name and then search for security group.


  const securityGroupName = ssm.StringParameter.fromStringParameterAttributes(
    stack,
    `ssm-params-api`,
    {
      parameterName: `securitygroup/value`,
    }
  ).stringValue;

  const securityGroup = ec2.SecurityGroup.fromLookupByName(
    stack,
    `unique id`,
    `${securityGroupName}`,
    vpc
  );

It is necessary to store the security group name as CDK does not provide way to match only the arn and pick the values. It is also good to store these values if we are not creating it through CDK or created from some place else.


This will help when we have multiple environments and we have different security groups.


No comments:

Post a Comment

If you have any doubts, Please let me know

Copyright 2022, KuchBhiLearning - A free website to learn and code. All rights Reserved.
| Designed by Yaseen Shariff