Skip to content

Updating a working code for secrets manager for my needs

0

My main goal is to use a secret_name from a CSV file. The working code uses the secret key as the name which I dont want. I modified the below code and getting a key error for secret_name on line 9:

import csv import boto3

Load CSV file

with open("secrets.csv", "r") as csvfile: reader = csv.DictReader(csvfile)

for row in reader:
    secret_name = row["secret_name"] # Use the nameofsecret from the CSV as secret name
    secret_key = row["secret_key"]
    secret_value = row["secret_value"]
    secret_description = row["description"]	

    # Create the secret in Secrets Manager
    client = boto3.client("secretsmanager")
    response = client.create_secret(
        Name=secret_name,
        Description=secret_description,
        SecretString=secret_value
    )
    print(f"Secret '{secret_name}' created with ARN: {response['ARN']}")

asked 2 years ago243 views

1 Answer
1
Accepted Answer

Can you provide the specific error message?

Also, please ensure that your name adheres to these requirements.

The secret name can contain ASCII letters, numbers, and the following characters: /_+=.@-

Do not end your secret name with a hyphen followed by six characters. If you do so, you risk confusion and unexpected results when searching for a secret by partial ARN. Secrets Manager automatically adds a hyphen and six random characters after the secret name at the end of the ARN.

Testing your code, the following worked for me:

secrets.csv

secret_name,secret_key,secret_value,description
name,key,value,description

python code (file named tmp)

import csv
import boto3

with open("secrets.csv", "r") as csvfile:
	reader = csv.DictReader(csvfile)

	for row in reader:
    		secret_name = row["secret_name"] # Use the nameofsecret from the CSV as secret name
    		secret_key = row["secret_key"]
    		secret_value = row["secret_value"]
    		secret_description = row["description"]

    		# Create the secret in Secrets Manager
    		client = boto3.client("secretsmanager")
    		response = client.create_secret(
        		Name=secret_name,
        		Description=secret_description,
        		SecretString=secret_value
    		)
    		print(f"Secret '{secret_name}' created with ARN: {response['ARN']}")

run output

python3 tmp
Secret 'name' created with ARN: arn:aws:secretsmanager:us-east-1:<redacted acct>:secret:name-NwWxkC
AWS
EXPERT

answered 2 years ago

EXPERT

reviewed 2 years ago

  • Hi, I am getting this error:

    [cloudshell-user@ip-10-123-123-123 ~]$ python secretstest.py Traceback (most recent call last): File "/home/cloudshell-user/secretstest.py", line 8, in <module> secret_name = row["secret_name"] # Use the nameofsecret from the CSV as secret name KeyError: 'secret_name'

  • Can you share the header row from your CSV file? It seems like there is not a column labeled secret_name based on that error message.

  • secret_name,secret_key,secret_value,description are the headers

  • Since I can see that you were running this in CloudShell, I retested it there, again with success.

    At this point, I would suggest that you test the version of the code that I have posted above with the secrets.csv which I have also posted since those work both on my laptop and in CloudShell.

    I am sorry that I can not definitively tell you what is wrong with your code or CSV as they stand. My hunch would be perhaps spelling error or formatting issue in the CSV.

  • Thank you. I managed to get it going. It ended up being some syntax issues on the csv. Commas and "&" on the names.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.