Python脚本无法将AWS配置文件名称选项传递给脚本

时间:2018-10-29 11:36:23

标签: python-3.x boto3

我正在尝试将AWS profile选项传递给python脚本。我在脚本中使用click模块。当出现以下错误消息时,我的脚本失败。

这是错误消息

bash-3.2$ pipenv run python snapst.py --help
Traceback (most recent call last):
  File "snapst.py", line 29, in <module>
    session = ses_init(profile)
NameError: name 'profile' is not defined
bash-3.2$ 

这是我的代码。

import boto3
import botocore
import click

def filter_instances(project):
    instances = []

    if project:
        filters = [{'Name':'tag:Project', 'Values':[project]}]
        instances = ec2.instances.filter(Filters=filters)
    else:
        instances = ec2.instances.all()

    return instances

def has_pending_snaphost(volume):
    snapshots = list(volume.snapshots.all())
    return snapshots and snapshot[0].state == 'pending'

@click.group()
def cli():
    """Snapshot mantains snapshot of volumes"""

@click.option('--profile', default=None, help="Profile name for AWS access keys")
def ses_init(profile):
    ses = boto3.Session(profile_name=profile)
    return ses

session = ses_init(profile)
ec2 = session.resource('ec2')

<script truncated>

if __name__ == '__main__':
    cli()

0 个答案:

没有答案