title: AWS CodeDeploy with Autoscaling Launch Templates with Spot and On-Demand Instances mix
body: <p>It is possible to configure an autoscaling group to use Launch Template instead of Launch Configuration. I want to use it because when I configure an autoscaling group using Launch Configuration I can not set mixed instances policy (ondemand + spot instances).</p>
  
  <p>I have created launch template (name 'test') with such configuration:</p>
  
  <pre><code>    AMI Ubuntu 16, 
      some security groups and IAM Role with EC2, AutoScaling and CodeDeploy full access 
      and all other settings in default *Not specified* state.
  
      Launch Template ID lt-0e1111
      Default Version Number 1
      Launch Template Name test
      Owner arn:aws:iam::11111111:user/itsp
      Version  1 (Default)
      Name  test
      Created by arn:aws:iam::179961111910:user/itsp
      Description Not specified
      Date Created February 8, 2019 at 12:47:54 AM UTC+3
      AMI ID ami-0bf1924ed30c48a6a
      Availability Zone Not specified
      Security Groups Not specified
      Instance Type t2.nano
      Key pair name itsp
      Security Group Ids sg-07e746cf0e4935b84,sg-0a45f7e6ac321614d,sg-27bf6d4b
  
      Advanced Details:
      Purchasing option Not specified
      Spot request type Not specified
      Spot interruption behavior Not specified
      IAM Instance Profile Not specified
      Stop - Hibernate behavior Not specified
      Monitoring Not specified
      EBS-optimized instance Not specified
      Tenancy Host ID Not specified
      RAM Disk ID Not specified
      User Data Not specified
      Elastic Inference Accelerator Not specified
      Maximum Spot price Not specified
      Spot expiry date -
      Spot block duration Not specified
      Shutdown behavior Not specified
      Termination Protection Not specified
      Placement Group Name Not specified
      Tenancy Not specified
      Tenancy Affinity Not specified
      Kernel ID Not specified
      Elastic Graphics Not specified
  </code></pre>
  
  <p>Then I am creating an AutoScaling group with that launch template. Here is my autoscaling group configuration:</p>
  
  <pre><code>Launch Template test
  Launch Template Version 1
  Launch Template Description -
  Instance Types -
  Spot Diversity 0
  Optional On-Demand Base 0
  On-Demand Percentage 0%
  Desired Capacity 1 
  Min 1 
  Max 1
  Availability Zone(s) eu-central-1a
  Subnet(s) subnet-53075e38
  Classic Load Balancers -
  Target Groups -
  Health Check Type EC2
  Health Check Grace Period 300
  Instance Protection - 
  Termination Policies Default
  Suspended Processes -
  Placement Groups -
  Default Cooldown 300
  Enabled Metrics -
  Creation Time Fri Feb 08 00:50:16 GMT+300 2019
  Service-Linked Role arn:aws:iam::11111111111:role/aws-service-role/autoscaling.amazonaws.com/AWSServiceRoleForAutoScaling
  </code></pre>
  
  <p>Such configuration appears to be set when I choose </p>
  
  <pre><code>*Fleet Composition - Adhere to the launch template
  The launch template determines the instance type and purchase option (On-Demand or Spot).* 
  </code></pre>
  
  <p>option and my lauch template configuration is described above.</p>
  
  <p>So there are no spots in my autoscaling group.</p>
  
  <p>Then I configure a CodeDeploy application 'Application' and DeploymentGroup 'DG_TEST' inside of it with such configuration:</p>
  
  <pre><code>Deployment type Blue/green
  Service role ARN arn:aws:iam::11111111111111:role/CodeDeployRole
  Deployment configuration CodeDeployDefault.AllAtOnce
  Rollback enabled -
  Environment configuration: Amazon EC2 Auto Scaling groups -&gt; Name 'test'
  </code></pre>
  
  <hr>
  
  <p>Ok, after everything is set up I set desired instances count to 1 in my Autoscaling group and new on-demand instance spawns in my 'test' group.</p>
  
  <p>I am now starting a blue-green deployment with AWS CLI command:</p>
  
  <pre><code>aws deploy create-deployment --application-name Application --s3-location bucket=codedeploybucket,key=application,bundleType=zip --deployment-group-name DG_TEST
  </code></pre>
  
  <p>And I can see new autoscaling group with same lauch template appears in my green fleet.</p>
  
  <p>So we just checked all our services are working as expected.</p>
  
  <hr>
  
  <p>Now I want to tweak my autoscaling group configuration a little to mix ondemand and spot instances in it:</p>
  
  <pre><code>Fleet Composition - Combine purchase options and instances
  Choose a mix of On-Demand Instances and Spot Instances and multiple instance types. Spot Instances are automatically launched at the lowest price available.
  Instance Types:
  t2.nano (1vCPUs, 0.5GiB)
  t2.small (1vCPUs, 2GiB)
  t2.micro (1vCPUs, 1GiB)
  Maximum Spot Price - Use default (recommended)
  Spot Allocation Strategy - Diversify Spot Instances across your 2 lowest priced instance types per Availability Zone
  Optional On-Demand Base - Designate the first 0 instances as On-Demand
  On-Demand Percentage 50% On-Demand and 50% Spot
  Desired Capacity 1
  Min 1
  Max 1
  </code></pre>
  
  <p>I save it and try same AWS CLI command</p>
  
  <pre><code>aws deploy create-deployment --application-name Application --s3-location bucket=codedeploybucket,key=application,bundleType=zip --deployment-group-name DG_TEST
  </code></pre>
  
  <p>And now I have my deployment failed with such comment:</p>
  
  <pre><code>The following validation error occurred: Valid requests must contain either LaunchTemplate, LaunchConfigurationName, InstanceId or MixedInstancesPolicy parameter. (Service: AmazonAutoScaling; Status Code: 400; Error Code: ValidationError; Request ID: 11111114-2b2b-11e9-93b7-11111111111)
  </code></pre>
  
  <hr>
  
  <p>Another strange thing is that if I configure my Launch Templae to use spots only:</p>
  
  <pre><code>Launch Template ID lt-00dc4c187d1111159
  Default Version Number   1
  Launch Template Name test2
  Owner
  arn:aws:iam::179961111910:user/itsp
  Version     1 (Default)
  Name  test2
  Created by  arn:aws:iam::179961111910:user/itsp
  Description Not specified
  Date Created    February 8, 2019 at 1:14:13 AM UTC+3
  AMI ID    ami-0bf1924ed30c48a6a
  Availability Zone    Not specified
  Security Groups   Not specified
  Instance Type    t2.micro
  Key pair name    itsp
  Security Group Ids    sg-0a45f7e6ac321614d,sg-07e746cf0e4935b84,sg-27bf6d4b
  
  Advanced details
  Purchasing option    spot
  Spot request type    one-time
  Spot interruption behavior    Not specified
  IAM Instance Profile    CodeDeployRole (arn:aws:iam::1111111111:instance-profile/CodeDeployRole)
  Stop - Hibernate behavior    Not specified
  Monitoring    Not specified
  EBS-optimized instance   Not specified
  Tenancy Host ID    Not specified
  RAM Disk ID    Not specified
  User Data    Not specified
  Elastic Inference Accelerator    Not specified
  Maximum Spot price    Not specified
  Spot expiry date    -
  Spot block duration    Not specified
  Shutdown behavior    Not specified
  Termination Protection    Not specified
  Placement Group Name    Not specified
  Tenancy    Not specified
  Tenancy Affinity    Not specified
  Kernel ID    Not specified
  Elastic Graphics    Not specified
  </code></pre>
  
  <p>and use that option in my autoscaling group:</p>
  
  <pre><code>*Fleet Composition - Adhere to the launch template
      The launch template determines the instance type and purchase option (On-Demand or Spot).* 
  </code></pre>
  
  <p>Then after same deployment command I receive another error in CodeDeploy (failed deployment reason):</p>
  
  <pre><code>The IAM role arn:aws:iam::179966034910:role/CodeDeployRole does not give you permission to perform operations in the following AWS service: AmazonAutoScaling. Contact your AWS administrator if you need help. If you are an AWS administrator, you can grant permissions to your users or groups by creating IAM policies.
  </code></pre>
  
  <p>And that is definitely not the case because my role for CodeDeploy have full EC2, Autoscaling and CodeDeploy access.</p>
  
  <hr>
  
  <p>Am I doing something wrong or that are bugs? How can I configure mixed autoscaling group (ondemand+spot instances) with codedeploy using launch templates? Thanks in advance!</p>