AWS Compute Blog
Running Swift Web Applications with Amazon ECS
This is a guest post from Asif Khan about how to run Swift applications on Amazon ECS.
—–
Swift is a general-purpose programming language built using a modern approach to safety, performance, and software design patterns. A goal for Swift is to be the best language for uses ranging from systems programming, to mobile and desktop applications, scaling up to cloud services. As a developer, I am thrilled with the possibility of a homogeneous application stack and being able to leverage the benefits of Swift both on the client and server side. My code becomes more concise, and is more tightly integrated to the iOS environment.
In this post, I provide a walkthrough on building a web application using Swift and deploying it to Amazon ECS with an Ubuntu Linux image and Amazon ECR.
Overview of container deployment
Swift provides an Ubuntu version of the compiler that you can use. You still need a web server, a container strategy, and an automated cluster management with automatic scaling for traffic peaks.
There are some decisions to make in your approach to deploy services to the cloud:
- HTTP server
Choose a HTTP server which supports Swift. I found Vapor to be the easiest. Vapor is a type-safe web framework for Swift 3.0 that works on iOS, MACOS, and Ubuntu. It is very simple and easy to deploy a Swift application. Vapor comes with a CLI that will help you create new Vapor applications, generate Xcode projects and build them, as well as deploy your applications to Heroku or Docker. Another Swift webserver is Perfect. In this post, I use Vapor as I found it easier to get started with.
Tip: Join the Vapor slack group; it is super helpful. I got answers on a long weekend which was super cool.
- Container model
Docker is an open-source technology that that allows you to build, run, test, and deploy distributed applications inside software containers. It allows you to package a piece of software in a standardized unit for software development, containing everything the software needs to run: code, runtime, system tools, system libraries, etc. Docker enables you to quickly, reliably, and consistently deploy applications regardless of environment.
In this post, you’ll use Docker, but if you prefer Heroku, Vapor is compatible with Heroku too. - Image repository
After you choose Docker as the container deployment unit, you need to store your Docker image in a repository to automate the deployment at scale. Amazon ECR is a fully-managed Docker registry and you can employ AWS IAM policies to secure your repositories. - Cluster management solution
Amazon ECS is a highly scalable, high performance container management service that supports Docker containers and allows you to easily run applications on a managed cluster of Amazon EC2 instances. ECS eliminates the need for you to install, operate, and scale your own cluster management infrastructure.
With ECS, it is very easy to adopt containers as a building block for your applications (distributed or otherwise) by skipping the need for you to install, operate, and scale your own cluster infrastructure. Using Docker container within ECS provides flexibility to schedule long-running applications, services, and batch processes. ECS maintains application availability and allows you to scale containers.
To put it all together, you have your Swift web application running in a HTTP server (Vapor), deployed on containers (Docker) with images are stored on a secure repository (ECR) with automated cluster management (ECS) to scale horizontally.
Prepare an AWS Account
- If you don’t already have an AWS account, create one at http://thinkwithwp.com by following the on-screen instructions.
- Use the region selector in the navigation bar to choose the AWS Region where you want to deploy Swift web applications on AWS.
- Create a key pair in your preferred region.
Walkthrough
The following steps are required to set up your first web application written in Swift and deploy it to ECS:
- Download and launch an instance of the AWS CloudFormation template. The CloudFormation template installs Swift, Vapor, Docker, and the AWS CLI.
- SSH into the instance.
- Download the vapor example code
- Test the Vapor web application locally.
- Enhance the Vapor example code to include a new API.
- Push your code to a code repository
- Create a Docker image of your code.
- Push your image to Amazon ECR.
- Deploy your Swift web application to Amazon ECS.
Detailed steps
- Download the CloudFormation template and spin up an EC2 instance. The CloudFormation has Swift , Vapor, Docker, and git installed and configured. To launch an instance, launch the CloudFormation template from here.
- SSH into your instance:
ssh –i ec2-user@<bastion host ip>
- Download the Vapor example code – this code helps deploy the example you are using for your web application:
git clone https://github.com/awslabs/ecs-swift-sample-app.git
- Test the Vapor application locally:
- Build a Vapor project:
cd ~/ecs-swift-sample-app/example \ vapor build
- Run the Vapor project:
vapor run serve --port=8080
- Validate that server is running (in a new terminal window):
ssh -i ubuntu@ curl localhost:8080
- Build a Vapor project:
- Enhance the Vapor code:
- Follow the guide to add a new route to the sample application: https://Vapor.readme.io/docs/hello-world
- Test your web application locally:
vapor run serve --port=8080 curl http://localhost/hello.
- Commit your changes and push this change to your GitHub repository:
git add –all git commit –m git push
- Build a new Docker image with your code:
docker build -t swift-on-ecs \ --build-arg SWIFT_VERSION=DEVELOPMENT-SNAPSHOT-2016-06-06-a \ --build-arg REPO_CLONE_URL=https://github.com/awslabs/ecs-swift-sample-app.git/ \ ~/ ecs-swift-sample-app/example
- Upload to ECR: Create an ECR repository and push the image following the steps in Getting Started with Amazon ECR.
- Create a ECS cluster and run tasks following the steps in Getting Started with Amazon ECS:
- Be sure to use the full registry/repository:tag naming for your ECR images when creating your task. For example, aws_account_id.dkr.ecr.us-east-1.amazonaws.com/my-web-app:latest.
- Ensure that you have port forwarding 8080 set up.
- You can now go to the container, get the public IP address, and try to access it to see the result.
- Open your running task and get the URL:
- Open the public URL in a browser:
- Open your running task and get the URL:
Your first Swift web application is now running.
At this point, you can use ECS with Auto Scaling to scale your services and also monitor them using CloudWatch metrics and events.
Conclusion
If you want to leverage the benefits of Swift, you can use Vapor as the web container with Amazon ECS and Amazon ECR to deploy Swift web applications at scale and delegate the cluster management to Amazon ECS.
There are many interesting things you could do with Swift beyond this post. To learn more about Swift, see the additional Swift libraries and read the Swift documentation.
If you have questions or suggestions, please comment below.