Skip to content
This repository has been archived by the owner on Oct 21, 2024. It is now read-only.

Commit

Permalink
vpc: customize NAT instance
Browse files Browse the repository at this point in the history
  • Loading branch information
fwang committed Oct 13, 2024
1 parent 87126d5 commit c57b22e
Showing 1 changed file with 39 additions and 7 deletions.
46 changes: 39 additions & 7 deletions platform/src/components/aws/vpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import {
interpolate,
output,
} from "@pulumi/pulumi";
import { $print, Component, Transform, transform } from "../component";
import { Component, Transform, transform } from "../component";
import { Input } from "../input";
import {
autoscaling,
ec2,
getAvailabilityZonesOutput,
iam,
Expand Down Expand Up @@ -68,7 +67,34 @@ export interface VpcArgs {
* }
* ```
*/
nat?: Input<"ec2" | "managed">;
nat?: Input<
| "ec2"
| "managed"
| {
/**
* Configures the NAT EC2 instance.
* @default `{instance: "t4g.nano"}`
* @example
* ```ts
* {
* nat: {
* ec2: {
* instance: "t4g.large"
* }
* }
* }
* ```
*/
ec2: Input<{
/**
* The type of instance to use for the NAT.
*
* @default `"t4g.nano"`
*/
instance: Input<string>;
}>;
}
>;
/**
* Configures a bastion host that can be used to connect to resources in the VPC.
*
Expand Down Expand Up @@ -325,7 +351,13 @@ export class Vpc extends Component implements Link.Linkable {
}

function normalizeNat() {
return output(args?.nat).apply((nat) => nat);
return output(args?.nat).apply((nat) => {
if (nat === "managed") return { type: "managed" as const };
if (nat === "ec2")
return { type: "ec2" as const, ec2: { instance: "t4g.nano" } };
if (nat) return { type: "ec2" as const, ec2: nat.ec2 };
return undefined;
});
}

function createVpc() {
Expand Down Expand Up @@ -417,7 +449,7 @@ export class Vpc extends Component implements Link.Linkable {

function createNatGateways() {
const ret = all([nat, publicSubnets]).apply(([nat, subnets]) => {
if (nat !== "managed") return [];
if (nat?.type !== "managed") return [];

return subnets.map((subnet, i) => {
const elasticIp = new ec2.Eip(
Expand Down Expand Up @@ -454,7 +486,7 @@ export class Vpc extends Component implements Link.Linkable {

function createNatInstances() {
return nat.apply((nat) => {
if (nat !== "ec2") return output([]);
if (nat?.type !== "ec2") return output([]);

const sg = new ec2.SecurityGroup(
`${name}NatInstanceSecurityGroup`,
Expand Down Expand Up @@ -533,7 +565,7 @@ export class Vpc extends Component implements Link.Linkable {
return new ec2.Instance(
`${name}NatInstance${i + 1}`,
{
instanceType: "t4g.nano",
instanceType: nat.ec2.instance,
ami: ami.id,
subnetId: publicSubnets[i].id,
vpcSecurityGroupIds: [sg.id],
Expand Down

0 comments on commit c57b22e

Please sign in to comment.