Three Simple Steps to Save Costs when Prototyping with App Engine Flexible Environment
--
If you’ve ever used Google App Engine, you know it’s one of the fastest ways to get from idea to working prototype. As long as you conform to the sandbox restrictions, you don’t need to set up servers, install packages, or do any of the tedious DevOps tasks that slow you down.
With the introduction of the App Engine Flexible Environment (formerly known as Managed VMs), Google lifted many of the sandbox restrictions and added more built-in runtimes including Node.js and Ruby. You can even customize everything by specifying your own Dockerfile!
However, this flexibility comes at a price. The Flexible Environment is slower to deploy and can’t scale as quickly as the Standard Environment. The default deployment is also overkill for prototyping.
The biggest difference in my opinion is the lack of “scale to zero.” With App Engine Standard, if no one is using your application it shuts everything down. The moment a user visits, App Engine spins up an instance in milliseconds to serve the new request. Combined with the generous free tier, you don’t really have to worry about infrastructure costs for prototypes. Currently, the Flexible Environment needs at least one instance running to serve traffic and there is no free tier.
Let’s take a look at some best practices for prototyping with the Flexible Environment that can minimize the cost.
2019 Update: I highly recommend using Cloud Run instead of App Engine Flex for most tasks. In my opinion, it blends the best from App Engine Standard (pay per use, scale to zero) with App Engine Flex (flexibility, Dockerfiles). The only major advantage Flex has is the larger instance sizes.
The Default Deployment
Let’s launch a Node.js app on App Engine Flexible Environment. The default app.yaml looks something like this:
runtime: nodejs
env: flex
Deploy it with the gcloud command:
$ gcloud app deploy
After deploying the app, we can check out the “Instances” section of App Engine to see the following:
By default, it launches two n1-standard-1 VMs. This is designed to provide higher reliability.
Let’s look at the monthly cost for this deployment. I’ve configured the this default deployment in the Google Cloud Pricing Calculator here.
It’s over $80 a month!
While this price would be fine if you were serving production traffic, it’s pretty ridiculous in the prototyping stage.
Step One: Reduce number of instances
We can cut our cost in half by launching one instance instead of two. Do this by turning on manual scaling (you don’t need autoscaling for a prototype) and setting the instances to 1. You can read more about scaling here.
Modify the app.yaml:
runtime: nodejs
env: flex
manual_scaling:
instances: 1
This will cut our cost from around $80 to $40! Nice!
Still, this is very expensive for a prototype. Typically, a prototype only needs minimal resources to run, so we can optimize further.
Step Two: Use smaller instances
Unfortunately this step is not possible at this time! Hopefully, this option will be brought back in the future.
#While the g1-small is pretty small already, we can go even smaller.
#The f1-micro is a perfect instance for prototypes. It has enough RAM and #CPU to run most prototype-level workloads, so you are not spending #money on unused resources.
runtime: nodejs
env: flexmanual_scaling:
instances: 1#resources:
# cpu: .5
# memory_gb: 0.18
# disk_size_gb: 10
#Now the price drops from $40 to just $15! Very Nice!
Step Three: Spin down dev instances
If you are working on a prototype, chances are you don’t need it running 24/7. By default, App Engine will deploy a new version every time. This makes rolling back to an old version or traffic splitting and A/B testing super easy, but is not needed for a prototype.
Modify your deploy command to the following:
$ gcloud app deploy --version dev
When you are finished working for the day, you can spin down your instance with the following command:
$ gcloud app versions stop dev
And you can start it back up just as easily:
$ gcloud app versions start dev
Let’s say you work 10 hours a day for 6 days a week because you are a 10x engineer. If you shut down App Engine when you go home, the total cost drops to $15–20 a month!
Conclusion
By following these three steps, we have dropped the cost of App Engine Flexible from $80 to $20.
That’s a 85% decrease in price!
While it is still not free like App Engine Standard, I feel it is much more reasonable.
The best part: you don’t have to make a single code or operations change to take your prototype to production scale. Just switch back to auto scaling and change your CPU and RAM requirements in the app.yaml, and you are good to go!
So remember: When prototyping use a single small instance and shut it down!
Appendix
app.yaml:
runtime: nodejs
env: flexmanual_scaling:
instances: 1#resources:
# cpu: .5
# memory_gb: 0.18
# disk_size_gb: 10
Deploy:
$ gcloud app deploy --version dev
Stop:
$ gcloud app versions stop dev
Start:
$ gcloud app versions start dev