Redefine ur Cloud Journey With Terraform and Packer
Tags: Programming, Technology, DevOps, Terraform

Redefine ur Cloud Journey With Terraform and Packer

For most DevOps professionals, creating a VM usually consists of spinning it up on a cloud using Terraform and then using a config management tool (e.g. Ansible or Puppet) or a bootstrap script (e.g. cloud-init) to convert the raw Virtual Machine to a purposeful server.

We all have been doing it for a long time and it works for most cases, but it comes with some drawbacks.

I will give you an example from personal experience. We have a horizontally scalable web server running on GCP using managed instance groups (MIG). Whenever the MIG wants to add new nodes to the group, it takes some time for the node to be ready.

After the server is up, Ansible takes a few minutes to install packages, do configurations, and make the node ready to process requests. For a production server processing huge volumes, the time lag means dropped requests that hamper the customer experience. To work around this, we had to drop the CPU and memory threshold so that the MIG can spin up a new server before it is too late. That also means that there are too many false positives and we’re wasting valuable infrastructure.

https://skatepowerplay.com/rex/Lep-v-Hof-liv-dazn-01.html

https://skatepowerplay.com/rex/Lep-v-Hof-liv-dazn-02.html

https://skatepowerplay.com/rex/Lep-v-Hof-liv-dazn-03.html

https://skatepowerplay.com/rex/Lep-v-Hof-liv-dazn-04.html

https://skatepowerplay.com/rex/Lep-v-Hof-liv-dazn-05.html

https://skatepowerplay.com/rex/Bay-v-Wol-auf-01.html

https://skatepowerplay.com/rex/Bay-v-Wol-auf-02.html

https://skatepowerplay.com/rex/Bay-v-Wol-auf-03.html

https://skatepowerplay.com/rex/Bay-v-Wol-auf-04.html

https://skatepowerplay.com/rex/Bay-v-Wol-auf-05.html

https://skatepowerplay.com/rex/Kol-v-Lev-dfb-01.html

https://skatepowerplay.com/rex/Kol-v-Lev-dfb-02.html

https://skatepowerplay.com/rex/Kol-v-Lev-dfb-03.html

https://skatepowerplay.com/rex/Mil-v-Gen-calcio-tv-01.html

https://skatepowerplay.com/rex/Mil-v-Gen-calcio-tv-02.html

https://skatepowerplay.com/rex/Mil-v-Gen-calcio-tv-03.html

https://skatepowerplay.com/rex/Mil-v-Gen-calcio-tv-04.html

https://skatepowerplay.com/rex/Mil-v-Gen-calcio-tv-05.html

https://skatepowerplay.com/rex/Int-v-Nap-liv-sky8-01.html

https://skatepowerplay.com/rex/Int-v-Nap-liv-sky8-02.html

https://skatepowerplay.com/rex/Int-v-Nap-liv-sky8-03.html

https://skatepowerplay.com/rex/Int-v-Nap-liv-sky8-04.html

https://skatepowerplay.com/rex/Int-v-Nap-liv-sky8-05.html

https://skatepowerplay.com/rex/Frn-v-Sas-liv-oggi-tv-01.html

https://skatepowerplay.com/rex/Frn-v-Sas-liv-oggi-tv-02.html

https://skatepowerplay.com/rex/Frn-v-Sas-liv-oggi-tv-03.html

https://skatepowerplay.com/rex/Ver-v-Sam-foot-it-01.html

https://skatepowerplay.com/rex/Ver-v-Sam-foot-it-02.html

https://skatepowerplay.com/rex/Ver-v-Sam-foot-it-03.html

https://skatepowerplay.com/rex/v-ideo-Parma-inicio-tv-01.html

https://skatepowerplay.com/rex/v-ideo-Parma-inicio-tv-02.html

https://skatepowerplay.com/rex/v-ideo-Parma-inicio-tv-03.html

https://skatepowerplay.com/rex/v-ideo-Bologna-liv-tyc01.html

https://skatepowerplay.com/rex/v-ideo-Bologna-liv-tyc02.html

