Skip to content

Commit 0edf908

Browse files
committed
Add readme
1 parent dfa1af6 commit 0edf908

File tree

4 files changed

+89
-8
lines changed

4 files changed

+89
-8
lines changed

README.md

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,61 @@
11
# Terraform-ecs-autoscale-alb
22

3-
- TODO: Write readme
4-
- TODO: Add comments
3+
Amazon [EC2 Container Service (ECS)](http://docs.aws.amazon.com/AmazonECS/latest/developerguide/) is a highly scalable, fast, container management service that makes it easy to run, stop, and manage Docker containers on a cluster of EC2 instances (called container instances).
4+
5+
The idea behind ECS is to create a cluster (a group of container instances managed by ECS), define what Docker containers we want to run (with configuration for each container) and ECS will take care of deploying those containers across the cluster, rolling out new versions and integrating with other AWS infrastructure/services.
6+
7+
A task definition is required to run a Docker container on an ECS cluster. A task definition specifies various parameters such as which Docker image(s) to use and the repository in which the image is stored, how much CPU and memory to use for the container, which environment variables should be passed to the container when it starts, which logging driver to use (awslogs, syslog etc.).
8+
9+
---
10+
11+
This repo contains Terraform configuration for an ECS cluster running three services (alpha, beta and charlie) with instance and service autoscaling configured at 80% CPU and memory (min and max autoscaling limits can be configured). The three services are sitting behind an Application Load Balancer (ALB) with path based routing set up.
12+
13+
As far as I could tell the ALB doesn't currently support URL Rewriting so I've had to manually perform this at the application level.
14+
15+
The code for the demo API is in the [/base-infra](../master/base-infra/) directory and is built and hosted on Docker Hub.
16+
17+
[![Dockerhub badge](http://dockeri.co/image/jch254/ecs-demo-api)](https://hub.docker.com/r/jch254/ecs-demo-api)
18+
19+
20+
## Base-infra components:
21+
22+
+ VPC
23+
+ Public and private subnets
24+
+ Internet Gateway
25+
+ NAT Gateways
26+
+ ALB in public subnet with Route53 record
27+
+ ECS cluster
28+
+ ECS container instances in private subnet with autoscaling configured (running Datadog agent and NGINX serving a default index.html for ALB default action on boot)
29+
+ Bastion instance in public subnet (in ASG with a fixed size of one). This only allows SSH access for a specific IP address.
30+
31+
## Service components
32+
33+
- ECS service with autoscaling configured
34+
- ALB listener and target group
35+
36+
## Deploying via Bitbucket Pipelines
37+
38+
Deployment to AWS is automated via Bitbucket Pipelines.
39+
40+
**Before running pipeline for the first time you must:**
41+
42+
1. Enable Bitbucket Pipelines for repository
43+
1. Create an S3 bucket named 'your-terraform-remote-state' for Terraform remote state via console or CLI
44+
1. Create a Bitbucket Pipelines IAM user with the required permissions
45+
1. Set up the following account-level Bitbucket Pipelines environment variables in Bitbucket UI:
46+
- AWS_ACCESS_KEY_ID = PIPELINES_USER_ACCESS_KEY_ID
47+
- AWS_SECRET_ACCESS_KEY = PIPELINES_USER_SECRET_ACCESS_KEY
48+
1. Set up the following repository-level Bitbucket Pipelines environment variables in Bitbucket UI:
49+
- TF_VAR_ssh_allowed_ip = YOUR_IP
50+
- TF_VAR_acm_arn = YOUR_ACM_CERT_ARN
51+
- TF_VAR_route53_zone_id = YOUR_R53_ZONE_ID
52+
- TF_VAR_datadog_api_key = YOUR_DATADOG_API_KEY
53+
- TF_VAR_key_pair_name = YOUR_KEY_PAIR_NAME
54+
- TF_VAR_bastion_key_pair_name = YOUR_KEY_PAIR_NAME
55+
1. Edit configuration in the .tfvars file in [/base-infra](../master/base-infra/), [/alpha-service](../master/alpha-service/), [/beta-service](../master/beta-service/) and [/charlie-service](../master/charlie-service/) directories with required values.
56+
1. Update deploy.bash file in [/base-infra](../master/base-infra/), [/alpha-service](../master/alpha-service/), [/beta-service](../master/beta-service/) and [/charlie-service] (../master/charlie-service/) directories with your remote state bucket name.
57+
1. Uncomment steps in [/bitbucket-pipelines.yml](../master/bitbucket-pipelines.yml) and commit to repository to trigger the pipeline
58+
59+
Refer to deploy.bash files for manual deployment steps.
60+
61+
- TODO: Add comments throughout infra code

bitbucket-pipelines.yml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ pipelines:
44
master:
55
- step:
66
script:
7-
- ./base-infra/deploy.bash
8-
- ./alpha-service/deploy.bash
9-
- ./beta-service/deploy.bash
10-
- ./charlie-service/deploy.bash
7+
- echo 'Refer to README.md for instructions and then uncomment steps below'
8+
#- ./base-infra/deploy.bash
9+
#- ./alpha-service/deploy.bash
10+
#- ./beta-service/deploy.bash
11+
#- ./charlie-service/deploy.bash

demo-api/Dockerfile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
FROM node:7
1+
FROM mhart/alpine-node:latest
22

3-
RUN npm install -g yarn@0.16.1
3+
RUN apk add --update python ca-certificates
4+
5+
RUN npm install -g yarn@0.17.3
46

57
WORKDIR /app
68

demo-api/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Demo-api
2+
3+
Demo API powered by Node.js/Express.
4+
5+
## Running development server (with live-reloading)
6+
7+
1. Run the following commands in the app's root directory then submit requests to http://localhost:3000
8+
9+
```
10+
yarn install
11+
yarn run dev
12+
```
13+
14+
## Running production version in Docker container
15+
1. Run the following commands in the app's root directory then submit requests to http://localhost:YOUR_CONTAINER_PORT. YOUR_CONTAINER_ID will be returned by the `docker run` command and YOUR_CONTAINER_PORT will be returned by the `docker port` command.
16+
17+
```
18+
docker build -t demo-api .
19+
docker run -d -P demo-api
20+
docker port YOUR_CONTAINER_ID
21+
```

0 commit comments

Comments
 (0)