Search code examples
amazon-web-servicesdockerterraformdocker-swarmterraform-provider-aws

How to pass docker swarm manager token to worker nodes in AWS using Terraform


I am trying to create a Docker Swarm cluster using Terraform, I created 1 manager and 3 worker nodes. I installed Docker and initiated the Docker Swarm and created the manager token.

How can I forward the key from my manager to worker nodes, all the servers running in AWS, and running the terrafrom apply command on my local machine?

I have multiple restrictions, as I can't request for new services for this particular task.

Sorry failed to mention, I have to use Dynamic IP as the entire environment is build using dynamic IP and no IP locked to any specific resource.


Solution

  • The swarm manager needs a way of passing the worker token to the workers once it has initialised. The best way to do that would be to have the swarm manager's userdata trigger generating the token and putting it into a shared store that they can both access. The simplest thing for this in AWS would be to use AWS SSM Parameter Store which allows you to store smallish strings optionally encrypted and backed by normal IAM permissions.

    You would need to give the swarm manager instance permission via an IAM instance profile to write a token to something like /swarm/token/worker and then allow the worker instances permission to read that same token.

    Then in your manager's userdata script you'd want to have something like:

    WORKER_TOKEN=$(docker swarm join-token worker)
    aws ssm put-parameter --region us-west-2 --name '/swarm/token/worker' --type SecureString --value "${WORKER_TOKEN}"
    

    In your worker's userdata script you'd then want to have the equivalent read and execute:

    WORKER_TOKEN=$(aws ssm get-parameter --region us-west-2 --name '/swarm/token/worker' --with-decryption --query 'Parameter.Value' --output text)
    eval "${WORKER_TOKEN}"
    

    There is also a community module that has an example of how to run Docker Swarm on AWS that relies on putting the secret token into S3 and then retrieving it on the worker node with the userdata script. That may give you more tips on how to get a Swarm cluster running nicely on AWS.