https://skatepowerplay.com/rex/v-ideo-Bologna-liv-tyc03.html

https://skatepowerplay.com/rex/Mar-v-Ren-lequipe-fr-01.html

https://skatepowerplay.com/rex/Mar-v-Ren-lequipe-fr-02.html

https://skatepowerplay.com/rex/Mar-v-Ren-lequipe-fr-03.html

https://skatepowerplay.com/rex/Mar-v-Ren-lequipe-fr-04.html

https://skatepowerplay.com/rex/Mar-v-Ren-lequipe-fr-05.html

https://skatepowerplay.com/rex/Psg-v-Lorient-tv-fr-01.html

https://skatepowerplay.com/rex/Psg-v-Lorient-tv-fr-02.html

https://skatepowerplay.com/rex/Psg-v-Lorient-tv-fr-03.html

https://skatepowerplay.com/rex/Psg-v-Lorient-tv-fr-04.html

https://skatepowerplay.com/rex/Psg-v-Lorient-tv-fr-05.html

https://skatepowerplay.com/rex/Mon-v-Len-foot-tv-01.html

https://skatepowerplay.com/rex/Mon-v-Len-foot-tv-02.html

https://skatepowerplay.com/rex/Mon-v-Len-foot-tv-03.html

https://skatepowerplay.com/rex/Bor-v-Snt-sur3-fr-01.html

https://skatepowerplay.com/rex/Bor-v-Snt-sur3-fr-02.html

https://skatepowerplay.com/rex/Bor-v-Snt-sur3-fr-03.html

https://skatepowerplay.com/rex/v-ideo-Brest-fcb-01.html

https://skatepowerplay.com/rex/v-ideo-Brest-fcb-02.html

https://skatepowerplay.com/rex/v-ideo-Brest-fcb-03.html

https://skatepowerplay.com/rex/v-ideo-Angers-wcq-01.html

https://skatepowerplay.com/rex/v-ideo-Angers-wcq-02.html

https://skatepowerplay.com/rex/v-ideo-Angers-wcq-03.html

https://phoenixvilleseniorcenter.org/bet1/Lep-v-Hof-liv-dazn-01.html

https://phoenixvilleseniorcenter.org/bet1/Lep-v-Hof-liv-dazn-02.html

https://phoenixvilleseniorcenter.org/bet1/Lep-v-Hof-liv-dazn-03.html

https://phoenixvilleseniorcenter.org/bet1/Lep-v-Hof-liv-dazn-04.html

https://phoenixvilleseniorcenter.org/bet1/Lep-v-Hof-liv-dazn-05.html

https://phoenixvilleseniorcenter.org/bet1/Bay-v-Wol-auf-01.html

https://phoenixvilleseniorcenter.org/bet1/Bay-v-Wol-auf-02.html

https://phoenixvilleseniorcenter.org/bet1/Bay-v-Wol-auf-03.html

https://phoenixvilleseniorcenter.org/bet1/Bay-v-Wol-auf-04.html

https://phoenixvilleseniorcenter.org/bet1/Bay-v-Wol-auf-05.html

https://phoenixvilleseniorcenter.org/bet1/Kol-v-Lev-dfb-01.html

https://phoenixvilleseniorcenter.org/bet1/Kol-v-Lev-dfb-02.html

https://phoenixvilleseniorcenter.org/bet1/Kol-v-Lev-dfb-03.html

https://phoenixvilleseniorcenter.org/bet1/Mil-v-Gen-calcio-tv-01.html

https://phoenixvilleseniorcenter.org/bet1/Mil-v-Gen-calcio-tv-02.html

https://phoenixvilleseniorcenter.org/bet1/Mil-v-Gen-calcio-tv-03.html

https://phoenixvilleseniorcenter.org/bet1/Mil-v-Gen-calcio-tv-04.html

https://phoenixvilleseniorcenter.org/bet1/Mil-v-Gen-calcio-tv-05.html

https://phoenixvilleseniorcenter.org/bet1/Int-v-Nap-liv-sky8-01.html

