Skip to main content

Tool to run ecs execute command in lambda.

Project description

interloper

Tool to run ecs execute command in Python and in AWS Lambda. Of course, locally you can do this via the session manager plugin, but it you want to do it in lambda, it is a bit more complex.

Features

  • Run simple commands or full scripts.
  • Pass arguments into those scripts.
  • Get output back in python.

Limitations

Here are some of the current limitations.

  • Script output is truncated if very long.
  • Testing shell is present, but still must be completed.
    • Still needs started really.
    • Need to mock away the websocket bits.
  • This documentation.

Installation

pip install interloper

Usage

For starters, one must enable ECS exec.

Locally

./interloper.py [event]

Lambda

You can import and use the provided handlers for simple operations.

cmd_handler

from interloper import cmd_handler

... Actually, that should be sufficient. Just set the handler to main.cmd_handler or whatever your module is called in place of main.

script_handler

from interloper import script_handler

Then create an interloper.sh that suits your needs.

Custom Handler and Script

Consider needing to generate heap dumps and thread dumps from java containers. You could do so with the following.

dumper.sh

#!/usr/bin/env sh

cleanup () {
    rm -rf /tmp/dumps
    mkdir -p /tmp/dumps
}

verify_awscli () {
    if which aws >/dev/null
    then
        echo "awscli found; proceeding"
        return 0
    else
        echo "awscli not found; can't proceed"
        return 1
    fi
}

verify_jattach () {
    if which jattach >/dev/null
    then
        echo "jattach found; proceeding"
        return 0
    else
        echo "jattach not installed; installing"
        if which apk >/dev/null
        then
            apk add --no-cache jattach --repository http://dl-cdn.alpinelinux.org/alpine/edge/community/
            return 0
        else
            echo "apk not found; can't proceed"
            return 1
        fi
    fi
}

cleanup

case "$1" in
    heap)
        if verify_jattach; then jattach $(pgrep java) dumpheap /tmp/dumps/heap.hprof; fi
        if verify_awscli; then aws s3 cp /tmp/dumps/heap.hprof s3://"$2"; fi
        ;;
    threads)
        if verify_jattach; then jattach $(pgrep java) threaddump > /tmp/dumps/threads; fi
        if verify_awscli; then aws s3 cp /tmp/dumps/threads s3://"$2"; fi
        ;;
    *)
        echo "usage: $0 {heap|threads} {key}"
esac

handler

import logging
import time

import interloper


LOG = logging.getLogger()
LOG.setLevel(logging.INFO)


def lambda_handler(event: dict, context) -> None:
    LOG.debug("event received: %s", event)
    input = interloper.Input(**event)
    key = "{}/{}/{}-{}".format(input.cluster, input.task, input.cmd, int(time.time()))
    if input.cmd == "heap":
        key += ".hprof"
    output = interloper.Interloper(
        input.task, cluster=input.cluster, container=input.container
    ).script("dumper.sh", [input.cmd, key])
    LOG.info(output)

Project details


Download files

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

Source Distribution

interloper-0.1.2.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

interloper-0.1.2-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file interloper-0.1.2.tar.gz.

File metadata

  • Download URL: interloper-0.1.2.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.11

File hashes

Hashes for interloper-0.1.2.tar.gz
Algorithm Hash digest
SHA256 956acb6c418e991344698f3e0d22cd3d8da5865aa9f8d6ebb648d3c56e7312eb
MD5 888b5b5826e5700d4022cbfcc4216d44
BLAKE2b-256 ed942833102ea6dba4c6c6f928f9722ec5f5be9ef321f7c6fa222a343462b018

See more details on using hashes here.

File details

Details for the file interloper-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: interloper-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.11

File hashes

Hashes for interloper-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ccf4a94601ea7d276796135aa89853b9c3f748dcf2db7b524b75ee96387816d8
MD5 fad6e026c06aafb7700ab035d2f79f8f
BLAKE2b-256 15605d3c29703d62918c012c5f50c7de0d12a3337d8ec9a52bed29ad18fe35e4

See more details on using hashes here.

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