项目作者: baikonur-oss

项目描述 :
ECS Dynamic Environment Manager (eden) CLI
高级语言: Python
项目地址: git://github.com/baikonur-oss/aws-eden-cli.git
创建时间: 2019-08-22T07:31:55Z
项目社区:https://github.com/baikonur-oss/aws-eden-cli

开源协议:MIT License

下载


aws-eden-cli: ECS Dynamic Environment Manager

Clone Amazon ECS environments easily.
Provide eden with a Amazon ECS service and eden will clone it.


PyPi version
t
Language grade: Python

eden is provided in CLI and Terraform module (Lambda with HTTP API) flavors.
You can use HTTP API from CI of your choice on Pull Request open/close,
new commit pushes to fully automate environment creation.
For API flavor, see eden API at GitHub.

Works only with Python 3.6+.

Developing with eden

simple-figure

Requirements:

  1. Config JSON file in a S3 bucket with structure described below
  2. A reference ECS Service with Target Group Attached
  3. An ALB with HTTPS Listener
    • Will be reused by all environments with Host Header Listener Rules
    • Separate from what reference service uses
    • Listener must have wildcard certificate for target dynamic zone
  4. Simple ALB usage
    • No multiple path rules etc.
    • One ALB per one ECS Service

What it does

Resources created/deleted

eden creates

  1. ECS Task Definition
    • Cloned from reference service
  2. ALB (elbv2) Target Group
    • Settings cloned from Target Group attached to reference service
  3. ECS Service
    • Created in the same cluster as reference service
  4. ALB Listener Rule
    • Host Header rule
  5. Route 53 CNAME record
    • Points at common ALB
  6. An entry is added to config JSON file

eden deletes resources in reverse order.

Config JSON file

Config file is used to:

  1. Check what environments exist and where their endpoints are
  2. Tell client apps what is available

Config file format:

  1. {
  2. "environments": [
  3. {
  4. "env": "dev",
  5. "name": "dev-dynamic-test",
  6. "api_endpoint": "api-test.dev.example.com"
  7. }
  8. ]
  9. }

Example above presumes config_update_key = api_endpoint. You can use multiple Lambdas/invoke eden-cli multiple times with different update keys to have multiple endpoints within single environment.

For example, you may want to have API, administration tool and a frontend service created as a single environment. Your environment file could look like this:

  1. {
  2. "environments": [
  3. {
  4. "env": "dev",
  5. "name": "dev-dynamic-test",
  6. "api_endpoint": "api-test.dev.example.com",
  7. "admin_endpoint": "admin-test.dev.example.com",
  8. "frontend_endpoint": "test.dev.example.com"
  9. }
  10. ]
  11. }

Usage (CLI interface)

Installation

  1. $ pip3 install aws-eden-cli
  2. $ eden -h
  3. usage: eden [-h] {create,delete,ls,config} ...
  4. ECS Dynamic Environment Manager. Clone Amazon ECS environments easily.
  5. positional arguments:
  6. {create,delete,ls,config}
  7. create Create environment or deploy to existent
  8. delete Delete environment
  9. ls List existing environments
  10. config Configure eden
  11. optional arguments:
  12. -h, --help show this help message and exit

Hint: you can use -h on subcommands as well:

  1. $ eden config -h
  2. usage: eden config [-h] {setup,check,push,remote-rm} ...
  3. positional arguments:
  4. {setup,check,push,remote-rm}
  5. setup Setup profiles for other commands
  6. check Check configuration file integrity
  7. push Push local profile to DynamoDB for use by eden API
  8. remote-rm Delete remote profile from DynamoDB
  9. optional arguments:
  10. -h, --help show this help message and exit
  11. $ eden config push -h
  12. usage: eden config push [-h] [-p PROFILE] [-c CONFIG_PATH] [-v]
  13. [--remote-table-name REMOTE_TABLE_NAME]
  14. optional arguments:
  15. -h, --help show this help message and exit
  16. -p PROFILE, --profile PROFILE
  17. profile name in eden configuration file
  18. -c CONFIG_PATH, --config-path CONFIG_PATH
  19. eden configuration file path
  20. -v, --verbose
  21. --remote-table-name REMOTE_TABLE_NAME
  22. profile name in eden configuration file

Configure

Let’s create a profile to work with,
so we won’t have to specify all the parameters every time

  1. $ eden config setup --endpoint-s3-bucket-name servicename-config
  2. $ eden config setup --endpoint-s3-key endpoints.json
  3. $ eden config setup --endpoint-name-prefix servicename-dev
  4. $ eden config setup --endpoint-update-key api_endpoint
  5. $ eden config setup --endpoint-env-type dev
  6. $ eden config setup --domain-name-prefix api
  7. $ eden config setup --dynamic-zone-id Zxxxxxxxxxxxx
  8. $ eden config setup --master-alb-arn arn:aws:elasticloadbalancing:ap-northeast-1:xxxxxxxxxxxx:loadbalancer/app/dev-alb-api-dynamic/xxxxxxxxxx
  9. $ eden config setup --name-prefix dev-dynamic
  10. $ eden config setup --reference-service-arn arn:aws:ecs:ap-northeast-1:xxxxxxxxxxxx:service/dev/dev01-api
  11. $ eden config setup --target-cluster dev

