[PYTHON] I tried to get an AMI using AWS Lambda

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

I tried to get an AMI using AWS Lambda
I tried to get a list of AMI Names using Boto3
I tried using AWS Chalice
I tried to become an Ann Man using OpenCV
I tried to create an environment to check regularly using Selenium with AWS Fargate
I tried to get Web information using "Requests" and "lxml"
I tried to get data from AS / 400 quickly using pypyodbc
I tried using Azure Speech to Text.
I tried to get a database of horse racing using Pandas
I tried to get the index of the list using the enumerate function
I tried to get started with Hy
Regularly post to Twitter using AWS lambda!
I tried to classify text using TensorFlow
[Python] I tried to get various information using YouTube Data API!
I tried to get data from AS / 400 quickly using pypyodbc Preparation 1
I tried to predict Covid-19 using Darts
I tried to delete bad tweets regularly with AWS Lambda + Twitter API
I tried to notify Zabbix Server of execution error of AWS Lambda function
I tried to get the batting results of Hachinai using image processing
[Lambda] I tried to incorporate an external module of python via S3
I tried using AWS Rekognition's Detect Labels API
I tried to synthesize WAV files using Pydub.
I tried to get CloudWatch data with Python
I tried to detect an object with M2Det!
I tried connecting AWS Lambda with other services
I tried to make a ○ ✕ game using TensorFlow
I tried using parameterized
I tried using argparse
I tried using mimesis
I tried using anytree
I tried using aiomysql
I tried using Summpy
I tried using coturn
I tried using Pipenv
I tried using matplotlib
I tried using "Anvil".
I tried using Hubot
I tried using ESPCN
I tried using openpyxl
I tried AWS CDK!
I tried using Ipython
I tried using PyCaret
I tried using cron
I tried using ngrok
I tried using face_recognition
I tried to paste
I tried using Jupyter
I tried using PyCaret
I tried using Heapq
I tried using doctest
I tried using folium
I tried using jinja2
I tried AWS Iot
I tried using folium
I tried using time-window
Python programming: I tried to get (crawling) news articles using Selenium and BeautifulSoup4.
Feel free to turn Python using the library into an AWS Lambda function
I tried to reduce costs by starting / stopping EC2 collectively on AWS Lambda
I tried to use Twitter Scraper on AWS Lambda and it didn't work.
The first step to get rid of slow queries! I tried to notify Chatwork of slow queries for RDS for MySQL using Lambda and AWS CLI v2
I tried to get started with blender python script_Part 01