4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

AWS Lambdaを使って、AMIを取得してみました

Posted at

AWS Lambdaを利用してAMIを取得する処理を作ってみました。
他にも作ってみたものがありますが、似たコードになってしまう・・・

タグにenv: devが設定されたインスタンスのAMIを取得します。
AMI名はdev-日付時間で作成されます。

普段はインフラ屋さんなのでこういったものを作ると新しい発見があって楽しいです。


from __future__ import print_function
import boto3
from boto3.session import Session
from datetime import datetime

ec2 = boto3.client('ec2')
dev_list = []
img_name = "dev-" + datetime.now().strftime("%Y%m%d%H")

# def
def get_list():
  instance_list = ec2.describe_instances(
    Filters=[{'Name': 'tag:env', 'Values': ['dev']}]
  )
  for Reservations in instance_list['Reservations']:
    for dev_instances in Reservations['Instances']:
      dev_list.append(dev_instances["InstanceId"])
      return dev_list

def create_image(dev_list):
  for instance_id in dev_list:
    response = ec2.create_image(
      InstanceId = instance_id,
      Name = img_name,
      NoReboot = True
    )
    
def lambda_handler(event, context):
  get_list()
  create_image(dev_list)
  return dev_list

4
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?