├── .dockerignore ├── .gitignore ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── api ├── Dockerfile ├── go.mod ├── go.sum └── main.go ├── images └── Architecture.png └── tracker ├── Dockerfile ├── main.js └── package.json /.dockerignore: -------------------------------------------------------------------------------- 1 | node_modules -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | ## Code of Conduct 2 | This project has adopted the [Amazon Open Source Code of Conduct](https://aws.github.io/code-of-conduct). 3 | For more information see the [Code of Conduct FAQ](https://aws.github.io/code-of-conduct-faq) or contact 4 | opensource-codeofconduct@amazon.com with any additional questions or comments. 5 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing Guidelines 2 | 3 | Thank you for your interest in contributing to our project. Whether it's a bug report, new feature, correction, or additional 4 | documentation, we greatly value feedback and contributions from our community. 5 | 6 | Please read through this document before submitting any issues or pull requests to ensure we have all the necessary 7 | information to effectively respond to your bug report or contribution. 8 | 9 | 10 | ## Reporting Bugs/Feature Requests 11 | 12 | We welcome you to use the GitHub issue tracker to report bugs or suggest features. 13 | 14 | When filing an issue, please check existing open, or recently closed, issues to make sure somebody else hasn't already 15 | reported the issue. Please try to include as much information as you can. Details like these are incredibly useful: 16 | 17 | * A reproducible test case or series of steps 18 | * The version of our code being used 19 | * Any modifications you've made relevant to the bug 20 | * Anything unusual about your environment or deployment 21 | 22 | 23 | ## Contributing via Pull Requests 24 | Contributions via pull requests are much appreciated. Before sending us a pull request, please ensure that: 25 | 26 | 1. You are working against the latest source on the *master* branch. 27 | 2. You check existing open, and recently merged, pull requests to make sure someone else hasn't addressed the problem already. 28 | 3. You open an issue to discuss any significant work - we would hate for your time to be wasted. 29 | 30 | To send us a pull request, please: 31 | 32 | 1. Fork the repository. 33 | 2. Modify the source; please focus on the specific change you are contributing. If you also reformat all the code, it will be hard for us to focus on your change. 34 | 3. Ensure local tests pass. 35 | 4. Commit to your fork using clear commit messages. 36 | 5. Send us a pull request, answering any default questions in the pull request interface. 37 | 6. Pay attention to any automated CI failures reported in the pull request, and stay involved in the conversation. 38 | 39 | GitHub provides additional document on [forking a repository](https://help.github.com/articles/fork-a-repo/) and 40 | [creating a pull request](https://help.github.com/articles/creating-a-pull-request/). 41 | 42 | 43 | ## Finding contributions to work on 44 | Looking at the existing issues is a great way to find something to contribute on. As our projects, by default, use the default GitHub issue labels (enhancement/bug/duplicate/help wanted/invalid/question/wontfix), looking at any 'help wanted' issues is a great place to start. 45 | 46 | 47 | ## Code of Conduct 48 | This project has adopted the [Amazon Open Source Code of Conduct](https://aws.github.io/code-of-conduct). 49 | For more information see the [Code of Conduct FAQ](https://aws.github.io/code-of-conduct-faq) or contact 50 | opensource-codeofconduct@amazon.com with any additional questions or comments. 51 | 52 | 53 | ## Security issue notifications 54 | If you discover a potential security issue in this project we ask that you notify AWS/Amazon Security via our [vulnerability reporting page](http://aws.amazon.com/security/vulnerability-reporting/). Please do **not** create a public github issue. 55 | 56 | 57 | ## Licensing 58 | 59 | See the [LICENSE](LICENSE) file for our project's licensing. We will ask you to confirm the licensing of your contribution. 60 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of 4 | this software and associated documentation files (the "Software"), to deal in 5 | the Software without restriction, including without limitation the rights to 6 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 7 | the Software, and to permit persons to whom the Software is furnished to do so. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 10 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 11 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 12 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 13 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 14 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 15 | 16 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Developing an application based on multiple microservices using AWS Copilot and AWS Fargate 2 | [![Build Status](https://travis-ci.org/joemccann/dillinger.svg?branch=master)](https://travis-ci.org/joemccann/dillinger) 3 | ## Introduction 4 | On July 9, 2020, we introduced AWS Copilot, a new command line interface (CLI) to build, release, and operate production ready containerized applications on Amazon Elastic Container Service (Amazon ECS) and AWS Fargate. In this post, we walk you through how to communicate between microservices with service discovery using AWS Copilot. 5 | To illustrate, we will build a fully functional application named “emoji-race” to display the most popular emojis on Twitter. The application will be composed of a “tracker”Backend Service that subscribes to a stream of Twitter emojis and stores the counts in memory, and an “api” frontend service that will accept requests from the internet and query the “tracker” service to send the top results back to the users. 6 | 7 | ## Prerequisites 8 | To implement the instructions in this post, you will need the following accounts: 9 | 10 | * An AWS account – [how to create a new AWS account](https://aws.amazon.com/premiumsupport/knowledge-center/create-and-activate-aws-account/) 11 | * A GitHub account – [sign up for a new GitHub account](https://help.github.com/en/github/getting-started-with-github/signing-up-for-a-new-github-account) 12 | 13 | 14 | ## Architecture 15 | AWS Copilot provides a simple declarative set of commands, including examples and guided experiences built in to help customers deploy quickly. AWS Copilot automates each step in the deployment lifecycle including pushing to a registry, creating a task definition, and creating a cluster. 16 | 17 | architecture-screenshot 18 | 19 | 20 | ## Steps 21 | [![Build Status](https://travis-ci.org/joemccann/dillinger.svg?branch=master)](https://travis-ci.org/joemccann/dillinger) 22 | 23 | Here are the steps we’ll follow to implement the above architecture: 24 | 25 | 1. Create and configure the AWS Cloud9 environment 26 | * Install AWS Copilot 27 | * Configure AWS CLI 28 | 2. Building the application 29 | * Clone the GitHub repository 30 | * Create the “emoji-race” application 31 | * Create the Backend Service 32 | * Create the Load Balanced Web Service 33 | ## Contributors 34 | 35 | Irshad A Buchh, AWS 36 | 37 | Efe Karakus, AWS 38 | 39 | ## Details 40 | For additional details, please refer to the associated [blog post](https://aws.amazon.com/blogs/containers/developing-an-application-based-on-multiple-microservices-using-the-aws-copilot-and-aws-fargate/). 41 | -------------------------------------------------------------------------------- /api/Dockerfile: -------------------------------------------------------------------------------- 1 | 2 | # We specify the base image we need for our 3 | # go application 4 | FROM golang:1.14-buster as build 5 | # We create an /app directory within our 6 | # image that will hold our application source 7 | # files 8 | RUN mkdir /app 9 | # We copy everything in the root directory 10 | # into our /app directory 11 | ADD . /app 12 | # We specify that we now wish to execute 13 | # any further commands inside our /app 14 | # directory 15 | WORKDIR /app 16 | # we run go build to compile the binary 17 | # executable of our Go program 18 | ENV GOPROXY=direct 19 | RUN go build -o api . 20 | # Our start command which kicks off 21 | # our newly created binary executable 22 | 23 | # Now copy it into our base image. 24 | FROM gcr.io/distroless/base-debian10 25 | COPY --from=build /app/api / 26 | 27 | 28 | CMD ["./api"] 29 | 30 | EXPOSE 8080 -------------------------------------------------------------------------------- /api/go.mod: -------------------------------------------------------------------------------- 1 | module api 2 | 3 | go 1.14 4 | 5 | require github.com/gorilla/mux v1.7.4 // indirect 6 | -------------------------------------------------------------------------------- /api/go.sum: -------------------------------------------------------------------------------- 1 | github.com/gorilla/mux v1.7.4 h1:VuZ8uybHlWmqV03+zRzdwKL4tUnIp1MAQtp1mIFE1bc= 2 | github.com/gorilla/mux v1.7.4/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= 3 | -------------------------------------------------------------------------------- /api/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "flag" 5 | "fmt" 6 | "io/ioutil" 7 | "log" 8 | "net/http" 9 | "os" 10 | "time" 11 | 12 | "github.com/gorilla/mux" 13 | ) 14 | 15 | type server struct { 16 | router *mux.Router 17 | } 18 | 19 | func (s *server) ServeHTTP(w http.ResponseWriter, r *http.Request) { 20 | s.router.ServeHTTP(w, r) 21 | } 22 | 23 | func (s *server) routes() { 24 | s.router.HandleFunc("/", s.handleEmojis()) 25 | s.router.HandleFunc("/_healthcheck", s.handleHealthCheck()) 26 | } 27 | 28 | func (s *server) handleEmojis() http.HandlerFunc { 29 | return func(w http.ResponseWriter, r *http.Request) { 30 | endpoint := fmt.Sprintf("http://tracker.%s:3000/", os.Getenv("COPILOT_SERVICE_DISCOVERY_ENDPOINT")) 31 | resp, err := http.Get(endpoint) 32 | if err != nil { 33 | http.Error(w, err.Error(), http.StatusInternalServerError) 34 | return 35 | } 36 | defer resp.Body.Close() 37 | body, _ := ioutil.ReadAll(resp.Body) 38 | w.WriteHeader(http.StatusOK) 39 | w.Write(body) 40 | } 41 | } 42 | 43 | func (s *server) handleHealthCheck() http.HandlerFunc { 44 | return func(w http.ResponseWriter, r *http.Request) { 45 | w.WriteHeader(http.StatusOK) 46 | w.Write([]byte("healthcheck okay!")) 47 | } 48 | } 49 | 50 | func main() { 51 | addr := flag.String("addr", ":8080", "port to listen on") 52 | flag.Parse() 53 | log.Printf("port %s\n", *addr) 54 | 55 | handler := &server{ 56 | router: mux.NewRouter(), 57 | } 58 | handler.routes() 59 | 60 | s := &http.Server{ 61 | Addr: *addr, 62 | Handler: handler, 63 | WriteTimeout: time.Second * 15, 64 | ReadTimeout: time.Second * 15, 65 | IdleTimeout: time.Second * 60, 66 | } 67 | log.Fatal(s.ListenAndServe()) 68 | } 69 | -------------------------------------------------------------------------------- /images/Architecture.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aws-samples/aws-copilot-fargate/8c2369d92d5b5afd3ac2f01930d221979fa455e9/images/Architecture.png -------------------------------------------------------------------------------- /tracker/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM node:12 2 | 3 | WORKDIR /usr/src/app 4 | 5 | # Install dependencies 6 | COPY package*.json ./ 7 | RUN npm install 8 | 9 | COPY main.js . 10 | 11 | EXPOSE 3000 12 | HEALTHCHECK --interval=20s --timeout=30s --start-period=5s --retries=3 \ 13 | CMD curl -f http://localhost:3000/_healthcheck || exit 1 14 | 15 | CMD [ "node", "main.js" ] -------------------------------------------------------------------------------- /tracker/main.js: -------------------------------------------------------------------------------- 1 | const express = require('express'); 2 | const EventSource = require('eventsource'); 3 | const app = express(); 4 | const port = 3000; 5 | const maxResponseSize = 50; 6 | 7 | const emojis = new Map(); 8 | const strEmoji = (name) => String.fromCodePoint(...name.split("-").map(i => parseInt(i, 16))); 9 | 10 | const source = new EventSource("https://stream.emojitracker.com/subscribe/eps"); 11 | source.onmessage = (event) => { 12 | const updates = JSON.parse(event.data); 13 | for (const [k, v] of Object.entries(updates)) { 14 | const char = strEmoji(k); 15 | if (char in emojis) { 16 | emojis[char] += v; 17 | } else { 18 | emojis[char] = v; 19 | } 20 | } 21 | }; 22 | 23 | app.get('/_healthcheck', (req, res) => { 24 | res.send('healthcheck okay!') 25 | }); 26 | 27 | app.get('/', (req, res) => { 28 | const sortable = []; 29 | for (const [k, v] of Object.entries(emojis)) { 30 | sortable.push([k, v]); 31 | } 32 | sortable.sort((a, b) => b[1] - a[1]); 33 | 34 | const topK = sortable.slice(0, maxResponseSize); 35 | const response = { 36 | emojis: [], 37 | }; 38 | for (const [k, v] of topK) { 39 | response.emojis.push({ 40 | emoji: k, 41 | score: v, 42 | }); 43 | } 44 | res.json(response); 45 | }); 46 | 47 | app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`)) -------------------------------------------------------------------------------- /tracker/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "tracker", 3 | "version": "1.0.0", 4 | "description": "Twitter emoji tracker service", 5 | "main": "main.js", 6 | "author": { 7 | "name": "Amazon Web Services", 8 | "url": "https://aws.amazon.com", 9 | "organization": true 10 | }, 11 | "license": "Apache-2.0", 12 | "dependencies": { 13 | "eventsource": "^2.0.2", 14 | "express": "^4.17.1" 15 | } 16 | } 17 | --------------------------------------------------------------------------------