wacul / aws-ecs

Wercker step and github action for AWS ECS Deploy
6 stars 2 forks source link
ecs github-actions wercker-step

Wercker step AND github actions for AWS ECS

=======================

The step is written in Python 3.5 and use Pip and Boto3 module.

Example

options

test templates

service and task-definition settings. Details are described later.

or

  steps:
    - wacul/aws-ecs:
      key: $AWS_ACCESS_KEY_ID
      secret: $AWS_SECRET_ACCESS_KEY
      region: $AWS_DEFAULT_REGION
      services-yaml: infra/services.yml
      environment-yaml: infra/conf/dev.yml
      deploy-service-group: group
      template-group: repo
# or
  steps:
    - wacul/aws-ecs:
      key: $AWS_ACCESS_KEY_ID
      secret: $AWS_SECRET_ACCESS_KEY
      region: $AWS_DEFAULT_REGION
      task-definition-template-dir: infra/template/
      task-definition-config-json: infra/conf/dev.json
      deploy-service-group: group
      template-group: repo

Yaml Template examples

environment-yaml

environment parameter is required. then set docker environment ENVIRONMENT value.


---
environment: dev
cpu: 64
memoryReservation: 64
conf: dev.yaml

services:
  web:
    desiredCount: 4
    vars:
      cpu: 96
      memoryReservation: 96

services-yaml

service parameters
scheduled task parameters
taskDefinitionTemplates

Template can use jinja2 template engine. service or scheduled task name is set to {{item}}.


---

aliases:
  - &cluster_applications app
  - &cluster_batch batch

services:
  web:
    cluster: *cluster_applications
    serviceGroup: web
    templateGroup: repo
    desiredCount: 2
    minimumHealthyPercent: 50
    maximumPercent: 100
    registrator: true
    distinctInstance: true
    taskDefinitionTemplate: default
    vars:
      startupScript: ./script/startup_web.sh
      portMappings:
        - hostPort: 0
          containerPort: 3000
          protocol: tcp
scheduledTasks:
  batch:
    cluster: *cluster_batch
    serviceGroup: batch
    templateGroup: repo
    taskCount: 1
    placementStrategy:
      - field: memory
        type": binpack
    cloudwatchEvent:
      scheduleExpression: rate(5 minutes)
      targetLambdaArn: arn:aws:lambda:us-east-1:111111111111:function:lambda_name
    taskDefinitionTemplate: default
    vars:
      cpu: 64
      memory: 64
      startupScript: ./script/run.sh

taskDefinitionTemplates:
  default: |
    {
      "family": "{{environment}}-{{item}}",
      "containerDefinitions": [
        {
          "name": "{{environment}}-{{item}}",
          "cpu": {{cpu}},
          "memoryReservation": {{memoryReservation}},
          "image": "mydomain/myimage:{{environment}}{% if environment == 'production' %}-{{serviceGroup}}{% endif %}",
          "command": [
            "{{startupScript}}",
            "{{conf}}"
          ],
          "portMappings": {{portMappings|default([])|tojson}},
          "logConfiguration": {
            "logDriver": "syslog",
            "options": {
              "tag": "docker/{{environment}}/{{item}}/{% raw %}{{.ID}}{% endraw %}"
            }
          },
          "volumesFrom": [],
          "mountPoints": [],
          "essential": true
        }
      ]
    }

Json Template examples

environment parameter is required. only same task-definition's environment ENVIRONMENT service is deployed.

{
  "environment": "development",
  "cpu": 16,
  "memory": 64
}

infra/template/example.j2

task-definition parameters

http://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_definition_parameters.html

can use jinja2 template. this script use environment variables below:

  {
    "family": "{{environment}}-web",
    "containerDefinitions":  [
      {
        "environment": [
          {
            "name": "CLUSTER_NAME",
            "value": "cluster"
          },
          {
            "name": "TEMPLATE_GROUP",
            "value": "web-repo"
          },
          {
            "name": "ENVIRONMENT",
            "value": "{{environment}}"
          },
          {
            "name": "SERVICE_GROUP",
            "value": "web"
          },
          {
            "name": "DESIRED_COUNT",
            "value": "2"
          },
          {
            "name": "MINIMUM_HEALTHY_PERCENT",
            "value": "50"
          },
          {
            "name": "MAXIMUM_PERCENT",
            "value": "100"
          }
        ],
        "name": "{{environment}}-web",
        "image": "nginx",
        "cpu": {{cpu}},
        "portMappings": [
          {
            "hostPort": 0,
            "containerPort": 80,
            "protocol": "tcp"
          }
        ],
        "memoryReservation": {{memory}},
        "essential": true
      }
    ]
  }
]

Github Actions

    - name: aws ecs deploy
      uses: wacul/aws-ecs@master
      with:
        key: ${{ secrets.AWS_ACCESS_KEY_ID }}
        secret: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
        region: us-east-1
        services-yaml: services.yaml
        environment-yaml: conf/env.yaml
        deploy-service-group: group
        template-group: template

Docker

docker run -it --rm -e "AWS_PROFILE=profile" -v $HOME/.aws/:/root/.aws/ -v $(pwd)/infra/:/infra ghcr.io/wacul/aws-ecs test-templates --environment-yaml-dir /infra/conf/ --services-yaml /infra/services.yml
docker run -it --rm -e "AWS_PROFILE=profile" -v $HOME/.aws/:/root/.aws/ -v $(pwd)/infra/:/infra ghcr.io/wacul/aws-ecs service --environment-yaml /infra/conf/dev.yml --services-yaml /infra/services.yml --template-group web-repo --dry-run
docker run -it --rm -e "AWS_PROFILE=profile" -v $HOME/.aws/:/root/.aws/ -v $(pwd)/infra/:/infra ghcr.io/wacul/aws-ecs delete --environment dev