How to set the instance type with Elastic Beanstalk?

The setenv command is for changing Environment Variables. Hence the command you tried is bash equivalent of:

export InstanceType=t2.medium

And doesnt really do anything for your beanstalk environment.

You can create an environment using the -i option during create

eb create -i t2.micro

Or, you can use eb config to edit a currently running environment. This will open up a text editor. Look for the section that looks like:

aws:autoscaling:launchconfiguration:
    IamInstanceProfile: aws-elasticbeanstalk-ec2-role
    EC2KeyName: aws
    InstanceType: t1.micro

And edit the t1.micro to t2.micro. (save and quit)


But just to make your life easier, you can save the below as .elasticbeanstalk/saved_configs/default.cfg.yml and the CLI will use all these settings on all future creates.

AWSConfigurationTemplateVersion: 1.1.0.0
OptionSettings:
  aws:elb:loadbalancer:
    CrossZone: true
  aws:elasticbeanstalk:command:
    BatchSize: '30'
    BatchSizeType: Percentage
  aws:autoscaling:launchconfiguration:
    IamInstanceProfile: aws-elasticbeanstalk-ec2-role
    EC2KeyName: aws
    InstanceType: t2.micro
  aws:elb:policies:
    ConnectionDrainingEnabled: true
  aws:autoscaling:updatepolicy:rollingupdate:
    RollingUpdateType: Health
    RollingUpdateEnabled: true
  aws:elb:healthcheck:
    Interval: '30'

More scriptable way:

aws elasticbeanstalk update-environment --environment-name "your-env-name" --option-settings "Namespace=aws:autoscaling:launchconfiguration,OptionName=InstanceType,Value=t2.micro"

The accepted solution didn't work for me in 2020.

As of today (26th, February 2020), in my .ebextensions/02_python.config I had to add the following under option_settings:

option_settings:
  # ...

  aws:ec2:instances:
    InstanceTypes: 'm5.large'

Reference: https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/using-features.managing.as.html#environments-cfg-autoscaling-namespace.instances