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:
- Network Setup
- Storage (this post)
- Roles
- Manager Instance
- Worker Launch Template
- Worker Instances
- Docker Swarm
- Cleanup
Elastic Block Storage (AWS EBS)
Start in the project directory:
cloud-formation Template
Create a folder ebs
and a ebs.yml
file in it.
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:
Deploy
Finally let's run the "deploy" script to create the EBS volumes:
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