Skip to content

Instantly share code, notes, and snippets.

@pcolazurdo
Created March 8, 2021 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pcolazurdo/720df441a8a9ba34f2a2fc8d32066769 to your computer and use it in GitHub Desktop.
Save pcolazurdo/720df441a8a9ba34f2a2fc8d32066769 to your computer and use it in GitHub Desktop.
Sends a script to SSM Run Command
import json
import boto3
import argparse
class Args(object):
import argparse
parser = argparse.ArgumentParser(
description='Allows user to execute a local script using SSM Run Commands')
parser.add_argument(
'--targets',
dest="cmdTargets",
default='',
nargs = '?',
help='An array of search criteria that targets instances using a Key,Value combination that you specify. Specifying targets is most useful when you want to send a command to a large number of instances at once. Using Targets , which accepts tag key-value pairs to identify instances, you can send a command to tens, hundreds, or thousands of instances at once. # Example: [{"Key":"tag:Purpose","Values":["EBSLatencyTest"]}]'
)
parser.add_argument(
'--region',
nargs = '?',
default='us-east-1',
dest="cmdRegion",
help="Region where to run the command"
)
parser.add_argument(
'--timeout',
nargs = '?',
default=600,
dest="cmdTimeoutSeconds",
help="If this time is reached and the command has not already started running, it will not run (TimeoutSeconds)."
)
parser.add_argument(
'--concurrency',
nargs = '?',
dest="cmdMaxConcurrency",
default=100,
help="The maximum number of instances that are allowed to run the command at the same time. You can specify a number such as 10 or a percentage such as 10%%. The default value is 50 (MaxConcurrency)"
)
parser.add_argument(
'--errors',
nargs = '?',
dest="cmdMaxErrors",
default=0,
help="The maximum number of errors allowed without the command failing. When the command fails one more time beyond the value of MaxErrors, the systems stops sending the command to additional targets. You can specify a number like 10 or a percentage like 10%%. The default value is 0 (MaxErrors)."
)
parser.add_argument(
'--bucket',
dest="cmdOutputBucketName",
help="The name of the S3 bucket where command execution responses should be stored (OutputS3BucketName)."
)
parser.add_argument(
'--prefix',
dest="cmdOutput_s3KeyPrefix",
help="The directory structure within the S3 bucket where the responses should be stored (OutputS3KeyPrefix)"
)
parser.add_argument(
'scriptName',
help="Local path to the script to run."
)
def parse(self):
args = self.parser.parse_args()
return args
def main(args):
scriptText = open(args.scriptName)
lines = scriptText.readlines()
arr = []
for line in lines:
arr.append(line.strip())
cmdName = "AWS-RunShellScript"
cmdVersion = "1"
# targets = [{"Key":"tag:Purpose","Values":["EBSLatencyTest"]}]
cmdTargets = json.loads(args.cmdTargets)
# cmdTargets = json.dumps(targets, separators=(',', ':'))
cmdParametersBuild = {
"workingDirectory": [""],
"executionTimeout": ["36000"],
"commands": arr
}
# cmdParameters = json.dumps(cmdParametersBuild, separators=(',', ':'))
ssm = boto3.client('ssm')
response = ssm.send_command(
DocumentName=cmdName,
DocumentVersion=str(cmdVersion),
Targets=cmdTargets,
TimeoutSeconds=args.cmdTimeoutSeconds,
Parameters=cmdParametersBuild,
OutputS3BucketName=args.cmdOutputBucketName,
OutputS3KeyPrefix=args.cmdOutput_s3KeyPrefix,
MaxConcurrency=str(args.cmdMaxConcurrency),
MaxErrors=str(args.cmdMaxErrors),
)
a = Args()
args = a.parse()
# print(args)
main(args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment