Skip to main content

AWS ECS Deployment Tool With Terraform

Project description

Introduce

ECS deploy using docker compose and terraform.

You need to manage just yml file for docker compose and:

ecsdep cluster create
ecsdep service up

That's all.

Currently, ecsdep supports EC2 ECS, not Fargate.

Running Docker For Deployment.

Locally

Docker contains terrform, awscli and ecsdep.

docker run -d --privileged \
    --name docker \
    -v path/to/myproject:/app \
    hansroh/dep:dind
docekr exec -it docker bash

.gitlab-ci.yml for Gitlab CI/CD

image: hansroh/dep:latest
services:
  - name: docker:dind
    alias: dind-service

Prequisition

  • AWS credebtial for ECS deployment
  • AWS certification
  • AWS secret arn for private docker registry login
  • AWS s3 bucket for terraform state data at your region

Make Docker Compose File For Deploying.

Create /app/de[/compose.ecs.yml.

version: '3.9'

services:
  skitai-app:
    image: registry.gitlab.com/skitai/ecsdep
    x-ecs-pull_credentials: arn:aws:secretsmanager:ap-northeast-2:000000000:secret:gitlab/registry/mysecret-PrENMF
    build:
      context: ..
      dockerfile: dep/Dockerfile
      target: image-${SERVICE_STAGE}
    container_name: skitai-app
    environment:
      SERVICE_STAGE: ${SERVICE_STAGE}
      REGISTRY_USER: hansroh
    logging:
      x-ecs-driver: awslogs
    x-ecs-essential: true
    deploy:
      resources:
        reservations:
          memory: "160M"
          cpus: "1024"
          x-ecs-gpus: 0
        limits:
          memory: "512M"
    ports:
      - 5000
    healthcheck:
      test:
        - "CMD-SHELL"
        - "wget -O/dev/null -q http://localhost:5000 || exit 1"
      interval: 30s
      retries: 3

  skitai-nginx:
    image: registry.gitlab.com/skitai/ecsdep/nginx
    x-ecs-pull_credentials: arn:aws:secretsmanager:ap-northeast-2:000000000:secret:gitlab/registry/mysecret-PrENMF
    build:
      context: ..
      dockerfile: dep/Dockerfile.nginx
    container_name: skitai-nginx
    build:
      context: ..
      dockerfile: dep/Dockerfile.nginx
    logging:
      x-ecs-driver: awslogs
    deploy:
    depends_on:
      - skitai-app
    x-ecs-wait-conditions:
      - HEALTHY
    ports:
      - 80:80
    deploy:
      resources:
        reservations:
          memory: "16M"

networks:
  ecsdep:

secrets:
  REGISTRY_USER:
    name: "arn:aws:secretsmanager:ap-northeast-2:000000000:secret:gitlab/registry/mysecret-PrENMF:username::"
    external: true


# ECS config --------------------------------------------
x-ecs-service-config:
  name: ecsdep
  stages:
    default:
      env-service-stage: "qa"
      hosts: ["qa.myservice.com"]
      listener-priority: 100

    production:
      env-service-stage: "production"
      hosts: ["myservice.com"]
      listener-priority: 101

  loadbalancing-pathes:
    - /*

  autoscaling:
    desired_count: 1
    min: 1
    max: 4
    cpu: 75
    memory: 80

  target-group:
    port: 80
    protocol: http
    health-check:
      path: "/"
      matcher: "200,301,302,404"

x-terraform:
  provider: aws
  region: ap-northeast-2
  state-backend:
    bucket: "states-data"
    key-prefix: "terraform/ecs-cluster"
    region: "ap-northeast-2"

x-ecs-cluster:
  name: mycluster
  public-key_file: "~/.ssh/id_rsa.pub"
  instance-type: t3.medium
  ami: amzn2-ami-ecs-hvm-*-x86_64-*
  autoscaling:
    min: 1
    max: 20
    desired: 1
    cpu: 80
    memory: 80
  loadbalancer:
    cert-name: myservice.com
  availability-zones: 2
  s3-cors_hosts:
    - http://localhost:5000
    - https://myservice.com
    - https://qa.myservice.com

Testing Docker Containers

cd dep
docker-compose -f compose.ecs.yml build
docker-compose -f compose.ecs.yml up -d
docker-compose -f compose.ecs.yml down
docker-compose -f compose.ecs.yml push

Deployment

Creating ECS Cluster

ecsdep -f compose.ecs.yml cluster plan
# ecsdep find compose.ecs.yml default,
ecsdep cluster plan
# if no error,
ecsdep cluster create

As a results, AWS resources will be created.

  • VPC
  • Application Load Balancer
  • ECS Cluster
  • Launch Configureation
  • Security Group
  • Auto Scaling Group For Cluster
  • Public Accessable S3 Bucket

Deploying Service

export CI_COMMIT_SHA=latest
export SERVICE_STAGE=qa

ecsdep service plan
ecsdep service up

As a results, AWS resources will be created.

  • Task Definition
  • Update Service and Run

Removing Service

ecsdep service down

Destroying ECS Cluster

ecsdep cluster destroy

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

ecsdep-0.1.0a2-py3-none-any.whl (24.2 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page