python-3.x - 如何在 EC2 实例与列入白名单的 AMI ID 不匹配时标记它并将其标记为“未批准”
问题描述
如何将处于运行状态的新实例标记为停止状态,此 lambda 函数用于匹配列入白名单的 AMI ID(AMI ID 在 S3 的记事本中),如果该实例与批准的 AMI ID 不匹配,则 ec2 将被迫停止并使用 Python 3 将其标记为“未批准”。
import boto3
import botocore
def lambda_handler(event, context):
account = event['account']
s3 = boto3.client("s3")
bucketname = 'amivalidation-demo'
filename = 'ami-s3.csv'
fileObj = s3.get_object(Bucket=bucketname, Key=filename)
file_content = fileObj["Body"].read().decode('utf-8')
ec2_client = get_client('ec2',account)
response = event['detail']['responseElements']['instancesSet']
for res in response['items']:
instance = res['instanceId']
image = res['imageId']
if image in file_content:
#image in whitelist
pass
else:
#image not in whitelist, kill it!
ec2_client.stop_instances(InstanceIds=[instance],Force=True)
return
解决方案
response = ec2_client.create_tags(
Resources=['instance-id'],
Tags=[
{
'Key': 'string',
'Value': 'string'
},
]
)
推荐阅读
- c++ - 未找到重载的成员函数,类在header中
- c# - WPF ComboBox:将 SelectedItem 绑定到类成员
- assembly - 如何使用循环终止递归函数?
- javascript - 达到存储限制时如何自动为本地 Pouch 数据库腾出空间?
- javascript - 使用 Objects (JS) 中的对象构建 Json
- java - 是否可以在 UI Automator 测试中扫描 QR 码以进行 AWS 农场设备测试?
- mysql - 以非交互方式在命令行中执行 Google Cloud SQL 查询
- docker - 启动 docker 容器导致 docker-credential-secretservice: The connection is closed
- printing - 如何修复“对象不支持 button.print.min.js 中的属性或方法‘exportInfo’”
- javascript - 如何通过单击外部来隐藏 div?