Skip to content

Commit 2456979

Browse files
authored
Merge pull request #66 from Virtual-Finland-Development/VFD-276-siirretaan-users-api-n-vpc-n-infra-vastuu-pois-jaetuista-resursseista
Own the VPC instead of using shared
2 parents 212cca1 + 73e91f2 commit 2456979

7 files changed

Lines changed: 52 additions & 37 deletions

File tree

VirtualFinland.UsersAPI.Deployment/Common/Models/StackSetup.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,4 @@ public record StackSetup
88
public bool IsProductionEnvironment;
99
public string Environment = default!;
1010
public string ProjectName = default!;
11-
public VpcSetup VpcSetup = default!;
1211
}

VirtualFinland.UsersAPI.Deployment/Common/Models/VpcSetup.cs

Lines changed: 0 additions & 10 deletions
This file was deleted.

VirtualFinland.UsersAPI.Deployment/Features/DatabaseMigratorLambda.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace VirtualFinland.UsersAPI.Deployment.Features;
1212

1313
class DatabaseMigratorLambda
1414
{
15-
public DatabaseMigratorLambda(Config config, StackSetup stackSetup, SecretsManager secretsManager)
15+
public DatabaseMigratorLambda(Config config, StackSetup stackSetup, VpcSetup vpcSetup, SecretsManager secretsManager)
1616
{
1717
// Lambda function
1818
var execRole = new Role($"{stackSetup.ProjectName}-DatabaseMigratorLambdaRole-{stackSetup.Environment}", new RoleArgs
@@ -76,14 +76,14 @@ public DatabaseMigratorLambda(Config config, StackSetup stackSetup, SecretsManag
7676

7777
var defaultSecurityGroup = Pulumi.Aws.Ec2.GetSecurityGroup.Invoke(new GetSecurityGroupInvokeArgs()
7878
{
79-
VpcId = stackSetup.VpcSetup.VpcId,
79+
VpcId = vpcSetup.VpcId,
8080
Name = "default"
8181
});
8282

8383
var functionVpcArgs = new FunctionVpcConfigArgs()
8484
{
8585
SecurityGroupIds = defaultSecurityGroup.Apply(o => $"{o.Id}"),
86-
SubnetIds = stackSetup.VpcSetup.PrivateSubnetIds
86+
SubnetIds = vpcSetup.PrivateSubnetIds
8787
};
8888

8989
var appArtifactPath = Environment.GetEnvironmentVariable("DB_MIGRATOR_ARTIFACT_PATH") ?? config.Require("dbMigratorArtifactPath");

VirtualFinland.UsersAPI.Deployment/Features/LambdaFunctionUrl.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace VirtualFinland.UsersAPI.Deployment.Features;
1616
/// </summary>
1717
class LambdaFunctionUrl
1818
{
19-
public LambdaFunctionUrl(Config config, StackSetup stackSetup, SecretsManager secretsManager)
19+
public LambdaFunctionUrl(Config config, StackSetup stackSetup, VpcSetup vpcSetup, SecretsManager secretsManager)
2020
{
2121
// External references
2222
var codesetStackReference = new StackReference($"{Pulumi.Deployment.Instance.OrganizationName}/codesets/{stackSetup.Environment}");
@@ -84,14 +84,14 @@ public LambdaFunctionUrl(Config config, StackSetup stackSetup, SecretsManager se
8484

8585
var defaultSecurityGroup = Pulumi.Aws.Ec2.GetSecurityGroup.Invoke(new GetSecurityGroupInvokeArgs()
8686
{
87-
VpcId = stackSetup.VpcSetup.VpcId,
87+
VpcId = vpcSetup.VpcId,
8888
Name = "default"
8989
});
9090

9191
var functionVpcArgs = new FunctionVpcConfigArgs()
9292
{
9393
SecurityGroupIds = defaultSecurityGroup.Apply(o => $"{o.Id}"),
94-
SubnetIds = stackSetup.VpcSetup.PrivateSubnetIds
94+
SubnetIds = vpcSetup.PrivateSubnetIds
9595
};
9696

9797
var appArtifactPath = Environment.GetEnvironmentVariable("APPLICATION_ARTIFACT_PATH") ?? config.Require("appArtifactPath");

VirtualFinland.UsersAPI.Deployment/Features/PostgresDatabase.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System.Collections.Immutable;
2+
using System.Linq;
13
using Pulumi;
24
using Pulumi.Aws.Rds;
35
using Pulumi.Random;
@@ -11,11 +13,19 @@ namespace VirtualFinland.UsersAPI.Deployment.Features;
1113
/// </summary>
1214
public class PostgresDatabase
1315
{
14-
public PostgresDatabase(Config config, StackSetup stackSetup)
16+
public PostgresDatabase(Config config, StackSetup stackSetup, VpcSetup vpcSetup)
1517
{
16-
var dbSubNetGroup = new Pulumi.Aws.Rds.SubnetGroup("dbsubnets", new()
18+
// @TODO: Remove oldDbSubNetGroup once the the new subnet group is deployed
19+
var infraStackReference = new StackReference($"{Pulumi.Deployment.Instance.OrganizationName}/{config.Require("infraStackReferenceName")}/{stackSetup.Environment}");
20+
var infraStackReferencePrivateSubnetIds = infraStackReference.RequireOutput("PrivateSubnetIds");
21+
var oldDbSubNetGroup = new Pulumi.Aws.Rds.SubnetGroup("dbsubnets", new()
1722
{
18-
SubnetIds = stackSetup.VpcSetup.PrivateSubnetIds,
23+
SubnetIds = infraStackReferencePrivateSubnetIds.Apply(o => ((ImmutableArray<object>)(o ?? new ImmutableArray<object>())).Select(x => x.ToString())),
24+
});
25+
26+
var dbSubNetGroup = new Pulumi.Aws.Rds.SubnetGroup($"{stackSetup.ProjectName}-dbsubnets-{stackSetup.Environment}", new()
27+
{
28+
SubnetIds = vpcSetup.PrivateSubnetIds,
1929
});
2030

2131
var password = new RandomPassword("password", new()
@@ -36,7 +46,7 @@ public PostgresDatabase(Config config, StackSetup stackSetup)
3646
Username = config.Require("dbAdmin"),
3747
Password = password.Result,
3848
Tags = stackSetup.Tags,
39-
PubliclyAccessible = !stackSetup.IsProductionEnvironment, // DEV: For Production set to FALSE
49+
PubliclyAccessible = false,
4050
SkipFinalSnapshot = !stackSetup.IsProductionEnvironment, // DEV: For production set to FALSE to avoid accidental deletion of the cluster, data safety measure and is the default for AWS.
4151
//SnapshotIdentifier = "" // See README.database.md for more information
4252
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System.Collections.Immutable;
2+
using Pulumi;
3+
using Pulumi.Awsx.Ec2;
4+
using Pulumi.Awsx.Ec2.Inputs;
5+
6+
namespace VirtualFinland.UsersAPI.Deployment.Common.Models;
7+
8+
public class VpcSetup
9+
{
10+
public VpcSetup(StackSetup stackSetup)
11+
{
12+
var vpc = new Vpc($"{stackSetup.ProjectName}-vf-vpc-{stackSetup.Environment}", new VpcArgs()
13+
{
14+
Tags = stackSetup.Tags,
15+
EnableDnsHostnames = true,
16+
NatGateways = new NatGatewayConfigurationArgs
17+
{
18+
Strategy = stackSetup.IsProductionEnvironment ? NatGatewayStrategy.OnePerAz : NatGatewayStrategy.Single
19+
}
20+
});
21+
22+
this.VpcId = vpc.VpcId;
23+
this.PrivateSubnetIds = vpc.PrivateSubnetIds;
24+
}
25+
26+
public Input<string>? VpcId = default!;
27+
public Output<ImmutableArray<string>> PrivateSubnetIds = default!;
28+
}

VirtualFinland.UsersAPI.Deployment/UsersAPIStack.cs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
using System.Collections.Generic;
2-
using System.Collections.Immutable;
3-
using System.Linq;
41
using Pulumi;
52
using VirtualFinland.UsersAPI.Deployment.Common;
63
using VirtualFinland.UsersAPI.Deployment.Common.Models;
@@ -17,11 +14,6 @@ public UsersApiStack()
1714
var environment = Pulumi.Deployment.Instance.StackName;
1815
var projectName = Pulumi.Deployment.Instance.ProjectName;
1916

20-
var infraStackReference = new StackReference($"{Pulumi.Deployment.Instance.OrganizationName}/{config.Require("infraStackReferenceName")}/{environment}");
21-
var infraStackReferencePrivateSubnetIds = infraStackReference.RequireOutput("PrivateSubnetIds");
22-
var infraStackReferencePrivateSubnetIdsAsList = infraStackReferencePrivateSubnetIds.Apply(o => ((ImmutableArray<object>)(o ?? new ImmutableArray<object>())).Select(x => x.ToString()));
23-
var infraStackReferenceVpcId = infraStackReference.RequireOutput("VpcId");
24-
2517
InputMap<string> tags = new InputMap<string>()
2618
{
2719
{
@@ -38,22 +30,18 @@ public UsersApiStack()
3830
Environment = environment,
3931
IsProductionEnvironment = isProductionEnvironment,
4032
Tags = tags,
41-
VpcSetup = new VpcSetup()
42-
{
43-
VpcId = Output.Format($"{infraStackReferenceVpcId}"),
44-
PrivateSubnetIds = infraStackReferencePrivateSubnetIdsAsList as Output<IEnumerable<string>>
45-
}
4633
};
4734

48-
var dbConfigs = new PostgresDatabase(config, stackSetup);
35+
var vpcSetup = new VpcSetup(stackSetup);
36+
var dbConfigs = new PostgresDatabase(config, stackSetup, vpcSetup);
4937
var secretManagerSecret = new SecretsManager(config, stackSetup, dbConfigs);
5038

51-
var lambdaFunctionConfigs = new LambdaFunctionUrl(config, stackSetup, secretManagerSecret);
39+
var lambdaFunctionConfigs = new LambdaFunctionUrl(config, stackSetup, vpcSetup, secretManagerSecret);
5240
ApplicationUrl = lambdaFunctionConfigs.ApplicationUrl;
5341
LambdaId = lambdaFunctionConfigs.LambdaFunctionId;
5442
DBIdentifier = dbConfigs.DBIdentifier;
5543

56-
var databaseMigratorLambda = new DatabaseMigratorLambda(config, stackSetup, secretManagerSecret);
44+
var databaseMigratorLambda = new DatabaseMigratorLambda(config, stackSetup, vpcSetup, secretManagerSecret);
5745
DatabaseMigratorLambdaArn = databaseMigratorLambda.LambdaFunctionArn;
5846
}
5947

0 commit comments

Comments
 (0)