Hello Service is a JSON micro-service written in Go. The project leverages Docker, Terraform, EC2 Container Service, Elasticache, etc. This repository is intended as a template from which to base the development of an easily deployable micro-service.
Hello Service is a JSON micro-service written in Go with a simple purpose - to view how many times the server has said hello to a name. (Refer to the API section for more details).
Hello Service makes use of various components and tools in order to simplify the process of management, maintenance, and deployment of the service.
The following major AWS components are used:
Others:
Hello Service can be run locally on your machine via Docker - this greatly speeds up the process of testing new changes.
Note:
Hello Service has currently only been tested on OSX 10.11.5
The following must be set up locally:
make
. This will statically compile the service and subsequently build a docker image. Run this whenever changes are made to the source code of the Go project.redis-server
. This will initiate the local redis server. It may be desirable to run this on a separate terminal tab.docker run --publish 80:80 --env-file ./.env nytimes/hello
. This will run the service on localhost port 80. The environment variables outlined in the .env are loaded into the image. It may be necessary to confirm that the REDIS_CLUSTER_ADDRESS is accurate.docker ps
to list the docker containers currently running.docker kill CONTAINER_ID
or docker stop CONTAINER_ID
to stop the service.Note:
Infrastructure Deployment has currently only been tested on OSX 10.11.5
The following must be set up locally:
deployment
directory of the project source.terraform get
. This will install any terraform modules used.terraform plan
. Follow the prompts. This will provide an overview of the infrastructure that will be deployed. Ensure that there are no errors before proceeding.terraform apply
. Follow the prompts. This command will instantiate the infrastructure required to run Hello Service on AWS, and may take a few moments. terraform show
. This provides an overview of the deployed infrastructure. aws_elasticache_cluster
. Add the value of cache_nodes.0.address
to your clipboard.task_revision.json
, pasting the contents of task_revision_example.json
. Paste the value in your clipboard into the REDIS_CLUSTER_ADDRESS.terraform show
to obtain the repository_url
from aws_ecr_repository.hello-repository
. Update the appropriate value in the task_revision.json
file. Do not include the leading https://
. Setting up the task_revision.json
file is necessary for deploying service updates in the future.The service will live at the address of the Load Balancer. Use terraform show
, find aws_elb.hello_service_elb
, and take the dns_name
. Please note that a Release Deployment must be run for the actual service to run on the infrastructure, as the Docker image must be pushed to the repository, and the service updated with that image.
Note:
Theterraform.tfstate
andterraform.tfstate.backup
files that are generated should be kept (and likely committed to version control). These are necessary to iterate on the infrastructure of the service.
deployment/infrastructure.tf
terraform plan
. This provides an overview of the resources that will be changed, added, and destroyed.terraform apply
to update the infrastructure. If infrastructure in the infrastructure.tf
file has been deleted, these will be destroyed on AWS.terraform plan -destroy
. This provides an overview of the resources that will be destroyed.terraform destroy
to destroy the infrastructure.Note:
Release Deployment has currently only been tested on OSX 10.11.5
task_revision.json
file is set up, as described in the Deploying the Infrastructure sectionmake
. This will statically compile the service and subsequently build a docker image.docker tag nytimes/hello:latest YOUR_REPOSITORY_URL_HERE:latest
. Be sure to enter the URL of the ECR Repository.docker push YOUR_REPOSITORY_URL_HERE:latest
. If a response is received requesting to login, paste the commands provided, then attempt this step again.aws ecs register-task-definition --cli-input-json file://task_revision.json
. This will create a new task revision, which is necessary to update the service.aws ecs update-service --cluster ecs-hello --service hello-service --task-definition hello_service:REVISION_NUMBER
. ECS will run a blue-green deployment of the updates. This may take a while. Be sure to include the revision number from the output of step 5 into this command.Outputs a hello message including the provided name, and updates a Sorted Set on Redis, containing the name and a score of how many times the name was provided to this endpoint.
Hello, "{name}"
Outputs a JSON structure containing the names that have been provided to the hello endpoint coupled with a count of how many times they were provided.
{
"counts": [
{
"name": "Bob",
"count": "1"
},
{
"name": "Bill",
"count": "3"
}
]
}
Outputs a list of information about the server.
On AWS:
{
"ec2_instance_id": "i-4f066f57",
"uptime": 65473,
"cpu_utilization_percent": 0.27196008620621565,
"disk_utilization_percent": 43.82975528085612,
"total_ram_bytes_used": 95997952,
"total_ram_bytes_available": 947920896
}
Local Machine:
{
"ec2_instance_id": "local",
"uptime": 65473,
"cpu_utilization_percent": 0.27196008620621565,
"disk_utilization_percent": 43.82975528085612,
"total_ram_bytes_used": 95997952,
"total_ram_bytes_available": 947920896
}
Outputs the health of all cluster instances that are currently active on the load balancer.
On AWS:
{
"node_healths": [
{
"ec2_instance_id": "i-0236ca5d",
"uptime": 65706,
"cpu_utilization_percent": 0.33148680902462224,
"disk_utilization_percent": 43.642749476192,
"total_ram_bytes_used": 93929472,
"total_ram_bytes_available": 949989376
},
{
"ec2_instance_id": "i-4f066f57",
"uptime": 65704,
"cpu_utilization_percent": 0.16437408080283544,
"disk_utilization_percent": 43.82975528085612,
"total_ram_bytes_used": 96153600,
"total_ram_bytes_available": 947765248
}
]
}
Local Machine:
{
"node_healths": [
{
"ec2_instance_id": "local",
"uptime": 65706,
"cpu_utilization_percent": 0.33148680902462224,
"disk_utilization_percent": 43.642749476192,
"total_ram_bytes_used": 93929472,
"total_ram_bytes_available": 949989376
}
]
}
Improvements to consider for future iterations of Hello Service: