简体   繁体   中英

how to repeatedly call a function with a list of paired arguments from YAML file to Python using AWS SDK?

I want to write a script which takes AWS account's values from YAML file using python boto 3 and create multiple accounts under AWS organization. Please find the below steps which I want to execute: step 1 : I have list of AWS account's values in YAML file as below:(config.yaml)

Name:
   test1
   test2
Email:
    test1@gmail.com
    test2@gmail.com

step 2 : write a python script to automate the process

import yaml

with open("config.yml", 'r') as ymlfile:
    account = yaml.safe_load(ymlfile)

for section in cfg:
    print(section)
    print(account['Name'])
    print(account['Email'])
  • Pyhon is new for me...I tried with above code to load value from file but it prints only values
  • Can anyone help ,how can I load YAML values in below code ?

    • I can create only one account using below simple script:

       import json import boto3 client = boto3.client('organizations') response = client.create_account( Email="test1@gmail.com", AccountName= "Test1" ) 

The way I see it, your config file does not look right. Having two "parallel" lists is rarely a good idea (I suppose this was your intention, even if the dashes are missing). I would give it this structure:

accounts:
- name: test1
  email: test1@gmail.com
- name: test2
  email: test2@gmail.com

and read it in a way similar to this:

import yaml

with open("config.yml", 'r') as ymlfile:
    config = yaml.safe_load(ymlfile)
accounts = config['accounts']
for account in accounts:
    print()
    print(account['name'])
    print(account['email'])


UPDATE

Maybe you need to do something like this?

 # ... for account in accounts: response = client.create_account( AccountName = account['name'], Email = account['email']) 

(what an unpythonic naming convention boto3 has!)

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