https://phoenixvilleseniorcenter.org/bet1/Int-v-Nap-liv-sky8-02.html

https://phoenixvilleseniorcenter.org/bet1/Int-v-Nap-liv-sky8-03.html

https://phoenixvilleseniorcenter.org/bet1/Int-v-Nap-liv-sky8-04.html

https://phoenixvilleseniorcenter.org/bet1/Int-v-Nap-liv-sky8-05.html

https://phoenixvilleseniorcenter.org/bet1/Frn-v-Sas-liv-oggi-tv-01.html

https://phoenixvilleseniorcenter.org/bet1/Frn-v-Sas-liv-oggi-tv-02.html

https://phoenixvilleseniorcenter.org/bet1/Frn-v-Sas-liv-oggi-tv-03.html

https://phoenixvilleseniorcenter.org/bet1/Ver-v-Sam-foot-it-01.html

https://phoenixvilleseniorcenter.org/bet1/Ver-v-Sam-foot-it-02.html

https://phoenixvilleseniorcenter.org/bet1/Ver-v-Sam-foot-it-03.html

https://phoenixvilleseniorcenter.org/bet1/v-ideo-Parma-inicio-tv-01.html

https://phoenixvilleseniorcenter.org/bet1/v-ideo-Parma-inicio-tv-02.html

https://phoenixvilleseniorcenter.org/bet1/v-ideo-Parma-inicio-tv-03.html

https://phoenixvilleseniorcenter.org/bet1/v-ideo-Bologna-liv-tyc01.html

https://phoenixvilleseniorcenter.org/bet1/v-ideo-Bologna-liv-tyc02.html

https://phoenixvilleseniorcenter.org/bet1/v-ideo-Bologna-liv-tyc03.html

https://phoenixvilleseniorcenter.org/bet1/Mar-v-Ren-lequipe-fr-01.html

https://phoenixvilleseniorcenter.org/bet1/Mar-v-Ren-lequipe-fr-02.html

https://phoenixvilleseniorcenter.org/bet1/Mar-v-Ren-lequipe-fr-03.html

https://phoenixvilleseniorcenter.org/bet1/Mar-v-Ren-lequipe-fr-04.html

https://phoenixvilleseniorcenter.org/bet1/Mar-v-Ren-lequipe-fr-05.html

https://phoenixvilleseniorcenter.org/bet1/Psg-v-Lorient-tv-fr-01.html

https://phoenixvilleseniorcenter.org/bet1/Psg-v-Lorient-tv-fr-02.html

https://phoenixvilleseniorcenter.org/bet1/Psg-v-Lorient-tv-fr-03.html

https://phoenixvilleseniorcenter.org/bet1/Psg-v-Lorient-tv-fr-04.html

https://phoenixvilleseniorcenter.org/bet1/Psg-v-Lorient-tv-fr-05.html

https://phoenixvilleseniorcenter.org/bet1/Mon-v-Len-foot-tv-01.html

https://phoenixvilleseniorcenter.org/bet1/Mon-v-Len-foot-tv-02.html

https://phoenixvilleseniorcenter.org/bet1/Mon-v-Len-foot-tv-03.html

https://phoenixvilleseniorcenter.org/bet1/Bor-v-Snt-sur3-fr-01.html

https://phoenixvilleseniorcenter.org/bet1/Bor-v-Snt-sur3-fr-02.html

https://phoenixvilleseniorcenter.org/bet1/Bor-v-Snt-sur3-fr-03.html

https://phoenixvilleseniorcenter.org/bet1/v-ideo-Brest-fcb-01.html

https://phoenixvilleseniorcenter.org/bet1/v-ideo-Brest-fcb-02.html

https://phoenixvilleseniorcenter.org/bet1/v-ideo-Brest-fcb-03.html

https://phoenixvilleseniorcenter.org/bet1/v-ideo-Angers-wcq-01.html

https://phoenixvilleseniorcenter.org/bet1/v-ideo-Angers-wcq-02.html

https://phoenixvilleseniorcenter.org/bet1/v-ideo-Angers-wcq-03.html

