- Lambda 특정 태그를 가진 Instance 종료하기2024년 12월 24일
- wngnl05
- 작성자
- 2024.12.24.:28
import boto3 region = "" # 리전 이름 key = "" # 태그 KEY value = "" # 태그 VALUE instances = [] ec2_r = boto3.resource('ec2', region_name=region) ec2 = boto3.client('ec2', region_name=region) for instance in ec2_r.instances.all(): # 특정 태그가 있는 인스턴스 id 가져오기 for tag in instance.tags: if tag['Key'] == f'{key}': # Key if tag['Value'] == f'{value}': # Value instances.append(instance.id) ec2.terminate_instances(InstanceIds = instances) # 인스턴스 id를 이용하여 인스터스 종료 print('stopped your instances: ' + str(instances))
ec2를 중지할려면?
ec2.stop_instances(InstanceIds = instances)
변수들을 상황에 맞게 작성하고 코드를 실행시키면 특정 태그가 있는 인스턴스들은 모두 종료가 됩니다.
'Aws' 카테고리의 다른 글
EC2 Python 설치 (0) 2024.12.24 Jenkins 설치하기 (0) 2024.12.24 AWS Nuke를 사용해서 AWS 리소스 모두 삭제하기 (0) 2024.12.24 EC2 Docker 설치하기 (0) 2024.12.24 EC2 사용자 데이터 (0) 2024.12.24 다음글이전글이전 글이 없습니다.댓글
스킨 업데이트 안내
현재 이용하고 계신 스킨의 버전보다 더 높은 최신 버전이 감지 되었습니다. 최신버전 스킨 파일을 다운로드 받을 수 있는 페이지로 이동하시겠습니까?
("아니오" 를 선택할 시 30일 동안 최신 버전이 감지되어도 모달 창이 표시되지 않습니다.)