简体   繁体   中英

Convert AWS CLI command into Python AWS Lambda function

I'm using an AWS CLI command to extract results for reporting and I must create a lambda that does the same job but is facing many problems with syntax errors

The current AWS CLI command:

$ aws ec2 describe-instances \
    --query 'Reservations[].Instances[].[Tags[?Key==`platform`]|[0].Value, Tags[?Key==`resource-version`]|[0].Value]| sort_by(@, &[0])' \
    --output table \
    --filter Name=tag:platform,Values=aip,mmt,pame --profile prod | uniq

Could you help me to make the same code with python please?

Now i can print this result:

Deployed versions are:

mmt 1.104.12 mmt 1.104.15 mmt 1.104.15 mmt 1.112.0-SNAPSHOT mmt 1.104.15 mmt 1.104.12 aip 1.112.0-SNAPSHOT mmt 1.112.0-SNAPSHOT mmt 1.104.15 mmt 1.104.12 aip 1.112.0-SNAPSHOT aip 1.112.0-SNAPSHOT mmt 1.112.0-SNAPSHOT

Using this cide `import boto3

def instance_list(tagkey, tagvalue): # When passed a tag key, tag value this will return a list of InstanceIds that were found.

ec2client = boto3.client('ec2')

response = ec2client.describe_instances(
     Filters=[
      {'Name':'tag:platform', 'Values':["mmt","pame","aip"]}
]
)
name = []
instancelist= []
print ("Deployed versions are :")
for reservation in (response["Reservations"]):
    for instance in reservation["Instances"]:
       for tag in instance['Tags']:
           if tag['Key'] == 'platform':
               name.append(tag["Value"])
           if tag['Key'] == 'resource-version':
               instancelist.append(tag["Value"])
a  = [name + instancelist]
for x, y in zip(name, instancelist):
    print (x, y)

`

Am trying to delete duplicated line and send result by mail or slack; Any help please?

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM