将多行实例ID的输出传递给另一个输入(EC2-boto3)



我有以下python3代码。其目的是终止所有通过给定筛选器的已停止实例。我能够使代码正常工作,只是我发现很难将实例ID的列表(多行(传递到另一个变量上——长话短说,我无法将字符串的多行输出转换为单行字符串,每个输出都用逗号分隔的单引号。我将在我的代码下面提供所需的示例输出:

import jmespath
import boto3
from botocore.exceptions import ClientError
from pathlib import Path
import re
import configparser
homepath = str(Path.home())
config = configparser.ConfigParser()
config.read(homepath + '/Documents/Projects/in-team/scripts/python/config')
result = []

class StackName:
pass

for section_name in config.sections():
# Removes the word "profile" from the profile name, if present
if re.search('^profile', section_name):
section_name = re.split(' ', section_name, maxsplit=1)[1]

session = boto3.session.Session(
profile_name=section_name,
region_name='us-west-2'
)
# Create EC2 client
ec2client = session.resource('ec2')
try:
instances = ec2client.instances.filter(
Filters=[
{
'Name': 'instance-state-name',
'Values': ['stopped']
},
# {
#     'Name': 'tag:Name',
#     'Values': ['migrated*']
# },
{
'Name': 'tag:Environment',
'Values': ['Dev']
}
]
)
for instance in instances:
# print(instance.id.split())
print([instance.id)
# ids = ['i-089d6e80fa3f59129']
# for id in ids:
#     ec2client.Instance(id).modify_attribute(
#         DisableApiTermination={
#             'Value': False
#         }
#     )
# ec2client.Instance(id).terminate()

except ClientError as e:
if (e.response['Error']['Code'] == 'ValidationError' and
e.response['Error']['Message'] == "No updates are to be performed."):
print("%s: No updates are to be performed." % StackName)
else:
print("Unexpected error: %s" % e)

打印命令的输出如下:

i-06c517d7378ef7070
i-0155f6f9dbfe7b76e
i-08c5d054b6dbde86f

如果我们看到有注释的输出行

ids = ['i-089d6e80fa3f59129']

我希望输出传递为:

ids = ['i-06c517d7378ef7070','i-0155f6f9dbfe7b76e','i-08c5d054b6dbde86f']

这怎么可能?我尝试了多种方法,如联接、拆分等,但最终在每个字符串中添加了分隔符。

请帮忙。感谢

我终于找到了解决整体问题的方法。这是更新后的代码:

import jmespath
import boto3
from botocore.exceptions import ClientError
from pathlib import Path
import re
import configparser
homepath = str(Path.home())
config = configparser.ConfigParser()
config.read(homepath + '/Documents/Projects/in-team/scripts/python/config')
result = []

class StackName:
pass

for section_name in config.sections():
# Removes the word "profile" from the profile name, if present
if re.search('^profile', section_name):
section_name = re.split(' ', section_name, maxsplit=1)[1]

session = boto3.session.Session(
profile_name=section_name,
region_name='us-west-2'
)
# Create EC2 client
ec2client = session.resource('ec2')
try:
instances = ec2client.instances.filter(
Filters=[
{
'Name': 'instance-state-name',
'Values': ['stopped']
},
{
'Name': 'tag:Name',
'Values': ['decommissioned*']
},
{
'Name': 'tag:Environment',
'Values': ['Dev']
}
]
)
instance_ids = [instance.id for instance in instances]
print(instance_ids)
for id in instance_ids:
ec2client.Instance(id).modify_attribute(
DisableApiTermination={
'Value': False
}
)
ec2client.Instance(id).terminate()

except ClientError as e:
if (e.response['Error']['Code'] == 'ValidationError' and
e.response['Error']['Message'] == "No updates are to be performed."):
print("%s: No updates are to be performed." % StackName)
else:
print("Unexpected error: %s" % e)

最新更新