How to attach EBS Docker Volume to AWS ECS using CDK

Nir Hadassi
Aspecto
Published in
2 min readJun 4, 2020

Wow, lot’s of abbreviations in the title, let’s get things straight first:
• EBS— Elastic Block Storage.
• CDK — Cloud Development Kit.
• ECS — Elastic Container Service.

Bunch of logos

I won’t go into long intros, if you’re here you probably know why.
This post will walk you through the required steps to get EBS working, the same way we achieved this at Aspecto.
I’d be using JS CDK snippets.
Notice EBS will only work with EC2 launch type (Fargate won’t support it).

Steps Overview:
• Install rexray/ebs docker plugin using “UserData”.
• Give your EC2 instance the required roles.
• Create and attach EBS volume.

First, we need the get a hold of the cluster AutoScalingGroup:

Install REX-Ray EBS plugin

Now that we have the AutoScalingGroup, we can attach “UserData” to it.
UserData is basically a bash script that runs when the EC2 instance is instantiated.
This script will install the REX-Ray EBS plugin and will reset our ECS after.

Give your EC2 instance the required roles

Your EC2 gonna manage your EBS, it gonna need some roles to do it:

Attach EBS Docker Volume to your TaskDefinition

Cool, our ECS cluster can now work with EBS, all that’s left is attaching the EBS volume and mount it to our container:

That’s it, you’re done!

Let me know in the comments below how it worked out for you, I’m here to answer every question.

--

--