Configuration is saved to ~/.eden/config.
Commands above created a “default” profile.

  1. $ cat ~/.eden/config
  2. [api]
  3. name_prefix = dev-dynamic
  4. reference_service_arn = arn:aws:ecs:ap-northeast-1:xxxxxxxxxxxx:service/dev/dev01-api
  5. target_cluster = dev
  6. domain_name_prefix = api
  7. master_alb_arn = arn:aws:elasticloadbalancing:ap-northeast-1:xxxxxxxxxxxx:loadbalancer/app/dev-alb-api-dynamic/xxxxxxxxxx
  8. dynamic_zone_name = dev.example.com.
  9. dynamic_zone_id = Zxxxxxxxxxxxx
  10. config_bucket_name = servicename-config
  11. config_bucket_key = endpoints.json
  12. config_update_key = api_endpoint
  13. config_env_type = dev
  14. config_name_prefix = servicename-dev
  15. target_container_name = api

Don’t forget to check configuration file integrity:

  1. $ eden config check
  2. No errors found

Profiles

You can create multiple profiles in configuration
and specify a profile to use with -p profile_name for all commands.

  1. $ eden config check -p api
  2. No errors found

We can push profiles to DynamoDB for use by eden API
(if eden table does not exist, aws-eden-cli will create it)

  1. $ eden config push -p api
  2. Waiting for table creation...
  3. Successfully pushed profile api to DynamoDB

Use the same command to overwrite existing profiles
(push to existing profile will result in overwrite)

  1. $ eden config push -p api
  2. Successfully pushed profile api to DynamoDB table eden

Use remote-rm to delete remote profiles:

  1. $ eden config remote-rm -p api
  2. Successfully removed profile api from DynamoDB table eden

Execute commands

Create an environment:

  1. $ eden create -p api --name foo --image-uri xxxxxxxxxx.dkr.ecr.ap-northeast-1.amazonaws.com/api:latest
  2. Checking if image xxxxxxxxxx.dkr.ecr.ap-northeast-1.amazonaws.com/api:latest exists
  3. Image exists
  4. Retrieved reference service arn:aws:ecs:ap-northeast-1:xxxxxxxxxx:service/dev/api
  5. Retrieved reference task definition from arn:aws:ecs:ap-northeast-1:xxxxxxxxxx:task-definition/api:20
  6. Registered new task definition: arn:aws:ecs:ap-northeast-1:xxxxxxxxxx:task-definition/dev-dynamic-api-foo:1
  7. Registered new task definition: arn:aws:ecs:ap-northeast-1:xxxxxxxxxx:task-definition/dev-dynamic-api-foo:1
  8. Retrieved reference target group: arn:aws:elasticloadbalancing:ap-northeast-1:xxxxxxxxxx:targetgroup/api/xxxxxxxxxxxx
  9. Existing target group dev-dynamic-api-foo not found, will create new
  10. Created target group arn:aws:elasticloadbalancing:ap-northeast-1:xxxxxxxxxx:targetgroup/dev-dynamic-api-foo/xxxxxxxxxxxx
  11. ELBv2 listener rule for target group arn:aws:elasticloadbalancing:ap-northeast-1:xxxxxxxxxx:targetgroup/dev-dynamic-api-foo/xxxxxxxxxxxx and host api-foo.dev.example.com does not exist, will create new listener rule
  12. ECS Service dev-dynamic-api-foo does not exist, will create new service
  13. Checking if record api-foo.dev.example.com. exists in zone Zxxxxxxxxx
  14. Successfully created CNAME: api-foo.dev.example.com -> dev-alb-api-dynamic-297517510.ap-northeast-1.elb.amazonaws.com
  15. Updating config file s3://example-com-config/endpoints.json, environment example-api-foo: nodeDomain -> api-foo.dev.example.com
  16. Existing environment not found, adding new
  17. Successfully updated config file
  18. Successfully finished creating environment dev-dynamic-api-foo

Check creation:

  1. $ eden ls
  2. Profile api:
  3. dev-dynamic-api-foo api-foo.dev.example.com (last updated: 2019-11-20T19:44:10.179760)

Delete environment and check deletion:

  1. $ eden delete -p api --name foo
  2. Updating config file s3://example-com-config/endpoints.json, delete environment example-api-foo: nodeDomain -> api-foo.dev.example.com
  3. Existing environment found, and the only optional key is nodeDomain,deleting environment
  4. Successfully updated config file
  5. Checking if record api-foo.dev.example.com. exists in zone Zxxxxxxxxx
  6. Found existing record api-foo.dev.example.com. in zone Zxxxxxxxxx
  7. Successfully removed CNAME record api-foo.dev.example.com
  8. ECS Service dev-dynamic-api-foo exists, will delete
  9. Successfully deleted service dev-dynamic-api-foo from cluster dev
  10. ELBv2 listener rule for target group arn:aws:elasticloadbalancing:ap-northeast-1:xxxxxxxxxx:targetgroup/dev-dynamic-api-foo/xxxxxxxxxxxx and host api-foo.dev.example.com found, will delete
  11. Deleted target group arn:aws:elasticloadbalancing:ap-northeast-1:xxxxxxxxxx:targetgroup/dev-dynamic-api-foo/xxxxxxxxxxxx
  12. Deleted all task definitions for family: dev-dynamic-api-foo, 1 tasks deleted total
  13. Successfully finished deleting environment dev-dynamic-api-foo
  14. $ eden ls
  15. No environments available