--- title: Microservices deployment on Amazon EC2 Container Service with Weave Net excerpt: Weave Net & Weave Run provides a simpler way to run applications on the EC2 Container Service tags: aws, load-balancing, dns, packer, ecs, amazon-linux, microservices, weave scope --- **Note:** You are looking at our `old-guides` repository. The guides in here haven't been updated in a while. They might or might not work for you. We are keeping them around for informational purposes. --- [Amazon EC2 container service](https://aws.amazon.com/ecs/) is a scalable container management service that allows you to manage Docker containers on a cluster of Amazon EC2 instances. Weave Net provides a simple and robust software-defined network for apps running as Docker containers that does not require a external database (cluster store). Weave simplifies setting up a container network within the Amazon EC2 Container Service. Because Weave allows each container to use standard port numbers -- for example you can expose MySQL on port 3306 -- managing services is straightforward. Every container can find the IP of any other container using a simple DNS query on the container's name, and communicate directly without NAT or complicated port mapping. An advantage to using DNS is that when you use a container name within (say) a config file, you are not required to have a script in place to generate the name based on run-time variables. You can also optionally burn the config file with the hostname right into the container image. There is no need to deploy extra services to achieve DNS lookup and load balancing. Weave Net takes care of both automatic service discovery and load balancing, reducing overhead and complexity. Weave saves you time and money, and lets you focus on app development rather than your infrastructure design. ### About This Example This guide takes approximately 15 minutes to complete: you will use Weave for service discovery and load balancing between [containers that have been deployed to Amazon Elastic Cloud (EC2) instances using Amazon Container Service or ECS](http://aws.amazon.com/ecs/). This guide also introduces [Weave Scope](/oss/scope/), and [Weave Cloud](https://cloud.weave.works) which enables you to visualize and understand your container-based microservices. Two types of containerized microservices are demonstrated in this guide: HTTP Servers and "Data Producers".  Data producers generically model containers that produce a data feed of some kind. The HTTP Servers present a web interface to the data from the Data Producers. This is a very common pattern in distributed systems, but its implementation requires answers to the following questions: 1. Service discovery: How does an HTTP Server find a Data Producer to connect to? 2. Load balancing/fault tolerance: How can the HTTP Servers make uniform and efficient use of all the Data Producers available to them? Weave solves these issues using its built-in DNS server, where it securely and transparently: * Implements service discovery by adding DNS A-records for your containers based on their names. * Manages load balancing by randomizing the order of DNS responses. For more information about the weaveDNS service see [Automatic Discovery with weaveDNS](/docs/net/latest/weavedns/) ## What You Will Use * [Weave Net](/oss/net/) * [Weave Scope](/oss/scope/) * [Docker](https://docker.com) * [Amazon ECS](https://aws.amazon.com/ecs/) ## Before You Begin We will make use of the [Amazon Web Services (AWS) CLI tool](http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-welcome.html) to manage and access AWS. You need a valid [Amazon Web Services](http://aws.amazon.com) account, and also have the AWS CLI set up and configured before working through this guide. Please ensure your AWS account has the appropriate administrative privileges. Amazon provides extensive documentation on how to set up the [AWS CLI](http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-set-up.html). * [Git](http://git-scm.com/downloads) * [AWS CLI >= 1.7.35 ](http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-welcome.html) * [bash](https://en.wikipedia.org/wiki/Bash_(Unix_shell)) * An [SSH client](https://en.wikipedia.org/wiki/Comparison_of_SSH_clients) (optional) ## Getting the Code The code for this guide is available on GitHub. Clone the `weaveworks/guides` repository and then change to the `aws-ecs` directory, from which you will be working throughout most of this guide. ~~~ bash git clone https://github.com/weaveworks/guides cd guides/aws-ecs ~~~ ## AWS-CLI Configuration Make sure AWS-CLI is set to use a region where Amazon ECS is available (`us-east-1`, `us-west1`, `us-west-2`, `eu-west-1`, `ap-northeast-1` or `ap-southeast-2` at the time of writing this guide). See [Regions & Endpoints documentation at Amazon Web Service](http://docs.aws.amazon.com/general/latest/gr/rande.html) for more information. View AWS-CLI's configuration with ~~~bash aws configure list ~~~ and then modify it by running: ~~~bash aws configure ~~~ ## Obtain a Weave Cloud Service Token (Recommended) To visualize this example with the Weave Cloud service, you will need to obtain a Weave Cloud service token. To gain access, sign up at [Weave Cloud](https://cloud.weave.works). An email will be sent to you with further login instructions. Login to Weave Cloud and click the settings icon in the top right hand corner to obtain the cloud service token:  ## Automatic Setup and Configuration To configure this example, run the following command: ~~~bash ./setup.sh $SCOPE_TOKEN ~~~ where, * `$SCOPE_TOKEN` is an optional argument corresponding to your Weave Cloud service token. You should see something like this: ~~~bash Creating ECS cluster (weave-ecs-demo-cluster) .. done Creating VPC (weave-ecs-demo-vpc) .. done Creating Subnet (weave-ecs-demo-subnet) .. done Creating Internet Gateway (weave-ecs-demo) .. done Creating Security Group (weave-ecs-demo) .. done Creating Key Pair (weave-ecs-demo, file weave-ecs-demo-key.pem) .. done Creating IAM role (weave-ecs-role) .. done Creating Launch Configuration (weave-ecs-launch-configuration) .. done Creating Auto Scaling Group (weave-ecs-demo-group) with 3 instances .. done Waiting for instances to join the cluster (this may take a few minutes) .. done Registering ECS Task Definition (weave-ecs-demo-task) .. done Creating ECS Service with 3 tasks (weave-ecs-demo-service) .. done Waiting for tasks to start running .. done Setup is ready! Open your browser and go to any of these URLs: http://foo.region.compute.amazonaws.com http://bar.region.compute.amazonaws.com http://baz.region.compute.amazonaws.com ~~~ ## What Just Happened? The `setup.sh` script automatically: * Created an Amazon ECS cluster named `weave-ecs-demo-cluster` * Spawned three hosts (EC2 instances), which are now part of the cluster and are based on Weave's ECS [AMI](https://en.wikipedia.org/wiki/Amazon_Machine_Image). * Created an ECS task family definition that describes the HTTP Server and "Data Producer" containers. * Spawned three tasks, one per host, resulting in an HTTP Server and a Data Producer container running on each host. ## Testing the Setup The three URLs shown above communicate via your browser with the HTTP Server containers. Pick one of them (or all three, if you like) and open them in your browser: http://foo.region.compute.amazonaws.com http://bar.region.compute.amazonaws.com http://baz.region.compute.amazonaws.com This is what you should see:  Reload your browser to force the HTTP Server to refresh its Data Provider address list, balancing the load between the EC2 instances. ## How Do Service Discovery and Load Balancing Work? Both the HTTP Server and the Data Producer containers are very simple. They were implemented with a few lines of bash, using Netcat. Container `dataproducer`: ~~~bash while true; do IP=`hostname -i | awk '{ print $1 }'` echo "Hi, this is the data producer in $IP" | nc -q 0 -l -p 4540 done ~~~ Container `httpserver`: ~~~bash sleep 7 # Wait for data producers to start while true; do # Get a message from a data producer DATA_PRODUCER_MESSAGE=`nc dataproducer 4540` HTML="