CompositionalIT / farmer

Repeatable Azure deployments with ARM templates - made easy!
https://compositionalit.github.io/farmer
MIT License
525 stars 156 forks source link

Details about associating web app to vnet #1122

Closed BrianVallelunga closed 2 months ago

BrianVallelunga commented 3 months ago

I'm having issues trying to link an App Service with a VNET. I'm getting the error: Subnet record must be linked to a virtual network to properly assign the resourceId

Here's the basic structure of my config. Any ideas? I couldn't find an example of how this should be used. Thanks.

let appServiceSubnet =
    subnet {
        name "app-services"
        prefix "10.1.0.0/24"
    }

let primaryVNet =
    vnet {
        name "myvnet"
        add_address_spaces [ "10.1.0.0/16" ]
        add_subnets [ appServiceSubnet ]
    }

let appServiceSubnetReference =
    SubnetReference.create (primaryVNet, appServiceSubnet.Name)

let applicationAppService =
    webApp {
        name applicationAppServiceName
        link_to_vnet appServiceSubnetReference 
        depends_on primaryVNet
        depends_on appServiceSubnet
        // ...
    }
isaacabraham commented 3 months ago

One for @ninjarobot here!

BrianVallelunga commented 3 months ago

Fundamentally, VirtualNetwork is not assigned to the SubnetConfig object. I see that in the code, but I'm not sure how it would ever be assigned given the order of creating a vnet first and then adding the subnet to it.

BrianVallelunga commented 3 months ago

Well, this is a bit odd, but if I add the subnet directly under the vnet, it works. In my testing, the error wasn't even related to the web app, but rather the vnet itself. I would still like to know if there's a way to create the subnets outside of the vnet builder, but at least it works now and this might help someone else.


let primaryVNet =
    vnet {
        name "myvnet"
        add_address_spaces [ "10.1.0.0/16" ]
        add_subnets [ 
            subnet {
                name "app-services"
                prefix "10.1.0.0/24"
                add_delegations [ SubnetDelegationService.WebServerFarms ]
            }
         ]
    }

let applicationAppService =
    webApp {
        name applicationAppServiceName
        link_to_vnet (primaryVNet, ResourceName "app-services")
        depends_on primaryVNet
        // ...
    }
ninjarobot commented 3 months ago

If the subnet builder is used standalone, you have to specify the vnet. This is usually used when you want to add a subnet on an existing vmet. If you add that subnet with add_subnets on a vnet builder, it will get linked to that parent vnet.

Sorry about that - I know it's a bit confusing, but allows the same builder to work in both cases - standalone and within a vnet. Maybe it needs a better error message.

BrianVallelunga commented 3 months ago

Thanks @ninjarobot . That makes sense. How do you specify the vnet in that case?

ninjarobot commented 3 months ago

@BrianVallelunga with link_to_vnet. There is a complete example in this test.

ninjarobot commented 2 months ago

@BrianVallelunga does the link to the test resolve the issue?

isaacabraham commented 2 months ago

@BrianVallelunga can we close this issue?