From eadb4a21769b50379d2e168808a5e91c9fcfb28c Mon Sep 17 00:00:00 2001 From: cfln123 Date: Wed, 19 Mar 2025 16:48:12 -0300 Subject: [PATCH 1/2] Custom volume size --- bastion/ec2.go | 10 +++------- bastion/launch.go | 21 ++++++++++++++++----- entrypoint/main.go | 5 +++++ 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/bastion/ec2.go b/bastion/ec2.go index c8d4cba..723d72a 100644 --- a/bastion/ec2.go +++ b/bastion/ec2.go @@ -11,7 +11,7 @@ import ( "github.com/aws/aws-sdk-go/service/ec2" ) -func StartEc2(id string, sess *session.Session, ami string, instanceProfile string, subnetId string, securitygroupId string, instanceType string, launchedBy string, userdata string, keyName string, spot bool, public bool, volumeEncryption bool, volumeType string) (string, error) { +func StartEc2(id string, sess *session.Session, ami string, instanceProfile string, subnetId string, securitygroupId string, instanceType string, launchedBy string, userdata string, keyName string, spot bool, public bool, volumeSize int64, volumeEncryption bool, volumeType string) (string, error) { client := ec2.New(sess) input := &ec2.RunInstancesInput{ @@ -45,23 +45,19 @@ func StartEc2(id string, sess *session.Session, ami string, instanceProfile stri }, } - - blockDeviceMapping := &ec2.BlockDeviceMapping{ DeviceName: aws.String("/dev/xvda"), // Using default mapping Ebs: &ec2.EbsBlockDevice{ - VolumeSize: aws.Int64(8), // Default size GiB; + VolumeSize: aws.Int64(volumeSize), VolumeType: aws.String(volumeType), Encrypted: aws.Bool(volumeEncryption), - DeleteOnTermination: aws.Bool(true), // Default behavior + DeleteOnTermination: aws.Bool(true), // Default behavior }, } input.BlockDeviceMappings = []*ec2.BlockDeviceMapping{ blockDeviceMapping, } - - if public { input.NetworkInterfaces = []*ec2.InstanceNetworkInterfaceSpecification{ diff --git a/bastion/launch.go b/bastion/launch.go index cdd6e46..76a9034 100644 --- a/bastion/launch.go +++ b/bastion/launch.go @@ -76,6 +76,7 @@ func CreateBastion(c *cli.Context) (string, string, error) { spot bool publicIpAddress bool bastionInstanceId string + volumeSize int64 volumeEncryption bool volumeType string ) @@ -126,16 +127,21 @@ func CreateBastion(c *cli.Context) (string, string, error) { publicIpAddress = false } + volumeSize = 8 + if c.IsSet("volume-size") { + volumeSize = c.Int64("volume-size") //Default volume-size + } + volumeEncryption = true if c.Bool("volume-encryption") { volumeEncryption = false } - volumeType = c.String("volume-type") + volumeType = c.String("volume-type") if volumeType == "" { volumeType = "gp2" //Default volume-type } - + subnetId = c.String("subnet-id") if subnetId == "" { subnets, err := GetSubnets(sess) @@ -165,7 +171,7 @@ func CreateBastion(c *cli.Context) (string, string, error) { userdata = BuildLinuxUserdata(sshKey, c.String("ssh-user"), expire, expireAfter, c.String("efs"), c.String("access-points")) - bastionInstanceId, err = StartEc2(id, sess, ami, instanceProfile, subnetId, securitygroupId, instanceType, launchedBy, userdata, keyName, spot, publicIpAddress, volumeEncryption, volumeType) + bastionInstanceId, err = StartEc2(id, sess, ami, instanceProfile, subnetId, securitygroupId, instanceType, launchedBy, userdata, keyName, spot, publicIpAddress, volumeSize, volumeEncryption, volumeType) if err != nil { return "", "", err } @@ -190,6 +196,7 @@ func CmdLaunchWindowsBastion(c *cli.Context) error { spot bool publicIpAddress bool bastionInstanceId string + volumeSize int64 volumeEncryption bool volumeType string ) @@ -224,13 +231,17 @@ func CmdLaunchWindowsBastion(c *cli.Context) error { publicIpAddress = false } + volumeSize = 8 + if c.IsSet("volume-size") { + volumeSize = c.Int64("volume-size") //Default volume-size + } + volumeEncryption = true if c.Bool("volume-encryption") { volumeEncryption = false } volumeType = c.String("volume-type") - if volumeType == "" { volumeType = "gp2" //Default volume-type } @@ -281,7 +292,7 @@ func CmdLaunchWindowsBastion(c *cli.Context) error { userdata = BuildWindowsUserdata() - bastionInstanceId, err = StartEc2(id, sess, ami, instanceProfile, subnetId, securitygroupId, instanceType, launchedBy, userdata, keyName, spot, publicIpAddress, volumeEncryption, volumeType) + bastionInstanceId, err = StartEc2(id, sess, ami, instanceProfile, subnetId, securitygroupId, instanceType, launchedBy, userdata, keyName, spot, publicIpAddress, volumeSize, volumeEncryption, volumeType) if err != nil { return err } diff --git a/entrypoint/main.go b/entrypoint/main.go index ec4a53f..bb2fb44 100644 --- a/entrypoint/main.go +++ b/entrypoint/main.go @@ -105,6 +105,11 @@ func CliMain() { Aliases: []string{"o"}, Usage: "any additional ssh options such as tunnels '-L 3306:db.internal.example.com:3306'", }, + &cli.Int64Flag{ + Name: "volume-size", + Value: 8, + Usage: "specify volume volume size in GB", + }, &cli.BoolFlag{ Name: "volume-encryption", Usage: "enable volume encryption", From 8e0c43da08e56b43c845f2a6689bbb4af863b0d4 Mon Sep 17 00:00:00 2001 From: cfln123 Date: Wed, 19 Mar 2025 16:58:32 -0300 Subject: [PATCH 2/2] Custom volume size for windows --- entrypoint/main.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/entrypoint/main.go b/entrypoint/main.go index bb2fb44..949ce59 100644 --- a/entrypoint/main.go +++ b/entrypoint/main.go @@ -108,7 +108,7 @@ func CliMain() { &cli.Int64Flag{ Name: "volume-size", Value: 8, - Usage: "specify volume volume size in GB", + Usage: "specify volume size in GB", }, &cli.BoolFlag{ Name: "volume-encryption", @@ -116,7 +116,7 @@ func CliMain() { }, &cli.StringFlag{ Name: "volume-type", - Usage: "specify volume volume type [gp2, gp3, io2, io1]", + Usage: "specify volume type [gp2, gp3, io2, io1]", }, }, }, @@ -178,13 +178,18 @@ func CliMain() { Name: "private", Usage: "don't attach a public IP to the bastion", }, + &cli.Int64Flag{ + Name: "volume-size", + Value: 8, + Usage: "specify volume size in GB", + }, &cli.BoolFlag{ Name: "volume-encryption", Usage: "enable volume encryption", }, &cli.StringFlag{ Name: "volume-type", - Usage: "specify volume volume type [gp2, gp3, io2, io1]", + Usage: "specify volume type [gp2, gp3, io2, io1]", }, }, },