I have the following python3 code. The purpose is to terminate all stopped instances that pass the given filters. I am able to make the code work, just that I am finding it difficult to pass the list (multiline) of instance ids onto another variable - Long story short, I am not able to convert the multiline output of strings into a single line string with each output in single quotes separated by commas. I will provide the example output desired right below my code:
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)
Output of the print command is as follows:
i-06c517d7378ef7070
i-0155f6f9dbfe7b76e
i-08c5d054b6dbde86f
If we see the commented out line
ids = ['i-089d6e80fa3f59129']
I want the output to be passed on as:
ids = ['i-06c517d7378ef7070','i-0155f6f9dbfe7b76e','i-08c5d054b6dbde86f']
How is this possible? I tried multiple ways like join, split, etc but it ends up adding delimiter within each string.
Please help. Thanks
I found a solution to my overall problem finally. Here is the updated code:
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.