https://www.visualcom.it/nuk/Mil-v-Gen-calcio-tv-01.html

https://www.visualcom.it/nuk/Mil-v-Gen-calcio-tv-02.html

https://www.visualcom.it/nuk/Mil-v-Gen-calcio-tv-03.html

https://www.visualcom.it/nuk/Mil-v-Gen-calcio-tv-04.html

https://www.visualcom.it/nuk/Mil-v-Gen-calcio-tv-05.html

https://www.visualcom.it/nuk/Int-v-Nap-liv-sky8-01.html

https://www.visualcom.it/nuk/Int-v-Nap-liv-sky8-02.html

https://www.visualcom.it/nuk/Int-v-Nap-liv-sky8-03.html

https://www.visualcom.it/nuk/Int-v-Nap-liv-sky8-04.html

https://www.visualcom.it/nuk/Int-v-Nap-liv-sky8-05.html

https://www.visualcom.it/nuk/Frn-v-Sas-liv-oggi-tv-01.html

https://www.visualcom.it/nuk/Frn-v-Sas-liv-oggi-tv-02.html

https://www.visualcom.it/nuk/Frn-v-Sas-liv-oggi-tv-03.html

https://www.visualcom.it/nuk/Ver-v-Sam-foot-it-01.html

https://www.visualcom.it/nuk/Ver-v-Sam-foot-it-02.html

https://www.visualcom.it/nuk/Ver-v-Sam-foot-it-03.html

https://www.visualcom.it/nuk/v-ideo-Parma-inicio-tv-01.html

https://www.visualcom.it/nuk/v-ideo-Parma-inicio-tv-02.html

https://www.visualcom.it/nuk/v-ideo-Parma-inicio-tv-03.html

https://www.visualcom.it/nuk/v-ideo-Bologna-liv-tyc01.html

https://www.visualcom.it/nuk/v-ideo-Bologna-liv-tyc02.html

https://www.visualcom.it/nuk/v-ideo-Bologna-liv-tyc03.html

https://www.fareva.com/dff/video-m-v-c6fr-201.html

https://www.fareva.com/dff/video-m-v-c6fr-202.html

https://www.fareva.com/dff/video-m-v-c6fr-203.html

https://www.fareva.com/dff/video-m-v-b-fr01.html

https://www.fareva.com/dff/video-m-v-b-fr02.html

https://www.fareva.com/dff/video-m-v-b-fr03.html

https://www.fareva.com/dff/video-m-v-c2-0-1.html

https://www.fareva.com/dff/video-m-v-c2-0-2.html

https://www.fareva.com/dff/video-m-v-c2-0-3.html

https://www.fareva.com/dff/video-m-v-c7-paris201.html

https://www.fareva.com/dff/video-m-v-c7-paris202.html

https://www.fareva.com/dff/video-m-v-c7-paris203.html

https://www.fareva.com/dff/video-m-v-c7-paris204.html

https://www.fareva.com/dff/video-m-v-c7-paris205.html

https://www.fareva.com/dff/Video-rennes-v-OM-fr-1xdz-01.html

https://www.fareva.com/dff/Video-rennes-v-OM-fr-1xdz-02.html

https://www.fareva.com/dff/Video-rennes-v-OM-fr-1xdz-03.html

https://www.fareva.com/dff/Video-rennes-v-OM-fr-1xdz-04.html

https://www.fareva.com/dff/Video-rennes-v-OM-fr-1xdz-05.html

https://www.fareva.com/dff/Livrpool-v-totten-epl01.html

https://www.fareva.com/dff/Livrpool-v-totten-epl02.html

https://www.fareva.com/dff/Livrpool-v-totten-epl03.html

https://www.fareva.com/dff/Livrpool-v-totten-epl04.html

https://www.fareva.com/dff/Livrpool-v-totten-epl05.html

https://www.fareva.com/dff/Video-united-v-crystal-201.html

https://www.fareva.com/dff/Video-united-v-crystal-202.html

