我正在用python在aws cdk中创建多个ipset,我想知道是否有更好的方法用python编写这个。
我是这样写的:
ip_set01 = wafv2.CfnIPSet(
scope_=self,
id='WAFTESTIPSET01',
scope='REGIONAL',
description='Block test01',
addresses= [],
ip_address_version="IPV4",
)
ip_set02 = wafv2.CfnIPSet(
scope_=self,
id='WAFTESTIPSET02',
scope='REGIONAL',
description='Block test02',
addresses= [],
ip_address_version="IPV6",
)
ip_set03 = wafv2.CfnIPSet(
scope_=self,
id='WAFTESTIPSET03',
scope='REGIONAL',
description='Block test03',
addresses= [],
ip_address_version="IPV4",
)
是的,你可以这样写:
ip_sets = [
wafv2.CfnIPSet(
scope_=self,
id=f"WAFTESTIPSET0{i}",
scope="REGIONAL",
description=f"Block test0{i}",
addresses=[],
ip_address_version="IPV4",
)
for i in range(1, 4)
]
如果你真的想使用不同的ip地址版本,那么你可能需要使用map:
ip_map = {
"01": "IPV4",
"02": "IPV6",
"03": "IPV4"
}
ip_sets = [
wafv2.CfnIPSet(
scope_=self,
id=f"WAFTESTIPSET{key}",
scope="REGIONAL",
description=f"Block test{key}",
addresses=[],
ip_address_version=ip_map[key],
)
for key in ip_map
]