Scroll to top
© 2018 All Rights Reserved.

Creating a Docker Swarm on AWS using Terraform

Dominik Sachsenhofer - 4. October 2017 - 1 comment

What is Terraform?

Terraform is a tool for building, changing, and versioning infrastructure safely and efficiently. Terraform can manage existing and popular service providers as well as custom in-house solutions.

Configuration files describe to Terraform the components needed to run a single application or your entire datacenter. Terraform generates an execution plan describing what it will do to reach the desired state, and then executes it to build the described infrastructure. As the configuration changes, Terraform is able to determine what changed and create incremental execution plans which can be applied.

The infrastructure Terraform can manage includes low-level components such as compute instances, storage, and networking, as well as high-level components such as DNS entries, SaaS features, etc.


1. Requirements

1.1 Docker

See this article on how to install Docker.

1.2 AWS Command Line Interface

See this article on how to install the AWS Command Line Interface.

1.3 Packer

See this article on how to install Packer.

1.4 Terraform

See this article on how to install Terraform.

1.5 (Optional) Dynatrace Account

See for more information.

2. Create a dockerized microservice

2.1 Required Software:

  • docker (1.6.0 or above)
  • python (2.7 or above)
  • ubuntu

2.2 Create a simple Python Flask App (python_example)

Create a folder python_example. All our files will be inside this directory.

Create a new file inside python_example and add the following python code.

File /python_example/

from flask import Flask

app = Flask(__name__)

def hello_world():
return 'SUCCESS: This Flask application is dockerized.'

def health():
return 'OK'

if __name__ == '__main__':, host='')

2.3 Requirements File:

Requirements file states the software required to be installed in the container. Create a file requirements.txt inside python_example folder.

File: /python_example/requirements.txt



2.4 Dockerfile

This file is needed to create a docker image:

File: /python_example/Dockerfile

FROM ubuntu:latest
MAINTAINER Dominik Sachsenhofer ""
RUN apt-get update
RUN apt-get install -y python-pip python-dev build-essential wget curl
COPY . /app
RUN pip install -r requirements.txt
ENTRYPOINT ["python"]
CMD [""]



2.5 Build the Docker Image

Run the following command to build the docker image python_example from python_example directory.
$ docker build -t python_example:latest .

2.6 Run the Docker Container

$ docker run -d -p 5000:5000 python_example

2.7 Optional

Connect Docker with your GitHub Repository: Create an GitHub account and push your work to a repository called “python_example”. Create an Docker account and create a new Docker Image and connect the image to your GitHub repository “python_example”. Whenever you push code into your GitHub repository, the corresponding Docker Image is rebuilt.



Dockerized microservice: sachsenhofer/python_example

3. Set up your Environment

3.1 Create a new Key Pair

Create a new Key Pair in your Amazon AWS EC2 Management Console. You will get a file with filename [AWS_SSH_KEY_ID].pem. See this article for more information. 


3.1 Set your environment variables

nano ~/.bash_profile
Append the following lines:
export AWS_DEFAULT_REGION=eu-central-1


3.2 Clone this repository:

git clone

4. Build an AWS AMI with Docker using Packer

Creating a AMI with docker preinstalled will speed up the creation of the docker swarm as all the nodes will be exactly the same.

4.1 Create a VPC

cd packer
terraform plan
terraform apply


4.2 Get the values of [AWS_VPC_ID], [AWS_SUBNET_ID] and [AWS_SECURITY_GROUP_ID]

terraform show


4.3 Create the AMI

packer build \
       -var aws_vpc_id=[AWS_VPC_ID] \
       -var aws_subnet_id=[AWS_SUBNET_ID] \
       -var aws_security_group_id=[AWS_SECURITY_GROUP_ID] \


4.4 Teardown the VPC

terraform destroy -force

5. A deep dive into Terraform

How to setup Terraform configuration files.


6. Creating the Swarm

Use terraform to create several masters and slaves.

Get the AMI name

aws ec2 describe-images --owners self --filters "Name=name,Values=docker*" --query 'Images[*].[ImageId,Name,CreationDate]' --output text


Test the configuration

cd swarm
terraform plan -var 'amis={eu-central-1 = "..." }' \
               -var aws_region=$AWS_DEFAULT_REGION \
               -var ssh_key_filename=$AWS_SSH_KEY \
               -var ssh_key_name=$AWS_SSH_KEY_ID


Build the swarm

terraform apply -var 'amis={eu-central-1 = "..." }' \
                -var aws_region=$AWS_DEFAULT_REGION \
                -var ssh_key_filename=$AWS_SSH_KEY \
                -var ssh_key_name=$AWS_SSH_KEY_ID


Teardown the swarm

terraform destroy -force \
                  -var 'amis={eu-central-1 = "..." }' \
                  -var aws_region=$AWS_DEFAULT_REGION \
                  -var ssh_key_filename=$AWS_SSH_KEY \
                  -var ssh_key_name=$AWS_SSH_KEY_ID

6. Using the Swarm

Log into the swarm manager

6.1 View services

docker service ls


6.1 Docker Cloud stack

A stack is a collection of services that make up an application in a specific environment. Learn more about stacks for Docker Cloud here. A stack file is a file in YAML format that defines one or more services, similar to a docker-compose.yml file for Docker Compose but with a few extensions. The default name for this file is docker-cloud.yml.

Looking for information on stack files for Swarm? A good place to start is the Compose reference file, particularly the section on deploy key and its sub-options, and the reference on Docker stacks. Also, the new Getting Started tutorial demos use of a stack file to deploy an application to a swarm.

6.2 Stack file example

Below is an example docker-stack.yml:
File: docker-stack.yml

version: "3"

    image: redis:alpine
      - "6379"
      - frontend
      replicas: 2
        parallelism: 2
        delay: 10s
        condition: on-failure

    image: postgres:9.4
      - db-data:/var/lib/postgresql/data
      - backend
        constraints: [node.role == manager]

    image: sachsenhofer/python_example:latest
      - 5000:5000
      - frontend
      - redis
      replicas: 2
        parallelism: 2
        condition: on-failure




6.3 Deploying the stack

docker stack deploy --compose-file docker-stack.yml python_example


6.4 View the stack

docker stack ls

7. Install Dynatrace OneAgent on Linux hosts

Run the installation script on each Linux machine that you want to monitor.

7.1 Use this command on the target host:

wget -O "[API_TOKEN]&arch=x86"

7.2 Verify signature:

wget ; ( echo 'Content-Type: multipart/signed; protocol="application/x-pkcs7-signature"; micalg="sha-256"; boundary="--SIGNED-INSTALLER"'; echo ; echo ; echo '----SIGNED-INSTALLER' ; cat ) | openssl cms -verify -CAfile dt-root.cert.pem > /dev/null

7.3 And run the installer with root rights:


Restart the processes that you want to monitor.


Terraform is a powerful tool to programmatically create infrastructure (infrastructure-as-code) and to automate the deployment of dockerized images and services.

1 comment

  1. John

    Good article! Worked for me!

Post a Comment