https://www.fareva.com/dff/Video-united-v-crystal-203.html

https://www.fareva.com/dff/Fulham-v-Brighton-hq001.html

https://www.fareva.com/dff/Fulham-v-Brighton-hq002.html

https://www.fareva.com/dff/Fulham-v-Brighton-hq003.html

http://trob.be/mrg/video-d-v-be-y0-01.html

http://trob.be/mrg/video-d-v-be-y0-02.html

http://trob.be/mrg/video-d-v-be-y0-03.html

http://trob.be/mrg/video-d-v-be-y0-04.html

http://trob.be/mrg/video-d-v-be-y0-05.html

http://trob.be/mrg/KAA-Gent-Waasland-Beveren-v-en-gb201.html

http://trob.be/mrg/KAA-Gent-Waasland-Beveren-v-en-gb202.html

http://trob.be/mrg/KAA-Gent-Waasland-Beveren-v-en-gb203.html

http://trob.be/mrg/KAA-Gent-Waasland-Beveren-v-en-gb204.html

http://trob.be/mrg/KAA-Gent-Waasland-Beveren-v-en-gb205.html

https://www.rcc.org.uk/vmr/Livrpool-v-totten-epl01.html

https://www.rcc.org.uk/vmr/Livrpool-v-totten-epl02.html

https://www.rcc.org.uk/vmr/Livrpool-v-totten-epl03.html

https://www.rcc.org.uk/vmr/Livrpool-v-totten-epl04.html

https://www.rcc.org.uk/vmr/Livrpool-v-totten-epl05.html

https://www.rcc.org.uk/vmr/Video-united-v-crystal-201.html

https://www.rcc.org.uk/vmr/Video-united-v-crystal-202.html

https://www.rcc.org.uk/vmr/Video-united-v-crystal-203.html

https://www.rcc.org.uk/vmr/Fulham-v-Brighton-hq001.html

https://www.rcc.org.uk/vmr/Fulham-v-Brighton-hq002.html

https://www.rcc.org.uk/vmr/Fulham-v-Brighton-hq003.html

https://www.msae.org/gnj/Video-Barca-v-Real-hd01.html

https://www.msae.org/gnj/Video-Barca-v-Real-hd02.html

https://www.msae.org/gnj/Video-Barca-v-Real-hd03.html

https://www.msae.org/gnj/Video-Barca-v-Real-hd04.html

https://www.msae.org/gnj/Video-Barca-v-Real-hd05.html

https://phoenixvilleseniorcenter.org/vmw/video-m-v-c6fr-201.html

https://phoenixvilleseniorcenter.org/vmw/video-m-v-c6fr-202.html

https://phoenixvilleseniorcenter.org/vmw/video-m-v-c6fr-203.html

https://phoenixvilleseniorcenter.org/vmw/video-m-v-b-fr01.html

https://phoenixvilleseniorcenter.org/vmw/video-m-v-b-fr02.html

https://phoenixvilleseniorcenter.org/vmw/video-m-v-b-fr03.html

https://phoenixvilleseniorcenter.org/vmw/video-m-v-c2-0-1.html

https://phoenixvilleseniorcenter.org/vmw/video-m-v-c2-0-2.html

https://phoenixvilleseniorcenter.org/vmw/video-m-v-c2-0-3.html

https://phoenixvilleseniorcenter.org/vmw/video-m-v-c7-paris201.html

https://phoenixvilleseniorcenter.org/vmw/video-m-v-c7-paris202.html

https://phoenixvilleseniorcenter.org/vmw/video-m-v-c7-paris203.html

https://phoenixvilleseniorcenter.org/vmw/video-m-v-c7-paris204.html

https://phoenixvilleseniorcenter.org/vmw/video-m-v-c7-paris205.html

https://phoenixvilleseniorcenter.org/vmw/Video-rennes-v-OM-fr-1xdz-01.html

https://phoenixvilleseniorcenter.org/vmw/Video-rennes-v-OM-fr-1xdz-02.html

https://phoenixvilleseniorcenter.org/vmw/Video-rennes-v-OM-fr-1xdz-03.html

