How to create a http load balancer in GCP with the java SDK?

Viewed 110

I am trying to use the java SDK to create a Load Balancer but as of now i am getting scope mismatched error. I am new to GCP and not sure what mistake i am doing.

I am getting the following exception:

Scope mismatch between forwarding rule and backend service, forwarding rule scope: region us-east1, backend service scope: global.

    Backend backend = new Backend().setGroup("");
    BackendService backendService = new BackendService()
            .setName("backendservice")
            .setPort(80)
            .setProtocol("TCP")
            .setPortName("http")
            .setTimeoutSec(30)
            .setBackends(Collections.singletonList(backend))
            .setHealthChecks(Collections.singletonList(""));
    compute.backendServices()
            .insert("", backendService)
            .execute();


    ForwardingRule forwardingRule = new ForwardingRule()
            .setName("lb-forwarding-rule")
            .setPortRange("80-80")
            .setIPProtocol("TCP")
            .setIpVersion("IPv4")
            .setLoadBalancingScheme("EXTERNAL")
            .setBackendService("");
        
    compute.forwardingRules().insert("", "us-east1", forwardingRule).execute();
1 Answers

Kampassi:

A forwarding rule and a backend should have the same scope in order to work together, so they should be either regional or global. Try to create your forwarding rule again but specifying a global scope. Verify the scope of the resources you are trying to create and set the scope accordingly so they can match.

Consider the following, a load balancer can have two kinds of Network Service Tiers, Premium or Standard. For the former the scope is global, for the latter, the scope is regional.

Related