Hi
I’m trying to use existing AWS security group rules from JSON file and update them back. In this regarding below is the sample data
[
{
"Description": "dev_sg",
"GroupName": "dev_sg",
"IpPermissions": [
{
"FromPort": 80,
"IpProtocol": "tcp",
"IpRanges": [
{
"CidrIp": "192.168.1.10/32",
"Description": "Test office"
}
],
"Ipv6Ranges": [],
"PrefixListIds": [],
"ToPort": 80,
"UserIdGroupPairs": []
},
{
"FromPort": 0,
"IpProtocol": "tcp",
"IpRanges": [
{
"CidrIp": "192.168.10.12/32"
},
{
"CidrIp": "192.168.11.12/32"
},
{
"CidrIp": "192.168.13.12/32",
"Description": "Test office"
}
],
"Ipv6Ranges": [],
"PrefixListIds": [],
"ToPort": 6555,
"UserIdGroupPairs": []
},
{
"IpProtocol": "-1",
"IpRanges": [
{
"CidrIp": "192.168.13.11/32"
},
{
"CidrIp": "192.168.13.14/32"
},
{
"CidrIp": "192.168.13.12/32",
"Description": "Test office new ISP"
}
],
"Ipv6Ranges": [],
"PrefixListIds": [],
"UserIdGroupPairs": []
},
{
"FromPort": 22,
"IpProtocol": "tcp",
"IpRanges": [
{
"CidrIp": "192.168.13.16/32"
},
{
"CidrIp": "192.168.13.38/32"
}
],
"Ipv6Ranges": [],
"PrefixListIds": [],
"ToPort": 22,
"UserIdGroupPairs": []
},
{
"FromPort": 443,
"IpProtocol": "tcp",
"IpRanges": [
{
"CidrIp": "192.168.1.10/32",
"Description": "Test office"
}
],
"Ipv6Ranges": [],
"PrefixListIds": [],
"ToPort": 443,
"UserIdGroupPairs": []
},
{
"FromPort": 8433,
"IpProtocol": "tcp",
"IpRanges": [
{
"CidrIp": "192.168.13.178/32",
"Description": "testing2"
}
],
"Ipv6Ranges": [],
"PrefixListIds": [],
"ToPort": 8433,
"UserIdGroupPairs": []
}
],
"OwnerId": "2542495439859",
"GroupId": "sg-25y495lngkelfgm",
"IpPermissionsEgress": [
{
"IpProtocol": "-1",
"IpRanges": [
{
"CidrIp": "0.0.0.0/0"
}
],
"Ipv6Ranges": [],
"PrefixListIds": [],
"UserIdGroupPairs": []
}
],
"VpcId": "vpc-sldngslgw454232624"
},
{
"Description": "SG rule",
"GroupName": "allow_access",
"IpPermissions": [
{
"FromPort": 6379,
"IpProtocol": "tcp",
"IpRanges": [
{
"CidrIp": "192.168.13.34/32",
"Description": "test data"
}
],
"Ipv6Ranges": [],
"PrefixListIds": [],
"ToPort": 6379,
"UserIdGroupPairs": []
}
],
"OwnerId": "2542495439859",
"GroupId": "sg-3254365erlgkh",
"IpPermissionsEgress": [
{
"IpProtocol": "-1",
"IpRanges": [
{
"CidrIp": "0.0.0.0/0"
}
],
"Ipv6Ranges": [],
"PrefixListIds": [],
"UserIdGroupPairs": []
}
],
"VpcId": "vpc-sldngslgw454232624"
}
]
I see errors while trying to use in through locals
block. Below is the code
locals {
config = jsondecode(file("test-json.json"))
nested_details = flatten([
for k, v in local.config : [
for keys, values in v : [
]
]
])
}
output "result" {
value = local.config
}
To understand on more of it, I tried to use terraform console
with the command flatten([for k,v in local.config : [for key,values in v: [ for value in values: value.IpPermissions ] ] ])
but it throws an error.
Error: Unsupported attribute
│
│ on <console-input> line 1:
│ (source code not available)
│
│ This object does not have an attribute named "IpPermissions".
I’m trying to access FromPort
, Toport
, CidrIp
and Description
. Please help me understand how to access them to use in security_group_rule
resource