Skip to content

Instantly share code, notes, and snippets.

@tpai
Last active February 24, 2021 14:38
Show Gist options
  • Save tpai/b4e1a24a6e659313c08e63b3a8ac22bc to your computer and use it in GitHub Desktop.
Save tpai/b4e1a24a6e659313c08e63b3a8ac22bc to your computer and use it in GitHub Desktop.
cheatsheet for aws ecs

Auto Scaling Launch Config

  • Amazon ECS Optimized AMI: ami-b4ae1dd7
  • Instance Type: t2.small

Config Details

  • Name: [CONFIG_NAME]
  • IAM role: fever_ecs
  • User data: As text
#!/bin/bash
echo ECS_CLUSTER=[CLUSTER_NAME] >> /etc/ecs/ecs.config

IP Address Type: Only assign a public IP address to instances launched in the default VPC and subnet. (default)

Security Group

Create/Select an security group which contained inbound port 32768-61000 rule.

Key Pair

Choose a key pair for SSH login.

Auto Scaling Group

Details

  • Group Name: [GROUP_NAME]
  • Group Size: > 1
  • Network: VPC
  • Subnet: ap-southeast-1a,b
  • Load Balancing: [check] Receive traffic from load balancer
  • Target Groups: [TARGET_GROUP]
  • Health Check Type: EC2

Scaling Policies

Based on your requirement.

Notifications

Based on your requirement.

Cheatsheet For ECS

Launch your ECS with custom domain and support dynamic port mapping.

Steps

  1. Create ELB & Target Group (point to container port)

  2. Create Security Group(inbound https?, ssh and tcp 32768 - 61000)

  3. Add Hosted Zone then get NS record for custom domain.

ns-1590.awsdns-06.co.uk.
ns-984.awsdns-59.net.
ns-21.awsdns-02.com.
ns-1172.awsdns-18.org.
  1. Add A record and switch to alias, and value to DNS Name of ELB

  2. Create IAM Role attach with AmazonEC2ContainerServiceforEC2Role policy

  3. Create Task Definition with specific docker image and awslog config(make sure to create Log Group of Cloud Watch for it)

  4. Create Empty Cluster

  5. Launch EC2 with ecsRole, security group above and advance details below

#!/bin/bash
echo ECS_CLUSTER=[CLUSTER_NAME] >> /etc/ecs/ecs.config
  1. Create Service which bind with Target Group in Cluster

  2. Done!

ECS

  • Cluster Services: Manage task version of live containers and its number of tasks.
  • Task Definitions: Define docker image, settings and environment variables.
  • Repositories: Docker images which upload by developers.

Outdated ECS Agent

sudo yum update -y ecs-init

Register Existing EC2 To Cluster

sudo rm /var/lib/ecs/data/*
sudo service docker restart
sudo start ecs
curl http://localhost:51678/v1/metadata

Task Definitions

  • Container Definitions
    • Port Mapping
    • Environment Variables
    • Log Configuration: CloudWatch awslogs
awslogs-group=RD-TEST // log group must create first
awslogs-region=ap-southeast-1
awslogs-stream-prefix=[repo]-dev

Repositories

Upload_To_ECS.sh

#!bin/sh

TAG=$(git rev-parse --abbrev-ref HEAD)
HASH=$(git rev-parse HEAD | cut -c1-6)

# aws login
export $(cat .env | xargs)
eval $(aws ecr get-login --region $REGION)

# build
docker build -t $REPO_NAME .
docker tag $REPO_NAME $REPO_URI:$TAG-$HASH

# push
docker push $REPO_URI:$TAG-$HASH

ELB

EC2 - Load Balancing

Create Load Balancer > Application Load Balancer

Name:
Scheme: internet-facing
IP address type: ipv4
Listeners: HTTP 80 public side
Availability Zones: VPC
Security Settings:
Security Groups: Firewall Rules (be sure to allow inbound port 32768 - 61000 for dynamic port mapping)

Route 53

Create Hosted Zone > Add A Record

  1. Add domain
  2. Alias to DNS name of load balancer

Useful Links

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment