2017-03-07 13 views
1

boto3でcreate_snapshot()メソッドを呼び出すときにタグを追加できますか?私は次のコードを実行すると:boto3を使用してEBSスナップショットを作成中にタグを追加

client = boto3.client('ec2') 

root_snap_resp = client.create_snapshot(
    Description='My snapshot description', 
    VolumeId='vol-123456', 
    Tags=[{'Key': 'Test_Key', 'Value': 'Test_Value'}] 
) 

を私は次のエラーを取得する:

botocore.exceptions.ParamValidationError: Parameter validation failed: 
Unknown parameter in input: "Tags", must be one of: DryRun, VolumeId, Description 

はcreate_tags()メソッドを使用して事実の後のタグを追加する唯一の方法ですか?

+0

はい。 'create_tags'は、EC2リソース用のタグを追加する唯一の方法です。 – helloV

答えて

1

EC2 APIの基礎となるCreateSnapshot actionには、スナップショットの作成と同時にタグを追加する手段がありません。あなたは戻ってそれを作成した後にタグをつけなければなりません。

API(とCLI)からの応答に実際にタグの空の配列が含まれているのは奇妙なようですが、それが実装された方法です。

0

は私のバックアップスクリプトを見てください:

import boto3 
import collections 
import datetime 

ec = boto3.client('ec2') 

def lambda_handler(event, context): 
    reservations = ec.describe_instances(
     Filters=[ 
      {'Name':'tag:Backup', 'Values':['Yes','yes']} 
     ] 
    ).get(
     'Reservations', [] 
    ) 

    instances = sum(
     [ 
      [i for i in r['Instances']] 
      for r in reservations 
     ], []) 

    print "Found %d instances that need backing up" % len(instances) 

    to_tag = collections.defaultdict(list) 

    for instance in instances: 
     try: 
      retention_days = [ 
       int(t.get('Value')) for t in instance['Tags'] 
       if t['Key'] == 'Retention'][0] 
     except IndexError: 
      retention_days = 30 

     for dev in instance['BlockDeviceMappings']: 
      if dev.get('Ebs', None) is None: 
       continue 
      vol_id = dev['Ebs']['VolumeId'] 
      print "Found EBS volume %s on instance %s" % (
       vol_id, instance['InstanceId']) 

      snap = ec.create_snapshot(
       VolumeId=vol_id, 
      ) 

      to_tag[retention_days].append(snap['SnapshotId']) 

      print "Retaining snapshot %s of volume %s from instance %s for %d days" % (
       snap['SnapshotId'], 
       vol_id, 
       instance['InstanceId'], 
       retention_days, 
      ) 

      snapshot_name = 'N/A' 
      if 'Tags' in instance: 
       for tags in instance['Tags']: 
        if tags["Key"] == 'Name': 
         snapshot_name = tags["Value"] 

      print "Tagging snapshot with Name: %s" % (snapshot_name) 

      ec.create_tags(
       Resources=[ 
        snap['SnapshotId'], 
       ], 
       Tags=[ 
        {'Key': 'Name', 'Value': snapshot_name}, 
        {'Key': 'Description', 'Value': "Created by lambda automated backups"} 
       ] 
      ) 

    for retention_days in to_tag.keys(): 
     delete_date = datetime.date.today() + datetime.timedelta(days=retention_days) 
     delete_fmt = delete_date.strftime('%Y-%m-%d') 
     print "Will delete %d snapshots on %s" % (len(to_tag[retention_days]), delete_fmt) 
     ec.create_tags(
      Resources=to_tag[retention_days], 
      Tags=[ 
       {'Key': 'DeleteOn', 'Value': delete_fmt} 
      ] 
     ) 

そして、この本私のスクリプトをYYYY-MM-DD形式で

import boto3 
import re 
import datetime 

ec = boto3.client('ec2') 
iam = boto3.client('iam') 

""" 
This function looks at *all* snapshots that have a "DeleteOn" tag containing 
the current day formatted as YYYY-MM-DD. This function should be run at least 
daily. 
""" 

def lambda_handler(event, context): 
    account_ids = list() 
    try: 
     """ 
     You can replace this try/except by filling in `account_ids` yourself. 
     Get your account ID with: 
     > import boto3 
     > iam = boto3.client('iam') 
     > print iam.get_user()['User']['Arn'].split(':')[4] 
     """ 
     iam.get_user() 
    except Exception as e: 
     # use the exception message to get the account ID the function executes under 
     account_ids.append(re.search(r'(arn:aws:sts::)([0-9]+)', str(e)).groups()[1]) 


    delete_on = datetime.date.today().strftime('%Y-%m-%d') 
    filters = [ 
     {'Name': 'tag-key', 'Values': ['DeleteOn']}, 
     {'Name': 'tag-value', 'Values': [delete_on]}, 
    ] 
    snapshot_response = ec.describe_snapshots(OwnerIds=account_ids, Filters=filters) 


    for snap in snapshot_response['Snapshots']: 
     print "Deleting snapshot %s" % snap['SnapshotId'] 
     ec.delete_snapshot(SnapshotId=snap['SnapshotId']) 
を今日の値を持つ「delete_on」タグを持っている古いバックアップを削除します
関連する問題