Skip to content

Commit

Permalink
ft: use pulumi for infra (network + sql)
Browse files Browse the repository at this point in the history
  • Loading branch information
shivanshs9 committed Nov 10, 2024
1 parent 5e57367 commit 3354b15
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 0 deletions.
20 changes: 20 additions & 0 deletions iac/pulumi/libs/network/ip.libsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
local Utils = (import '../utils/config.libsonnet');
{
GlobalAddress: {
local this = self,
Config:: Utils.Config,
type: 'gcp:compute:GlobalAddress',
name:: error 'GlobalAddress requires name',
network:: error 'GlobalAddress requires VPC network',
purpose:: error 'GlobalAddress requires purpose',
addressType:: error 'GlobalAddress requires addressType',
prefixLength:: error 'GlobalAddress requires prefixLength',
properties: {
name: 'ip-%s-%s-%s-%s' % [this.Config.Project, this.Config.Env, this.name, this.Config.ShortRegion],
network: this.network,
purpose: this.purpose,
addressType: this.addressType,
prefixLength: this.prefixLength,
},
},
}
44 changes: 44 additions & 0 deletions iac/pulumi/libs/network/nat.libsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
local Utils = (import '../utils/config.libsonnet');
{
Router: {
local this = self,
Config:: Utils.Config,
type: 'gcp:compute:Router',
name:: error 'Router name is required',
network:: error 'Router requires VPC network',
properties: {
name: 'router-%s-%s-%s-%s' % [this.Config.Project, this.Config.Env, this.name, this.Config.ShortRegion],
region: this.Config.Region,
network: this.network,
bgp: {
asn: 64569,
},
},
},
Nat: {
local this = self,
Config:: Utils.Config,
name:: error 'NAT name is required',
type: 'gcp:compute:RouterNat',
router:: error 'NAT requires router',
subnets:: [],
properties: {
name: 'nat-%s-%s-%s-%s' % [this.Config.Project, this.Config.Env, this.name, this.Config.ShortRegion],
region: this.Config.Region,
router: this.router,
natIpAllocateOption: 'AUTO_ONLY',
sourceSubnetworkIpRangesToNat: if std.length(this.subnets) > 0 then 'LIST_OF_SUBNETWORKS' else 'ALL_SUBNETWORKS_ALL_IP_RANGES',
logConfig: {
enable: true,
filter: 'ERRORS_ONLY',
},
subnetworks: [
{
name: subnet,
sourceIpRangesToNats: ['ALL_IP_RANGES'],
}
for subnet in this.subnets
],
},
},
}
16 changes: 16 additions & 0 deletions iac/pulumi/libs/network/peering.libsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
local Utils = (import '../utils/config.libsonnet');
{
Connection: {
local this = self,
Config:: Utils.Config,
type: 'gcp:servicenetworking:Connection',
network:: error 'Connection requires VPC network',
service:: 'servicenetworking.googleapis.com',
reservedPeeringRanges:: error 'Connection requires reservedPeeringRanges',
properties: {
network: this.network,
service: this.service,
reservedPeeringRanges: this.reservedPeeringRanges,
},
},
}
21 changes: 21 additions & 0 deletions iac/pulumi/libs/network/subnet.libsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
local Utils = (import '../utils/config.libsonnet');
{
Subnet: {
local this = self,
Config:: Utils.Config,
type: 'gcp:compute:Subnetwork',
name:: error 'Subnet name is required',
network:: error 'Subnet requires VPC network',
ipCidrRange:: error 'Subnet requires IP CIDR range',
properties: {
name: 'subnet-%s-%s-%s-%s' % [this.Config.Project, this.Config.Env, this.name, this.Config.ShortRegion],
network: this.network,
ipCidrRange: this.ipCidrRange,
secondaryIpRanges: [],
region: this.Config.Region,
description: 'Subnet for the %s' % (this.Config.Project),
privateIpGoogleAccess: true,
purpose: 'PRIVATE',
},
},
}
14 changes: 14 additions & 0 deletions iac/pulumi/libs/network/vpc.libsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
local Utils = (import '../utils/config.libsonnet');
{
Vpc: {
local this = self,
Config:: Utils.Config,
type: 'gcp:compute:Network',
name:: error 'Vpc name is required',
properties: {
name: 'vpc-%s-%s-%s' % [this.Config.Project, this.Config.Env, this.name],
autoCreateSubnetworks: false,
description: 'VPC network for the %s' % (this.Config.Project),
},
},
}
65 changes: 65 additions & 0 deletions iac/pulumi/libs/sql/postgres.libsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
local Utils = (import '../utils/config.libsonnet');
{
Database: {
local this = self,
Config:: Utils.Config,
type: 'gcp:sql:DatabaseInstance',
privateNetwork:: '',
tier:: 'db-g1-small',
name:: error 'DB Instance name must be set',
settings:: {},
properties: {
name: 'pgsql-%s-%s-%s-%s' % [this.Config.Project, this.Config.Env, this.name, this.Config.ShortRegion],
region: this.Config.Region,
databaseVersion: 'POSTGRES_15',
deletionProtection: true,
settings: {
tier: this.tier,
availabilityType: 'REGIONAL',
diskSize: 20,
ipConfiguration: {
[if this.privateNetwork != '' then 'privateNetwork']: this.privateNetwork,
[if this.privateNetwork != '' then 'enablePrivatePathForGoogleCloudServices']: true,
[if this.privateNetwork == '' then 'ipv4Enabled']: true,
sslMode: 'ENCRYPTED_ONLY',
},
backupConfiguration: {
enabled: true,
backupRetentionSettings: {
retentionUnit: 'COUNT',
retainedBackups: 7,
},
pointInTimeRecoveryEnabled: true,
},
userLabels: {
project: this.Config.Project,
env: this.Config.Env,
region: this.Config.Region,
pulumi: '${pulumi.project}${pulumi.stack}',
},
} + this.settings,
},
},
SqlDB: {
local this = self,
type: 'gcp:sql:Database',
database:: error 'Database name must be set',
instance:: error 'DB Instance must be set',
properties: {
instance: this.instance,
name: this.database,
},
},
SqlUser: {
local this = self,
type: 'gcp:sql:User',
name:: error 'User name must be set',
instance:: error 'DB Instance must be set',
password:: error 'Password must be set',
properties: {
instance: this.instance,
name: this.name,
password: this.password,
},
},
}
8 changes: 8 additions & 0 deletions iac/pulumi/libs/utils/config.libsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
Config:: {
Project: error 'Config.Project not found',
Env: error 'Config.Env not found',
Region: error 'Config.Region not found',
ShortRegion: '[SHORTREGION]',
},
}

0 comments on commit 3354b15

Please sign in to comment.