Skip to content

Commit

Permalink
Merge pull request #67 from chrisgala/add-sample-dynamic-folder-expor…
Browse files Browse the repository at this point in the history
…t-aws-ssm-ec2

add new sample for AWS EC2 SSM
  • Loading branch information
lemonmojo committed Jul 24, 2023
2 parents 8be939d + 6771512 commit 6b918ca
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions Dynamic Folder/Amazon Web Services/EC2/AWS EC2 SSM (Python).rdfe
@@ -0,0 +1,20 @@
{
"Name": "Dynamic Folder Export",
"Objects": [
{
"Type": "DynamicFolder",
"Name": "AWS EC2 SSM (Python)",
"Description": "This Dynamic Folder sample for AWS SSM EC2 supports grabbing all EC2 instances of a specified region managed by AWS Systems Manager.",
"Notes": "<h2><strong>Dynamic Folder sample for Amazon Web Services (AWS) EC2 managed by SSM</strong></h2>\n\n<p><strong>Version</strong>: 1.0.0<br />\n<strong>Author</strong>: Chrysostomos Galatoulas</p>\n\n<p>This Dynamic Folder sample for AWS EC2 SSM supports grabbing all EC2 instances of a specified region managed by SSM.</p>\n\n<h3><strong>Prerequisites</strong></h3>\n\n<ul>\n\t<li>AWS Command Line Interface (CLI) needs to be installed and configured.</li>\n</ul>\n\n<h3><strong>Setup</strong></h3>\n\n<ul>\n\t<li>Enter the region that you want to grab instances from in the &quot;Region&quot; field in the &quot;Custom Properties&quot; section or leave it as an empty string if you configured the AWS CLI with a default region.</li>\n</ul>\n\n<h3><strong>Notes</strong></h3>\n\n<ul>\n\t<li>You can append the --profile option on AWS cli commands to use a configured profile instead of a default.</li>\n</ul>\n",
"CustomProperties": [
{
"Name": "Region",
"Type": "Text",
"Value": "us-east-1"
}
],
"ScriptInterpreter": "python",
"Script": "import subprocess\nimport json\n\ndef get_instances(region = \"\"):\n\tcmd = \"aws ssm describe-instance-information --output json\"\n\n\tif region != \"\":\n\t\tcmd += \" --region \" + region\n\n\taws = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)\n\t(response_json, err) = aws.communicate()\n\texit_code = aws.wait()\n\n\tresponse = json.loads(response_json)\n\n\tconnections = [ ]\n\n\tfor instance in response.get(\"InstanceInformationList\", None):\n\t\tinstance_id = instance.get(\"InstanceId\", \"\")\n\t\tplatform = instance.get(\"PlatformType\", \"\")\n\n\t\tis_windows = platform.lower() == \"windows\"\n\n\t\tcomputer_name = instance.get(\"ComputerName\", \"\")\n\t\tprivate_ip_address = instance.get(\"IPAddress\", \"\")\n\n\t\tname = instance_id\n\n\t\tconnection = { }\n\t\tif not is_windows:\n\t\t\tconnection[\"Type\"] = \"TerminalConnection\"\n\t\t\tconnection[\"TerminalConnectionType\"] = \"CustomTerminal\"\n\t\telse:\n\t\t\tconnection[\"Type\"] = \"RemoteDesktopConnection\"\n\n\t\tconnection[\"ID\"] = instance_id\n\t\tconnection[\"Name\"] = computer_name\n\t\tconnection[\"ComputerName\"] = computer_name\n\n\t\tconnection[\"CustomCommand\"] = f\"aws ssm start-session --target {instance_id}\"\t\t\n\t\t\n\t\t\n\t\tconnection[\"Properties\"] = {}\n\t\tconnection[\"Properties\"][\"RunInsideLoginShell\"] = True\n\t\t\n\t\t\n\t\tconnections.append(connection)\n\n\tstore = {\n\t\t\"Objects\": connections\n\t}\n\n\tstore_json = json.dumps(store)\n\n\treturn store_json\n\t\nprint(get_instances(\"$CustomProperty.Region$\"))\n"
}
]
}

0 comments on commit 6b918ca

Please sign in to comment.