diff --git a/multi-node/aws/pkg/config/config.go b/multi-node/aws/pkg/config/config.go index db83079fc2..f81753c1fa 100644 --- a/multi-node/aws/pkg/config/config.go +++ b/multi-node/aws/pkg/config/config.go @@ -180,8 +180,9 @@ type StackTemplateOptions struct { type stackConfig struct { *Config - UserDataWorker string - UserDataController string + UserDataWorker string + UserDataController string + ControllerSubnetIndex int } func execute(filename string, data interface{}, compress bool) (string, error) { @@ -227,6 +228,25 @@ func (c Cluster) stackConfig(opts StackTemplateOptions, compressUserData bool) ( stackConfig.Config.TLSConfig = compactAssets + controllerIPAddr := net.ParseIP(stackConfig.ControllerIP) + if controllerIPAddr == nil { + return nil, fmt.Errorf("invalid controllerIP: %s", stackConfig.ControllerIP) + } + controllerSubnetFound := false + lastSubnetIndex := len(stackConfig.Subnets) - 1 + for i, subnet := range stackConfig.Subnets { + _, instanceCIDR, err := net.ParseCIDR(subnet.InstanceCIDR) + if err != nil { + return nil, fmt.Errorf("invalid instanceCIDR: %v", err) + } + if instanceCIDR.Contains(controllerIPAddr) { + stackConfig.ControllerSubnetIndex = i + controllerSubnetFound = true + } else if !controllerSubnetFound && i == lastSubnetIndex { + return nil, fmt.Errorf("Fail-fast occurred possibly because of a bug: ControllerSubnetIndex couldn't be determined for subnets (%v) and controllerIP (%v)", stackConfig.Subnets, stackConfig.ControllerIP) + } + } + if stackConfig.UserDataWorker, err = execute(opts.WorkerTmplFile, stackConfig.Config, compressUserData); err != nil { return nil, fmt.Errorf("failed to render worker cloud config: %v", err) } diff --git a/multi-node/aws/pkg/config/templates/stack-template.json b/multi-node/aws/pkg/config/templates/stack-template.json index 10c39cbc87..bb94fce333 100644 --- a/multi-node/aws/pkg/config/templates/stack-template.json +++ b/multi-node/aws/pkg/config/templates/stack-template.json @@ -261,7 +261,7 @@ ], "PrivateIpAddress": "{{.ControllerIP}}", "SubnetId": { - "Ref": "Subnet0" + "Ref": "Subnet{{.ControllerSubnetIndex}}" } } ],