Set IIS Idle Timeout Automatically on Elastic Beanstalk
So, you’re running an ASP.NET app in IIS on Elastic Beanstalk and trying to figure out how to keep the app from shutting down due to idleness. Read on, my friend. The answer to your problem lies below.
As you’re no doubt aware, low-traffic ASP.NET web apps can time out on IIS due to idleness. This is the server’s way of helping you conserve resources and make sure the machine is available to serve higher demand applications. That’s great, but the logic doesn’t really apply in Elastic Beanstalk.
On Elastic Beanstalk, there’s only one app running on your instance. There’s no need to conserve resources because there’s nothing else running. As long as the instance is online, you’re paying for it, so there’s no reason to let the app shut down due to idleness.
On graphical Windows with IIS, you might go into the settings on the App Pool and tell it not to timeout. There’s a couple reasons why that won’t work in Elastic Beanstalk. First, the Windows instance you’re given is Windows Core. There’s really no GUI to point-and-click your way to success. Secondly, and more importantly, your Elastic Beanstalk deployment should be built in such a way that it can be rebuilt at a moment’s notice. You can’t have steps in your deployment that require you to manually login to the machine in order to configure settings. No. You need a way to do it automatically when the machine is deployed and .ebextensions is that way.
.ebextensions to the Rescue. Again.
As we talked about in the New Relic with Elastic Beanstalk,
.ebextensions is a mechanism that allows for automatic configuration and setup of instances during deployment.
For IIS, to keep the app from shutting down, there’s one primary settings we may want to change:
idleTimeout. Since we don’t have a GUI, we’re going to use the appcmd.exe command-line tool to set this property during deployment. Here’s the entirety of the .ebextensions
commands: setIdleTimeoutToZero: cwd: "C:\\windows\\system32\\inetsrv" command: "appcmd set apppool /apppool.name:DefaultAppPool /.processModel.idleTimeout:0.00:00:00"
In this file, there is one command to run, labeled
setIdleTimeoutToZero. The command has two subparts,
cwd simply changes the working directory. But
command is where the meat is, so let’s look at it.
This line is the full command you would run if you were using a command line rather than the GUI. We’re telling
appcmd to set a property on an app pool. First, it wants the name of the app pool (
/apppool.name:DefaultAppPool), which is just the default name of an app pool for Elastic Beanstalk. Yours is the same unless you’ve somehow changed it. Then comes the property we want to set,
/.processModel.idleTimeout. Setting this to zero will keep the pool from timing out. The default is every 20 minutes.
This works on both IIS 7.X and 8.X. If you’re running a Windows Server 2012 R2 and above platform, there’s another setting you can use called “Idle Time-out Action”. Setting to
Suspend will — instead of terminating the app down after 20 minutes — page the process off to disk. If this is what you’d prefer to do, the command is:
appcmd set apppool /apppool.name:DefaultAppPool /.processModel.idleTimeoutAction:1
1 in this option is “Suspend”.
0 (the default) is “Terminate”.
There are so many more processModel options you can set, so take a look at them if there’s something else you need to set.