I made a process to get AMI using AWS Lambda. I have made other ones, but the code is similar ...
Gets the AMI of the instance with the tag env: dev. The AMI name is created with dev-date time.
I'm usually an infrastructure shop, so it's fun to make new discoveries like this.
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
https://github.com/handa3/study/blob/master/aws/lambda/create_ami.py
Recommended Posts