hashicorp / terraform-cdk

Define infrastructure resources using programming constructs and provision them using HashiCorp Terraform
https://www.terraform.io/cdktf
Mozilla Public License 2.0
4.79k stars 443 forks source link

issue with referencing ipv6 cidr block in subnet #3603

Open anilkaliya123 opened 2 months ago

anilkaliya123 commented 2 months ago

Expected Behavior

Actual Behavior

The ipv6_cidr_block of vpc is /56 (amazon provided ipv6), Subnet is not able to get the /64 cidr_block and it throws an error like invalid cidr block 2600:1f18:7fb:9600::/56/64 seems like ipv6 of subnet is getting appended with /64 , and truncation is not happening . Any suggestion here, how can i achieve the same

Steps to Reproduce

  1. create stack
    
    class DbVpcStack(TerraformStack):
    def __init__(
        self,
        scope: Construct,
        stack_id: str,
        region: str,
        dbvpc_cidr_block: str,
    ):
        super().__init__(scope, stack_id)
            test_vpc = Vpc(
                self,
                "cmc-vpc",
                cidr_block="10.10.0.0/16",
                enable_dns_hostnames=True,
                tags={**DEFAULT_TAGS, "Name": f"{stack_id}-test"},
                assign_generated_ipv6_cidr_block=true
            )
                  test_subnet = Subnet(
                self,
                "test-subnet-external",
                tags={**DEFAULT_TAGS_CMC, "Name": f"{stack_id}-test-external"},
                cidr_block="10.10.0.0/16",
                availability_zone=REGION_AZ_MAPPING[region][0],
                vpc_id=test_vpc.id,
                map_public_ip_on_launch=False,
                depends_on =[cmc_vpc],
                ipv6_cidr_block = test_vpc.ipv6_cidr_block.split("/")[0] + "/64"
            )


2. run terrform init and terraform apply

### Versions

0.16.3

### Providers

_No response_

### Gist

_No response_

### Possible Solutions

_No response_

### Workarounds

_No response_

### Anything Else?

_No response_

### References

_No response_

### Help Wanted

- [ ] I'm interested in contributing a fix myself

### Community Note

- Please vote on this issue by adding a 👍 [reaction](https://blog.github.com/2016-03-10-add-reactions-to-pull-requests-issues-and-comments/) to the original issue to help the community and maintainers prioritize this request
- Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request
- If you are interested in working on this issue or have submitted a pull request, please leave a comment
nbaju1 commented 1 month ago

Pythonic string manipulation only happens when you synthesize the stack. The ipv6 block is resolved on deploy. You need to use the objects that corresponds with hcl string manipulation, which is resolved on deploy:

from cdktf import StringConcat, FnGenerated

str_concat = StringConcat()
test_vpc = Vpc(self, "cmc-vpc", cidr_block="10.10.0.0/16", enable_dns_hostnames=True, assign_generated_ipv6_cidr_block=True)
test_subnet = Subnet(
    self,
    "test-subnet-external",
    cidr_block="10.10.0.0/16",
    vpc_id=test_vpc.id,
    map_public_ip_on_launch=False,
    ipv6_cidr_block=str_concat.join(FnGenerated.element(FnGenerated.split("/", test_vpc.ipv6_cidr_block), 0), "/64"),
)

Converted into JSON:

  "resource": {
    "aws_subnet": {
      "test-subnet-external": {
        "//": {
          "metadata": {
            "path": "example/test-subnet-external",
            "uniqueId": "test-subnet-external"
          }
        },
        "cidr_block": "10.10.0.0/16",
        "ipv6_cidr_block": "${element(split(\"/\", aws_vpc.cmc-vpc.ipv6_cidr_block), 0)}/64",
        "map_public_ip_on_launch": false,
        "vpc_id": "${aws_vpc.cmc-vpc.id}"
      }
    },
    "aws_vpc": {
      "cmc-vpc": {
        "//": {
          "metadata": {
            "path": "example/cmc-vpc",
            "uniqueId": "cmc-vpc"
          }
        },
        "assign_generated_ipv6_cidr_block": true,
        "cidr_block": "10.10.0.0/16",
        "enable_dns_hostnames": true
      }
    }
  },