The github repository for this project is here!.
Introduction
Using AWS CDK
we can quickly deploy a small EC2 Instance
to act as an FTP Server with an unlimited S3 storage backend.
With an hourly rate of only $0.0058
(us-east-1), t2-nano
is sufficient for running a small FTP server with the S3 integration!
The server is configured on deploy using EC2 UserData. mount-s3
is used for mounting an S3 bucket to your instance. We start up VSFTPD
, set up a virtual user in the same directory as our mounted S3 bucket, and we have our end-to-end FTP solution!
Architecture
- A user connects to the instance with the instance's Public IP or DNS.
- The user agrees to use the server's TLS certificate.
- A secure FTPS connection is made and the user can freely work with the files on the S3 bucket.
CDK Structure
You'll find the code for the stack in lib/ftp-server-stack.ts
. It uses four
custom Constructs
:
-
VPCStuff
creates a simpleVPC
with noNAT Gateways
since they aren't necessary and we want to save on cost (NAT Gateways
are expensive!) -
StorageStuff
creates a bucket to persist data from the FTP server. It's set to automatically delete data and remove itself oncdk destroy
. -
SecurityStuff
creates anIAM Role
with permissions to read and write to the FTP bucket and access theSecret
that stores the password for theftpUser
account we will create on the EC2 instance.SecurityStuff
also creates anEC2 Security Group
inside ourVPC
which allowsSSH
andFTP
access into the EC2 instance. Additionally, we open ports which will be used for the data connection withVSFTPD
- Finally,
ec2InstanceStuff
creates and configures the EC2 instance. The instance is based onAmazon Linux 2
The EC2 instance Public IP and the FTP Bucket name gets output upon successful deploy.
Method
AWS CDK
is used to synthesize the CloudFormation
template that will deploy our architecture and also configure the server.
We configure the EC2 instance using EC2 UserData stored as a bash script in lib/ec2-setup.sh
. We also use CloudFormationInit
to store a slightly customized vsftpd.conf
file.
Things you need to do yourself
- Make a secret on
AWS Secrets Manager
for your ftp user password. You'll use this when connecting to the server. - Fix references to your secret. I named by secret "ftpUser_Password". You'll find references to this in the
ec2InstanceStuff
andsecurityStuff
constructs inlib/ftp-server-stack.ts
- Create a
Key Pair
using theAWS EC2 Console
. You'll need this to access the instance by SSH. - Fix references to the
Key Pair
. I've named mine"RiceCooker_FTP_Server"
- this needs to match theKey Pair
that you just made. - Copy the repo and
cdk deploy
!
Conclusion
In researching this solution I ran into many different ways people were trying to make an affordable FTP server. Some used ECS, others were deploying docker containers onto an EC2 instance (main inspiration). I always prefer a repeatable approach using CDK
, so I combined a number of these posts into something that is easy for anyone to deploy.
I hope you found this useful - let me know!
Top comments (0)