https://phoenixvilleseniorcenter.org/vmw/Video-rennes-v-OM-fr-1xdz-04.html

https://phoenixvilleseniorcenter.org/vmw/Video-rennes-v-OM-fr-1xdz-05.html

https://phoenixvilleseniorcenter.org/vmw/Livrpool-v-totten-epl01.html

https://phoenixvilleseniorcenter.org/vmw/Livrpool-v-totten-epl02.html

https://phoenixvilleseniorcenter.org/vmw/Livrpool-v-totten-epl03.html

https://phoenixvilleseniorcenter.org/vmw/Livrpool-v-totten-epl04.html

https://phoenixvilleseniorcenter.org/vmw/Livrpool-v-totten-epl05.html

https://phoenixvilleseniorcenter.org/vmw/Video-united-v-crystal-201.html

https://phoenixvilleseniorcenter.org/vmw/Video-united-v-crystal-202.html

https://phoenixvilleseniorcenter.org/vmw/Video-united-v-crystal-203.html

https://phoenixvilleseniorcenter.org/vmw/Fulham-v-Brighton-hq001.html

https://phoenixvilleseniorcenter.org/vmw/Fulham-v-Brighton-hq002.html

https://phoenixvilleseniorcenter.org/vmw/Fulham-v-Brighton-hq003.html

https://phoenixvilleseniorcenter.org/vmw/video-d-v-be-y0-01.html

https://phoenixvilleseniorcenter.org/vmw/video-d-v-be-y0-02.html

https://phoenixvilleseniorcenter.org/vmw/video-d-v-be-y0-03.html

https://phoenixvilleseniorcenter.org/vmw/video-d-v-be-y0-04.html

https://phoenixvilleseniorcenter.org/vmw/video-d-v-be-y0-05.html

https://phoenixvilleseniorcenter.org/vmw/KAA-Gent-Waasland-Beveren-v-en-gb201.html

https://phoenixvilleseniorcenter.org/vmw/KAA-Gent-Waasland-Beveren-v-en-gb202.html

https://phoenixvilleseniorcenter.org/vmw/KAA-Gent-Waasland-Beveren-v-en-gb203.html

https://phoenixvilleseniorcenter.org/vmw/KAA-Gent-Waasland-Beveren-v-en-gb204.html

https://phoenixvilleseniorcenter.org/vmw/KAA-Gent-Waasland-Beveren-v-en-gb205.html

Also, consider the amount it would take to patch every server and ensure that the patches would be right on every node. The complexity and risk increase with the number of nodes.

Well, we figured out that we were doing it wrong and there is a better way: by using Immutable Infrastructure. Now, what do we mean by this term? Well, it means that instead of spinning up infrastructure and configuring it after it is ready, we can pre-bake all configuration within the OS image so that when the machine is up, it is ready. It also means that you are not supposed to configure it any further, as the next time a copy of the machine is launched, it will not have your settings — hence the immutability. It’s the concept of containers within the VM space.

While this concept is not new, there were challenges with the approach because there weren’t tools available then. Luckily, with the modern DevOps and CI/CD practices, we can easily bake a new image and release it when we need to. You can also run tests on the new image before you decide to release it. It helps in reducing a lot of pain that you might incur in production. Rolling back a change is easy as well. Simply deploy the old image. You can do canary deployments with this approach if you need to and do A/B testing with ease.

HashiCorp has an impressive DevOps stack that relates to both managing infrastructure and server configuration. While HashiCorp’s Terraform is the leading multi-cloud Infrastructure as Code solution, HashiCorp Packer provides you with the power to create Immutable Infrastructure.

Packer helps you take OS images from a base, run it in a temporary VM, customise your image by running shell scripts or even Ansible playbooks, test the configuration, build the image, and push it to your image repository. So next time, when you want to release a new configuration, you need to update your instance template.

In this article, we will have a look at some of these features in the hands-on demo. As we work primarily on GCP, we will use the GCP example, but the principle is the same and the tool supports any public cloud platform.