1

I am trying to construct a role in AWS where I am trying to have list of resources.

Below is an example

shared ={
    "mts":{
        "account_id":"11111",
        "workbench":"aaaaa",
        "prefix":"rad600-ars-sil,rad600-srr-sil-stage1,rad600-srr-sil-stage2"
    },
    "tsf":{
        "account_id":"22222",
        "workbench":"bbbbb",
        "prefix":"yyyy"

    }
}

I am trying to construct a list with

role_arn=[]
for key in shared:
    
    role_arn.append(f"arn:aws:iam::'{shared[key]['account_id']}':role/'{shared[key]['workbench']}'_role") 

here is my output:

["arn:aws:iam::'11111':role/'aaaaa'_role", "arn:aws:iam::'22222':role/'bbbbb'_role"]

I want the '' to be removed from the list while appending into the list itself.

desired output:

["arn:aws:iam::11111:role/aaaaa_role", "arn:aws:iam::22222:role/bbbbb_role"] 

I am trying my hands on python. IS there a way to achieve it?

1
  • 1
    It is YOU who are inserting those quotes. It's right there in your string: ...iam::'{shared[key.... If you don't want the single quotes, then remove them before and after the braces. Commented Jan 10, 2023 at 3:50

2 Answers 2

2
role_arn=[]
for key in shared:
    
    role_arn.append(f"arn:aws:iam::{shared[key]['account_id']}:role/{shared[key]['workbench']}_role") 

You don't need those ' unless you want them. You can remove it and the string formatting would still work as expected and get rid of the '.

Most likely your concern was coming from not knowing the Lietral format strings. You don't need to use '' before every variable. {} takes care of it.

Sign up to request clarification or add additional context in comments.

Comments

1

This is my take on it

This uses dictionary comprehension to iterate over the shared dictionary instead of a for loop

shared ={
    "mts":{
        "account_id":"11111",
        "workbench":"aaaaa",
        "prefix":"rad600-ars-sil,rad600-srr-sil-stage1,rad600-srr-sil-stage2"
    },
    "tsf":{
        "account_id":"22222",
        "workbench":"bbbbb",
        "prefix":"yyyy"

    }
}

role_arn = [f"arn:aws:iam::{data['account_id']}:role/{data['workbench']}_role" for key, data in shared.items()]

print(role_arn)

Which gives the output

['arn:aws:iam::11111:role/aaaaa_role', 'arn:aws:iam::22222:role/bbbbb_role']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.