Deploying an application to AWS with Terraform and Ansible – Part 2 (Ansible)

In my previous blog post on using Terraform and Ansible to deploy a simple ‘dummy’ application, I showed you how to use Terraform to build a simple two-tier application infrastructure (Web/App + DB). This blog shows how to use Ansible to configure the application on the deployed infrastructure.

Ansible-logo

Ansible

For anyone unfamiliar with Ansible, I think this quote from the Ansible site covers what it is and what it does. For more information on Ansible please visit their site:

App deployment, configuration management and orchestration – all from one system. Ansible is powerful automation that you can learn quickly.

https://www.ansible.com/

Terraform Ansible Provider Plugin

As shown in the previous blog post first we will need the Terraform Ansible Provider Plugin to be able to define Ansible inventory information from within Terraform. This allows us to define groups (i.e. security for bastionhosts, db for database-servers and web for web/app-servers), which we can do to target our specific configuration needs to the different kinds of servers we have in our application infrastructure.

This Ansible Provider Plugin can be found here and needs to be installed in the plugins directory. Which in my case (Ubuntu 18.04) has to be located in ~/.terraform.d/plugins (so in the users home-directory). More information on the location of 3th-party Terraform plugins can be found here.

Ansible Provider Plugin located in home-directory

After deploying your application infrastructure with Terraform the Ansible Inventory information will now be stored in the Terraform state-file (terraform.tfstate) which we can then use in Ansible by using a Ansible dynamic inventory script made for reading directly from terraform state.

Ansible resouce in terraform.tfstate file

Terraform Dynamic Inventory Script

The Terraform Dynamic Inventory Script can now be used to retrieve the information from the state-file.

Retrieving dynamic inventory information from Terraform in Ansible
Example output

This information can now be used in a Ansible playbook and allows for targeting the specific systems in your deployment by using the group specified in the Terraform Ansible resources.

Database server definition in Terraform definition

As you see above we put the database servers in the db groups and now we use this in the Ansible configuration to fully update the instance before installing MySql on the specific server.

Now we are ready to run our Ansible-playbook and further configure our application and thus putting it all together. I’ve built a short bash-script to run all the actions which are needed to deploy the application in a repeatable manner.

Deploy application infrastructure with Terraform and configure with Ansible

As mentioned in the previous blogpost, it is also very easy to destroy the application again if needed by running, which cleans up your full application infrastructure (very useful when not wanting to incur costs during blog post creation 🙂 ) :
terraform destroy -var environment=DEV -var application=APP01 -auto-approve

Demo

Full demo deploying dummy application to AWS using Terraform for application infrastructure and Ansible for configuration management

Have a look at the full demo which shows what the execution of the deployment script looks like and the resulting infrastructure on AWS.

End of part 2 – Please stay tuned!

At a later stage, I will publish the full source code for you to be able to deploy the same kind of ‘dummy’ application. Currently, the example is just deploying a dummy application because I didn’t have the time yet to also set-up and deploy the configuration files for Apache2, MySQL and PHP to build a demo-application, but this will definitely happen in the future.

So please stay tuned for part 3 of this blogpost-series which will talk about how the application deployment developed from this stage on. If you have any specific questions at the moment about the set-up please feel to contact me.


The following two tabs change content below.

Robert Verdam

Consultant at bConn ICT
My main focus is infrastructure (Storage, Networking and Computing), but I'm also very interested in designing and implementing VDI and Server Based Computing-environments.
You Might Also Like
Leave a Reply

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.