Before you go, check out these stories!

Hackernoon logoSetup Docker Swarm on AWS using Ansible & Terraform by@mlabouardy

Setup Docker Swarm on AWS using Ansible & Terraform

Author profile picture

@mlabouardyMohamed Labouardy

This post is part of โ€œIaCโ€ series explaining how to use Infrastracture as Code concepts with Terraform. In this part, I will show you how to setup a Swarm cluster on AWS using Ansible & Terraform as shown in the diagram below (1 Master and 2 Workers) in less than 1 min:

All the templates and playbooks used in this tutorial, can be found on my Github.

Note: I did some tutorials about how to get started with Terraform on AWS, so make sure you read it before you go through this post.

1โ€Šโ€”โ€ŠSetup EC2 Cluster using Terraform

1.1โ€Šโ€”โ€ŠGlobal Variables

This file contains environment specific configuration like region name, instance typeย โ€ฆ

1.2โ€Šโ€”โ€ŠConfig AWS as Provider

1.3โ€Šโ€”โ€ŠSecurity Group

This SG allows all the inbound/outbound traffic:

1.4โ€Šโ€”โ€ŠEC2 Instances

Bootstrap script to install latest version of Docker:

2โ€Šโ€”โ€ŠTransform to Swarm Cluster with Ansible

The playbook is self explanatory:

Now we defined all the required templates and playbook, we only need to type 2 commands to bring up the swarm cluster:

|terraform apply

|ansible -i hosts playbook.yml

Note: Make sure to update the hosts file with the public ip of each EC2 instance.

Setting up the Swarm cluster in action is show below:


Join Hacker Noon

Create your free account to unlock your custom reading experience.