I'm putting together a Python3 tool that allows me to interact with AWS SDK and pull back certain information from AWS, but I'm having trouble getting it to provide an output, and provide a different output based on an option. Explaining the code may help to understand.
import boto3
import click
@click.group()
@click.option('--profile', default=None, help='Specify AWS Profile')
@click.option('--region', default=None, help='Specify AWS Region')
@click.pass_context
def cli(ctx, profile, region):
ctx.obj = Params(profile, region)
@cli.command(name='ec2')
@click.pass_obj
def ec2(ctx, update):
print("list_instances")
@ec2.command()
@click.pass_obj
def update_instances(ctx):
print("update instances")
if __name__ == '__main__':
cli()
Ideally I'd like to be able to run the following:
# app.py ec2
<-- this would return a list of instances
# app.py ec2 --update instance_id/all
<-- This would update SSM agent on either all returned Ids or a specific instance Id.
I can get it to return the list of instances but no additional options, or I can get it to return the update command, but no list of instances.
Many thanks, Zak
import boto3
import click
@click.group()
@click.option('--profile', default=None, help='Specify AWS Profile')
@click.option('--region', default=None, help='Specify AWS Region')
@click.pass_context
def cli(ctx, profile, region):
ctx.obj = Params(profile, region)
@cli.command(name='ec2')
@click.option('--update', default=None)
@click.pass_obj
def ec2(ctx, update):
if update is not None:
update_instances(ctx, update)
else:
print("list_instances")
def update_instances(ctx, update):
print("update instances")
if __name__ == '__main__':
cli()
One caveat, in your example the variable Params
is not defined, not sure if it was on purpose or you just forgot to include it in the question.