Terraform EC2 Web Server

    In a previous post, I talked about using Terraform to spin up AWS instances.  Not to rehash what was already written, if you want to see the mechanics of account creation, permissions, and the basic server setup, please look to that post.  This post will expand on the basic server by executing a post install script that performs a number of tasks.  Really, this is where you can automate a ton and save time. 

    First, let's take a look at the install script.  Full disclosure, this process was already dialed in but the cyber gods feel the need to make changes and these are the latest working products. 

    When writing the post install script, keep in mind that you have to answer any questions in advance.  Meaning that if during an install script, if there's a prompt for Y, N, or whatever, we need to address that in the script.  For example, apt-get install -y apache2. 

    Or for the iptables 'save' question that comes up in the end, those are dealt with in the "parameters for install". 



    This is a basic Terraform config with some minor changes.  First, we've added the variable for the private key.  This will prompt us when we initiate 'terraform apply' and 'terraform plan'.  We could bake these into the config but I want to use different keys for different servers. 


    Standard ingress and egress rules:


    And finally, we need to add the SSH information and the install script:


    The server builds and upon completion, it runs the install script.  With some slight modifications, I can repeatedly roll out identical servers.  It requires a little bit of work upfront but totally worth it and it saves a bunch of time for repetitive tasks. 


    © 2020 sevenlayers.com