From 9884a9771718ea7444039677ed9992418e1d6c09 Mon Sep 17 00:00:00 2001 From: Michael Chandler Date: Sun, 6 Oct 2024 14:16:30 +1000 Subject: feat: #31 allow user to specify vCPU / Memory and let AWS pick the best spot instance. --- cf.yml | 54 ++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 42 insertions(+), 12 deletions(-) (limited to 'cf.yml') diff --git a/cf.yml b/cf.yml index 2eef48b..60f7870 100644 --- a/cf.yml +++ b/cf.yml @@ -20,16 +20,34 @@ Parameters: - Running - Stopped + InstancePurchaseMode: + Type: String + Description: "Spot: Much cheaper, but your instance might restart during gameplay with a few minutes of unsaved gameplay lost. On Demand: Instance will be created in on-demand mode. More expensive, but your gameplay is unlikely to be interrupted by the server going down." + Default: "Spot" + AllowedValues: + - "On Demand" + - "Spot" + InstanceType: Type: String - Description: "m3.medium is a good cost effective (albeit older) instance, 1 VCPU and 3.75 GB of RAM with moderate network performance. Change at your discretion. https://aws.amazon.com/ec2/instance-types/." - Default: m3.medium + Description: "Spot: You should leave this blank to get the best value instance. Override at your discretion: https://aws.amazon.com/ec2/instance-types/. On Demand: You must specify this. " + Default: "" SpotPrice: Type: String - Description: "An m3.medium shouldn't cost more than a cent per hour. Note: Leave this blank to use on-demand pricing." + Description: "Spot: the max cents/hr to pay for spot instance. On Demand: Ignored" Default: "0.05" + SpotMinMemoryMiB: + Type: Number + Description: "Spot: the minimum desired memory for your instance. On Demand: Ignored" + Default: 2048 + + SpotMinVCpuCount: + Type: Number + Description: "Spot: the minimum desired VCPUs for your instance. On Demand: Ignored" + Default: 2 + KeyPairName: Type: String Description: (Optional - An empty value disables this feature) @@ -74,10 +92,16 @@ Metadata: Parameters: - FactorioImageTag - ServerState - - InstanceType - - SpotPrice - EnableRcon - UpdateModsOnStart + - Label: + default: Instance Configuration + Parameters: + - InstancePurchaseMode + - InstanceType + - SpotPrice + - SpotMinMemoryMiB + - SpotMinVCpuCount - Label: default: Remote Access (SSH) Configuration (Optional) Parameters: @@ -95,8 +119,6 @@ Metadata: default: "Update this parameter to shut down / start up your Factorio server as required to save on cost. Takes a few minutes to take effect." InstanceType: default: "Which instance type? You must make sure this is available in your region! https://aws.amazon.com/ec2/pricing/on-demand/" - SpotPrice: - default: "Maximum spot price per hour? Leave blank to disable spot pricing." KeyPairName: default: "If you wish to access the instance via SSH, select a Key Pair to use. https://console.aws.amazon.com/ec2/v2/home?#KeyPairs:sort=keyName" YourIp: @@ -113,8 +135,9 @@ Conditions: KeyPairNameProvided: !Not [ !Equals [ !Ref KeyPairName, '' ] ] IpAddressProvided: !Not [ !Equals [ !Ref YourIp, '' ] ] DnsConfigEnabled: !And [ !Not [ !Equals [ !Ref HostedZoneId, '' ] ], !Not [ !Equals [ !Ref RecordName, '' ] ] ] - SpotPriceProvided: !Not [ !Equals [ !Ref SpotPrice, '' ] ] DoEnableRcon: !Equals [ !Ref EnableRcon, 'true' ] + UsingSpotInstance: !Equals [ !Ref InstancePurchaseMode, 'Spot' ] + InstanceTypeProvided: !Not [ !Equals [ !Ref InstanceType, '' ] ] Mappings: ServerState: @@ -287,16 +310,23 @@ Resources: MixedInstancesPolicy: InstancesDistribution: OnDemandPercentageAboveBaseCapacity: - !If [ SpotPriceProvided, 0, 100 ] - SpotAllocationStrategy: price-capacity-optimized + !If [ UsingSpotInstance, 0, 100 ] + SpotAllocationStrategy: lowest-price SpotMaxPrice: - !If [ SpotPriceProvided, !Ref SpotPrice, !Ref AWS::NoValue ] + !If [ UsingSpotInstance, !Ref SpotPrice, !Ref AWS::NoValue ] LaunchTemplate: LaunchTemplateSpecification: LaunchTemplateId: !Ref LaunchTemplate Version: !GetAtt LaunchTemplate.LatestVersionNumber Overrides: - - InstanceType: !Ref InstanceType + - Fn::If: + - InstanceTypeProvided + - InstanceType: !Ref InstanceType + - InstanceRequirements: + MemoryMiB: + Min: !Ref SpotMinMemoryMiB + VCpuCount: + Min: !Ref SpotMinVCpuCount MaxSize: !FindInMap [ ServerState, !Ref ServerState, DesiredCapacity ] MinSize: !FindInMap [ ServerState, !Ref ServerState, DesiredCapacity ] VPCZoneIdentifier: -- cgit v1.2.3