首页 > 解决方案 > 创建 ALB 时是否可以添加多个安全组

问题描述

https://docs.aws.amazon.com/cdk/api/latest/python/aws_cdk.aws_elasticloadbalancingv2/ApplicationLoadBalancer.html

ALB CDK 具有返回安全组列表的属性,但在创建 ALB 时找不到添加安全组列表的方法。有什么解决方法吗?

标签: aws-cdk

解决方案


取决于您对安全组的用例。这可能有效 - 只需将它放在多个安全组的循环中:

myAlb.connections.addSecurityGroup(mySG)

编辑:

这其实是有票的。

https://github.com/aws/aws-cdk/issues/5138

这是一种解决方法:

myElb;  // ELB created elsewhere in code

// get the CfnLoadBalancer from the LoadBalancer object
const cfnElb = myElb.node.defaultChild as elb.CfnLoadBalancer  

// View SG list before the addition
console.log(cfnAdminELB.securityGroups)

// SecurityGroups can be 'undefined' need to check so that you don't get a warning
if (cfnElb.securityGroups){ 
    cfnElb.securityGroups.push(mySecurityGroup.securityGroupId)
}

// Verify the token is added
console.log(cfnAdminELB.securityGroups)

理论上,如果您想摆脱默认 CDK 创建的内容,您也可以从 cfnElb.securityGroups 列表中删除内容。


推荐阅读