{"id":18914093,"url":"https://github.com/aws-devops-projects/container-demo","last_synced_at":"2025-04-15T08:31:05.500Z","repository":{"id":46591494,"uuid":"287512221","full_name":"AWS-Devops-Projects/Container-Demo","owner":"AWS-Devops-Projects","description":"How can I manage microservices using ecs-cli?","archived":false,"fork":false,"pushed_at":"2020-09-24T12:28:12.000Z","size":294,"stargazers_count":9,"open_issues_count":3,"forks_count":7,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-28T18:50:50.236Z","etag":null,"topics":["ecs","ecs-cli","ecs-service","fargate"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/AWS-Devops-Projects.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-08-14T10:58:23.000Z","updated_at":"2022-07-20T06:14:19.000Z","dependencies_parsed_at":"2022-09-13T15:45:08.955Z","dependency_job_id":null,"html_url":"https://github.com/AWS-Devops-Projects/Container-Demo","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AWS-Devops-Projects%2FContainer-Demo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AWS-Devops-Projects%2FContainer-Demo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AWS-Devops-Projects%2FContainer-Demo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AWS-Devops-Projects%2FContainer-Demo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AWS-Devops-Projects","download_url":"https://codeload.github.com/AWS-Devops-Projects/Container-Demo/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249035403,"owners_count":21202076,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ecs","ecs-cli","ecs-service","fargate"],"created_at":"2024-11-08T10:10:00.610Z","updated_at":"2025-04-15T08:31:05.213Z","avatar_url":"https://github.com/AWS-Devops-Projects.png","language":"Python","readme":"## Bring up a cloud9 IDE and run these prerequisite commands:\n```\n\n# Choose your region, and store it in this environment variable\nexport AWS_DEFAULT_REGION=ap-southeast-1 \necho \"export AWS_DEFAULT_REGION=$AWS_DEFAULT_REGION \u003e\u003e ~/.bashrc\"\n\n# Install software\nsudo yum -y install jq gettext\nsudo curl -so /usr/local/bin/ecs-cli https://s3.amazonaws.com/amazon-ecs-cli/ecs-cli-linux-amd64-latest\nsudo chmod +x /usr/local/bin/ecs-cli\n```\nThis installs some handy text parsing utilities, and the latest ecs-cli.\n\n## Clone this demo repository:\n```\ncd ~/environment\ngit clone https://github.com/AWS-Devops-Projects/Container-Demo.git\n```\n\n## Clone our application microservice repositories:\n```\ncd ~/environment\ngit clone https://github.com/brentley/ecsdemo-frontend.git\ngit clone https://github.com/brentley/ecsdemo-nodejs.git\ngit clone https://github.com/brentley/ecsdemo-crystal.git\n```\n\n## Build a VPC, ECS Cluster, and ALB:\n![infrastructure](images/private-subnet-public-lb.png)\n```\ncd ~/environment/container-demo\n\naws cloudformation deploy --stack-name container-demo --template-file cluster-fargate-private-vpc.yml --capabilities CAPABILITY_IAM\naws cloudformation deploy --stack-name container-demo-alb --template-file alb-external.yml\n```\nAt a high level, we are building what you see in the diagram. We will have 3 \navailability zones, each with a public and private subnet. The public subnets\nwill hold service endpoints, and the private subnets will be where our workloads run.\nWhere the image shows an instance, we will have containers on AWS Fargate.\n\n## Set environment variables from our build\n```\n\nexport clustername=$(aws cloudformation describe-stacks --stack-name container-demo --query 'Stacks[0].Outputs[?OutputKey==`ClusterName`].OutputValue' --output text)\nexport target_group_arn=$(aws cloudformation describe-stack-resources --stack-name container-demo-alb | jq -r '.[][] | select(.ResourceType==\"AWS::ElasticLoadBalancingV2::TargetGroup\").PhysicalResourceId')\nexport vpc=$(aws cloudformation describe-stacks --stack-name container-demo --query 'Stacks[0].Outputs[?OutputKey==`VpcId`].OutputValue' --output text)\nexport ecsTaskExecutionRole=$(aws cloudformation describe-stacks --stack-name container-demo --query 'Stacks[0].Outputs[?OutputKey==`ECSTaskExecutionRole`].OutputValue' --output text)\nexport subnet_1=$(aws cloudformation describe-stacks --stack-name container-demo --query 'Stacks[0].Outputs[?OutputKey==`PrivateSubnetOne`].OutputValue' --output text)\nexport subnet_2=$(aws cloudformation describe-stacks --stack-name container-demo --query 'Stacks[0].Outputs[?OutputKey==`PrivateSubnetTwo`].OutputValue' --output text)\nexport subnet_3=$(aws cloudformation describe-stacks --stack-name container-demo --query 'Stacks[0].Outputs[?OutputKey==`PrivateSubnetThree`].OutputValue' --output text)\nexport security_group=$(aws cloudformation describe-stacks --stack-name container-demo --query 'Stacks[0].Outputs[?OutputKey==`ContainerSecurityGroup`].OutputValue' --output text)\n\ncd ~/environment\n```\nThis creates our infrastructure, and sets several environment variables we will use to\nautomate deploys.\n\n## Configure `ecs-cli` to talk to your cluster:\n```\necs-cli configure --region $AWS_DEFAULT_REGION --cluster $clustername --default-launch-type FARGATE --config-name container-demo\n```\nWe set a default region so we can reference the region when we run our commands.\n\n\n## Authorize traffic:\n```\naws ec2 authorize-security-group-ingress --group-id \"$security_group\" --protocol tcp --port 3000 --cidr 0.0.0.0/0\n```\nWe know that our containers talk on port 3000, so authorize that traffic on our security group:\n\n## Deploy our frontend application:\n```\ncd ~/environment/ecsdemo-frontend\nenvsubst \u003c ecs-params.yml.template \u003eecs-params.yml\n\necs-cli compose --project-name ecsdemo-frontend service up \\\n    --create-log-groups \\\n    --target-group-arn $target_group_arn \\\n    --private-dns-namespace service \\\n    --enable-service-discovery \\\n    --container-name ecsdemo-frontend \\\n    --container-port 3000 \\\n    --cluster-config container-demo \\\n    --vpc $vpc\n    \n```\nHere, we change directories into our frontend application code directory.\nThe `envsubst` command templates our `ecs-params.yml` file with our current values.\nWe then launch our frontend service on our ECS cluster (with a default launchtype \nof Fargate)\n\nNote: ecs-cli will take care of building our private dns namespace for service discovery,\nand log group in cloudwatch logs.\n\n## View running container:\n```\necs-cli compose --project-name ecsdemo-frontend service ps \\\n    --cluster-config container-demo\n```\nWe should have one task registered.\n\n## Check reachability (open url in your browser):\n```\nalb_url=$(aws cloudformation describe-stacks --stack-name container-demo-alb --query 'Stacks[0].Outputs[?OutputKey==`ExternalUrl`].OutputValue' --output text)\necho \"Open $alb_url in your browser\"\n```\nThis command looks up the URL for our ingress ALB, and outputs it. You should \nbe able to click to open, or copy-paste into your browser.\n\n## View logs:\n```\n#substitute your task id from the ps command \necs-cli logs --task-id a06a6642-12c5-4006-b1d1-033994580605 \\\n    --follow --cluster-config container-demo\n```\nTo view logs, find the task id from the earlier `ps` command, and use it in this\ncommand. You can follow a task's logs also.\n\n## Scale the tasks:\n```\necs-cli compose --project-name ecsdemo-frontend service scale 3 \\\n    --cluster-config container-demo\necs-cli compose --project-name ecsdemo-frontend service ps \\\n    --cluster-config container-demo\n```\nWe can see that our containers have now been evenly distributed across all 3 of our\navailability zones.\n\n## Bring up NodeJS backend api:\n```\ncd ~/environment/ecsdemo-nodejs\nenvsubst \u003cecs-params.yml.template \u003eecs-params.yml\necs-cli compose --project-name ecsdemo-nodejs service up \\\n    --create-log-groups \\\n    --private-dns-namespace service \\\n    --enable-service-discovery \\\n    --cluster-config container-demo \\\n    --vpc $vpc\n\n```\nJust like earlier, we are now bringing up one of our backend API services.\nThis service is not registered with any ALB, and instead is only reachable by \nprivate IP in the VPC, so we will use service discovery to talk to it.\n\n## Scale the tasks:\n```\necs-cli compose --project-name ecsdemo-nodejs service scale 3 \\\n    --cluster-config container-demo\n    \n```\nWe can see that our containers have now been evenly distributed across all 3 of our\navailability zones.\n\n## Bring up Crystal backend api:\n```\ncd ~/environment/ecsdemo-crystal\nenvsubst \u003cecs-params.yml.template \u003eecs-params.yml\necs-cli compose --project-name ecsdemo-crystal service up \\\n    --create-log-groups \\\n    --private-dns-namespace service \\\n    --enable-service-discovery \\\n    --cluster-config container-demo \\\n    --vpc $vpc\n\n```\nJust like earlier, we are now bringing up one of our backend API services.\nThis service is not registered with any ALB, and instead is only reachable by \nprivate IP in the VPC, so we will use service discovery to talk to it.\n\n## Scale the tasks:\n```\necs-cli compose --project-name ecsdemo-crystal service scale 3 \\\n    --cluster-config container-demo\n    \n```\nWe can see that our containers have now been evenly distributed across all 3 of our\navailability zones.\n\n## Conclusion:\nYou should now have 3 services, each running 3 tasks, spread across 3 availability zones.\nAdditionally you should have zero instances to manage. :)\n\n## Cleanup:\n```\ncd ~/environment/ecsdemo-frontend\necs-cli compose --project-name ecsdemo-frontend service down --cluster-config container-demo\ncd ~/environment/ecsdemo-nodejs\necs-cli compose --project-name ecsdemo-nodejs service down --cluster-config container-demo\ncd ~/environment/ecsdemo-crystal\necs-cli compose --project-name ecsdemo-crystal service down --cluster-config container-demo\n\necs-cli down --force --cluster-config container-demo\naws cloudformation delete-stack --stack-name container-demo-alb\naws cloudformation wait stack-delete-complete --stack-name container-demo-alb\naws cloudformation delete-stack --stack-name container-demo\naws cloudformation delete-stack --stack-name amazon-ecs-cli-setup-private-dns-namespace-$clustername-ecsdemo-frontend\n```\n\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faws-devops-projects%2Fcontainer-demo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faws-devops-projects%2Fcontainer-demo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faws-devops-projects%2Fcontainer-demo/lists"}