AWS 채팅앱 인프라 배포 (2/2)

🖥 Node.js Chatting App

라라벨 앱과는 달리 노드JS 앱은 Docker 컨테이너로 ECS/Fargate 으로 배포되며, Redis, DynamoDB 등의 인프라를 사용한다. 필요한 경우 읽지 않은 채팅메시지 푸시알림은 SQS 를 통해 라라벨앱으로 전달하여, 워커 환경에서 처리하도록 되어 있다.

📙 DynamoDB

chat-messages, chat-rooms, chat-users 테이블을 생성시 hash key 를 지정하고 default 옵션으로 생성한다. 생성후에는 Provisioned capacity 의 read units 과 writes units 을 10, 5 로 조정했다. (DynamoDB 는 특정 VPC 에 종속되지 않고 접근이 가능하다.)

📙 SQS

chat-queue 큐를 생성한다. (SQS 는 특정 VPC 에 종속되지 않고 접근이 가능하다.)

📙 VPC

default vpc 를 사용할 수 도 있지만, chat-vpc 라는 별도의 vpc 를 생성해야만 환경삭제시 편하다.

  • Name tag: chat-vpc
  • IPv4 CIDR block: 10.0.0.0/16
  • IPv6 CIDR block: no

📙 Subnets

ECS 클러스터 생성시 VPC 를 생성하는 옵션을 선택하는 경우, 자동으로 생성된다.
Name tag: chat-subnet-az1, chat-subnet-az2
VPC: chat-vpc
AZ: AZ1, AZ2, …
IPv4 CIDR block: 10.0.0.0/24, 10.0.1.0/24, …

📙 Redis

Redis 는 Subnet Group 을 지정해야하는데, 이것이 VPC 와 연결되어 있다. 다시 말해 채팅서버가 바라보는 Redis 를 라라벨서버가 같이 바라봐야 한다면 같은 VPC 에 생성해야하지만, 그렇지 않다면, 다른 컨디션을 고려할 필요없다. 아래의 옵션으로 생성했다.

Engine: Redis
Location: Amazon Cloud
Redis settings:

  • chat-redis
  • 6.x
  • 6379
  • default.redis6.x
  • cache.t2.small (1.55 GB)
  • # of replicas : 0
  • subnet group : create new
    • name : chat-subnet-group
    • vpc : chat-vpc
    • subnets : 위 단계에서 생성한 subnets
    • security groups :
    • chat-default-sg (자동으로 생성) : all
    • chat-redis-sg (수동으로 추가): 6379

📙 Internet Gateway

  • ECS 클러스터 생성시 VPC 를 생성하는 옵션을 선택하는 경우, 자동으로 생성됨.
  • name: chat-igw
  • VPC: chat-vpc

📙 Route Table

  • ECS 클러스터 생성시 VPC 를 생성하는 옵션을 선택하는 경우, 자동으로 생성됨.
  • VPC 에 자동으로 생성된 라우트를 수정 (Edit routes)
    • Routes
      • 탭 = 0.0.0.0/0
      • target = chat-igw
    • Subnet Associations
      • chat-subnet-az1
      • chat-subnet-az2

📙 Security Groups

  • chat-task-sg: allows 3210
  • chat-lb-sg: allows HTTP, HTTPS
  • chat-dynamodb-sg: allows DynamoDB
  • chat-redis-sg: allows Redis

📙 System Manager

Parameter Store 에 .env.production 에 지정한 환경변수들을 빠짐없이 입력한다.

📙 Load Balancer

Application Load Balancer

  • chat-load-balancer
  • internet-facing
  • ipv4
  • Listeners
    • HTTP/HTTPS (80/443 포트)
  • AZ
  • 가용 AZ 모두 선택

Security Settings

chat.lousydomain.com 에 대한 Certificate ACM 에서 설정후

Security Groups

  • chat-default
  • chat-lb-sg

Routing

  • Target group
    • name: lousy-chat-tg
    • type: IP
    • protocol: HTTP
    • port: 80
  • health check
    • HTTP
    • /health (depending on your implementation)

Register targets

  • intact

📙 ECR

  • Repository name : chat-server

📙 ECS

Create Cluster 를 선택하고 cluster template 에서 Networking only (Powered by AWS Fargate) 을 선택한다.

  • 클러스터명 : lousy-chat-cluster
  • CloudWatch Container Insights : Enable

📙 Task Definition

  • Task definition name: lousy-chat-task
  • Requires Compatibilities: Fargate
  • Task execution role : ecsTaskExecutionRole
  • Task size : 0.5G, 0.25 vCPU

Container Definitions

  • Container name: chat-server
  • image: 000000000000.dkr.ecr.ap-northeast-2.amazonaws.com/chat-server:latest
  • private repository authentication: intact (false)
  • Memory limits: intact (soft limit 128)
  • Port mappings: 3210 tcp
  • Health check: CMD-SHELL, curl -f http://localhost:3210/health || exit 1
  • Environment: intact (default values)
  • Environment variables: ASM Parameter Store 에 지정한 내용을 아래 스크린샷처럼 입력한다. 키값, valueFrom, arn (형식: arn:aws:ssm:ap-northeast-2:000000000000:parameter/chat-REGION)

📙 Service

Configure service

  • Launch type: Fargate
  • Task Definition
    • Family : lousy-chat-task
    • Revision : 1 (latest)
  • Platform version: LATEST
  • Cluster: lousy-chat-cluster
  • Service name: lousy-chat-service
  • Service type: REPLICA
  • Number of tasks: 1
  • Task Group: chat-task-group
  • Minimum healthy percent: 100
  • Maximum percent: 200

Deployments

  • Rolling update (set them as default)

Configure network

  • Cluster VPC: lousy-chat-vpc
  • Subnets: chat-subnets (ap-northeast-2a, ap-northeast-2b)
  • Security groups: chat-lb-sg (80/443), chat-task-sg (3210)
  • Auto-assign public IP: ENABLED
  • Health check grace period: 60
  • Load balancing
    • Load balancer type: Application Load Balancer
    • Load balancer name: lousy-chat-load-balancer
  • Container to load balance
    • Container name : port : lousy-container:3210
  • Production listener port: 80
    • Production listener protocol: HTTP
    • Target group name: lousy-chat-tg
    • 생성이후 https 로 리다이렉팅 룰 추가 예정
  • Auto Scaling
    • Minimum number of tasks: 1
    • Desired number of tasks: 1
    • Maximum number of tasks: 4
    • IAM role for Service Auto Scaling: ecsAutoscaleRole
  • Automatic task scaling policies
    • Scaling policy type: Target tracking
    • Policy name: scaling-cpu-policy
    • ECS service metric: ECSServiceAverageCPUUtilization
    • Target value: 75
    • rest intact

📙 Associate ACM SSL certificate with Application Load balancer

Refer) https://aws.amazon.com/premiumsupport/knowledge-center/associate-acm-certificate-alb-nlb

  • choose EC2 > Load balancer
  • add listener
  • default action
  • forward to chat-tg
  • group-level stickiness
    • enable up to 30 seconds
  • select certificate from ACM
  • update HTTP 80 traffics redirecting to HTTPS://#{host}:443/#{path}?#{query}

Route 53

  • Add a A record to lousy-chat-load-balancer

Leave a Reply