Tim
Tim

Reputation: 95

AWS cloudformation spot instance parameters

I'm trying to add a parameter in my cloud formation stack that will allow the users to choose between on-demand and spot instances for the launch template, which will initiate the EC2 creation. This stack is designed to launch a workstation for a single user.

Currently there only seems to be one value available for the InstanceMarketType Parameter, does anyone know an alternative way of choosing the instance market type?

  InstanceMarketTypeParameter:
   Type: String
   Default: spot
   AllowedValues:
    - spot
    - on-demand
   Description: Choose between on-demand and spot instances.

The launch template would look something like this

Ec2LaunchTemplate:
    Type: AWS::EC2::LaunchTemplate
    Properties:
      LaunchTemplateName: LinuxWorkstation
      LaunchTemplateData:
        InstanceMarketOptions:
          MarketType:
            Ref: InstanceMarketTypeParameter

Any ideas are welcome!

Upvotes: 1

Views: 695

Answers (1)

Marcin
Marcin

Reputation: 238061

You can make InstanceMarketOptions optional using If:

Conditions:

  IsOnDemand:
    !Equals [!Ref InstanceMarketTypeParameter, "on-demand"]

Resources:

  Ec2LaunchTemplate:
      Type: AWS::EC2::LaunchTemplate
      Properties:
        LaunchTemplateName: LinuxWorkstation
        LaunchTemplateData:
          InstanceMarketOptions:
            !If 
                - IsOnDemand
                - !Ref "AWS::NoValue"
                - MarketType:
                    Ref: InstanceMarketTypeParameter

Upvotes: 3

Related Questions