Package: aws-cloud-unmap
AWS Cloud (un)MapExternal controller to remove terminated EC2 instances from AWS Cloud Map service.How it worksThis application scans - at a regular interval - the instances registered to a Cloud Map service and match them with the EC2 instances running in 1+ region: it will then deregister any instance registered in the service which doesn't match a running EC2 instance.Requisites:The instance must be registered in the Cloud Map service with Cloud Map instance id equal to the EC2 instance idThe instance must be registered in the Cloud Map service withAWS_INSTANCE_IPV4attribute (can be the private or public IP address)How the matching is done:A registered instance is considered valid ifboththe instance id and theAWS_INSTANCE_IPV4address match a running EC2 instanceA registered instance is skipped (left untouched) if registered withoutAWS_INSTANCE_IPV4attributeSafety countermeasures:The application logs a warning and donotderegister the unmatching instances, in case that would leave the service without registered instanceThe application handles graceful shutdown onSIGINTandSIGTERM. If such signals are received during a reconciling, it would complete the on-going reconcile before exitingHow to run itYou have two options to run it:Manually install and run theaws-cloud-unmapPython packagepip3 install aws-cloud-unmap

aws-cloud-unmap --service-id srv-12345 --service-region us-east-1 --instances-region us-east-1Use theDocker image available on Docker hubdocker run --env AWS_ACCESS_KEY_ID="id" --env AWS_SECRET_ACCESS_KEY="secret" spreaker/aws-cloud-unmap --service-id srv-12345 --service-region us-east-1 --instances-region us-east-1The cli supports the following arguments:ArgumentRequiredDescription--service-id IDyesAWS CloudMap service ID--service-region REGIONyesAWS CloudMap service region--instances-region REGION [REGION ...]yesAWS regions where EC2 instances should be checked--frequency NHow frequently the service should be reconciled (in seconds). Defaults to300sec--single-runRun a single reconcile and then exit--enable-prometheusEnable the Prometheus exporter. Disabled by default--prometheus-hostThe host at which the Prometheus exporter should listen to. Defaults to127.0.0.1--prometheus-portThe port at which the Prometheus exporter should listen to. Defaults to9100--log-level LOG_LEVELMinimum log level. Accepted values are:DEBUG,INFO,WARNING,ERROR,CRITICAL. Defaults toINFOExported metricsThe application features an integrated Prometheus exporter. The following metrics are exported:Metric nameLabelsDescriptionaws_cloud_unmap_upservice_idAlways1: can be used to check if it's runningaws_cloud_unmap_last_reconcile_success_timestamp_secondsservice_idThe timestamp (in seconds) of the last successful reconciliationRequired IAM privilegesIn order to successfully run, this application requires the following IAM privileges:{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid":      "DescribeEC2Instances",
      "Effect":   "Allow",
      "Action":   [ "ec2:DescribeInstances" ],
      "Resource": "*"
    },{
      "Sid":      "ListAndDeregisterServiceInstances",
      "Effect":   "Allow",
      "Action":   [
        "servicediscovery:ListInstances",
        "servicediscovery:DeregisterInstance",
        "route53:GetHealthCheck",
        "route53:DeleteHealthCheck",
        "route53:UpdateHealthCheck"
      ],
      "Resource": "*"
    },{
      "Sid":      "UpdateDnsWhileDeregisteringServiceInstances",
      "Effect":   "Allow",
      "Action":   [ "route53:ChangeResourceRecordSets" ],
      "Resource": [
        "ARN-OF-YOUR-ROUTE53-HOSTEDZONE"
      ]
    }
  ]
}DevelopmentRun the development environment:docker-compose build dev && docker-compose run --rm devRun tests in the dev environment:python3 -m unittestLicenseThis software is released under theMIT license.