https://pulumi.com logo
Title
w

wooden-battery-52855

07/19/2021, 2:53 PM
hey, another newbie. trying out pulumi and i can't figure out how i add a private service connection to my network (to connect to cloud sql etc.)
p

prehistoric-activity-61023

07/19/2021, 2:58 PM
I can share a working solution in python if you want 🙂
ip_address_range = gcp.compute.GlobalAddress(
    "global-address",
    purpose="VPC_PEERING",
    address_type="INTERNAL",
    prefix_length=16,
    network=network.name,
)
gcp.servicenetworking.Connection(
    "private-connection",
    network=network.id,
    service="<http://servicenetworking.googleapis.com|servicenetworking.googleapis.com>",
    reserved_peering_ranges=[ip_address_range.name],
)
where
network
is an instance of
gcp.compute.Network
👏 1
👀 1
Make sure you have
<http://servicenetworking.googleapis.com|servicenetworking.googleapis.com>
service enabled. If you want to make it via pulumi as well:
service_networking_api = gcp.projects.Service(
    "servicenetworking",
    disable_dependent_services=True,
    service="<http://servicenetworking.googleapis.com|servicenetworking.googleapis.com>",
)
However then you’ll need to explicitly add
depends_on
in
gcp.servicenetworking.Connection
(or run
pulumi up
twice 😛):
gcp.servicenetworking.Connection(
        "private-connection",
        network=network.id,
        service="<http://servicenetworking.googleapis.com|servicenetworking.googleapis.com>",
        reserved_peering_ranges=[ip_address_range.name],
=>      pulumi.ResourceOptions(depends_on=[service_networking_api])
    )
h

helpful-hair-30515

07/19/2021, 3:12 PM
and while creating cloudsql private instance we can use this service connection right...in that way cloud sql private IP is in the range of
allocated_range
p

prehistoric-activity-61023

07/19/2021, 3:13 PM
If you want to use private IP in CloudSQL, project must have at least one private connection declared. The code above should satisfy that requirement.
Setting private IP on cloudsql instance itself is pretty straighforward:
...
ip_configuration=gcp.sql.DatabaseInstanceSettingsIpConfigurationArgs(
    private_network=network.id,
),
...
and again (unfortunately), the dependency between
gcp.servicenetworking.Connection
and
gcp.sql.DatabaseInstance
cannot be detected automatically, so you either have to make sure that private connection is created before you try to create database instance or use
depends_on
again to explicitly mark this fact
👏 1
🙌 1
w

wooden-battery-52855

07/19/2021, 3:19 PM
thanks! just catching up and reading everything
ok, that all makes sense. wouldn't have thought globaladdress was what I was looking for, as for the dependency checks, @helpful-hair-30515 good case for splitting it out into its own class and doing the checks there (against the config)
👍 1
👏 1
p

prehistoric-activity-61023

07/19/2021, 3:27 PM
In my project, I created two stacks. One creates the project, VPC with private connection and enables all required APIs. The second one uses StackReference to get VPC name and creates other resources such as CloudSQL, Memorystore Redis etc.
h

helpful-hair-30515

07/19/2021, 3:28 PM
Thanks @prehistoric-activity-61023 I tried to understand it, if we do it in console there is actually two steps as you codified already
1.allocate IP range
2.Create Connection
@wooden-battery-52855 that should work as well and as @prehistoric-activity-61023 mentioned to use stack references instead also a great choice!!!