Skip to content

Deploy Docker Swarm on AWS EC2 via cloud-formation templates - Step 2 - Storage

In this step we will create a shared EBS volume which will be used as a home directory for the Linux users.

This post is part of a thread that includes these steps:

  1. Network Setup
  2. Storage (this post)
  3. Roles
  4. Manager Instance
  5. Worker Launch Template
  6. Worker Instances
  7. Docker Swarm
  8. Cleanup

Elastic Block Storage (AWS EBS)

Start in the project directory:

cd ~/swift-aws-ec2-swarm

cloud-formation Template

Create a folder ebs and a ebs.yml file in it.

mkdir -p ebs
touch ebs/ebs.yml
nano ebs/ebs.yml

Copy and paste this code into ebs.yml:

Description: Shared EBS volumes for the Docker Swarm instances  

Resources:
  HomeVolume:
    Type: 'AWS::EC2::Volume'
    Properties:
      VolumeType: io2
      Iops: 3000      
      Size: 30
      Encrypted: true
      AvailabilityZone: !Select [ 0, !GetAZs '' ]
      MultiAttachEnabled: true
      # categories:
        - Key: Name
          Value: shared-volume-home

Outputs:
  HomeVolumeId:
    Description: The ID of the created home EBS volume
    Value: !Ref HomeVolume

Scripts

Next add a script deploy-ebs.sh and paste this code in it:

#!/usr/bin/env bash

# switch to parent directory
script_path=`dirname ${BASH_SOURCE[0]}`
pushd $script_path/..

source config/names.sh

echo
echo "Deploying $stack_ebs stack via cloud-formation:"
echo 'https://us-west-2.console.aws.amazon.com/cloudformation/home'
echo

set -x

aws cloudformation deploy \
    --template-file ebs/ebs.yml \
    --stack-name $stack_ebs

popd

Let's also add a clean up script rm-ebs.sh:

#!/usr/bin/env bash


# switch to parent directory
script_path=`dirname ${BASH_SOURCE[0]}`
pushd $script_path/..

source config/names.sh

echo
echo "Destroying $stack_ebs stack via cloud-formation:"
echo 'https://us-west-2.console.aws.amazon.com/cloudformation/home'
echo

set -x

aws cloudformation delete-stack \
    --stack-name $stack_ebs 

aws cloudformation wait stack-delete-complete \
    --stack-name $stack_ebs

Make the scripts executable:

chmod +x ebs/deploy-ebs.sh 
chmod +x ebs/rm-ebs.sh

Deploy

Finally let's run the "deploy" script to create the EBS volumes:

./ebs/deploy-ebs.sh

You should see output similar to this:

Deploying swift-swarm-ebs stack via cloud-formation:
https://us-west-2.console.aws.amazon.com/cloudformation/home

+ aws cloudformation deploy --template-file ebs/ebs.yml --stack-name swift-swarm-ebs

Waiting for changeset to be created..
Waiting for stack create/update to complete
Successfully created/updated stack - swift-swarm-ebs

At this point your project structure should look like this:

.
├── config
│   └── names.sh
├── ebs
│   ├── deploy-ebs.sh
│   ├── ebs.yml
│   └── rm-ebs.sh
└── vpc
    ├── deploy-vpc.sh
    ├── rm-vpc.sh
    └── vpc.yml

Congratulations!

We are done with Step 2. Storage.

Next step is: Step 3. Roles