Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dotenv output format for aws ssm get-parameters-by-path #8599

Open
2 tasks
stuft2 opened this issue Mar 25, 2024 · 1 comment
Open
2 tasks

Add dotenv output format for aws ssm get-parameters-by-path #8599

stuft2 opened this issue Mar 25, 2024 · 1 comment
Assignees
Labels
feature-request A feature should be added or improved. p3 This is a minor priority issue response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. ssm

Comments

@stuft2
Copy link

stuft2 commented Mar 25, 2024

Describe the feature

Add a --dotenv flag (or maybe an --output dotenv format option) to format the output of the get-parameters-by-path command in dotenv format.

Use Case

Environment variables are often stored in the SSM Parameter Store. Fetching them out of the store and formatting them in dotenv format is common but not trivial and requires either many steps or a very complicated chain of terminal commands. The dotenv format is widely adopted by tools and mainstream IDEs for configuring environment variables for developers.

Proposed Solution

No response

Other Information

Use awsparams with the --dotenv flag

awsparams ls /$APP/$ENV --with-decryption --dotenv

OR with linux native tools and the aws cli

export APP=<my-app-name>
export ENV=<my-environment-name>

aws ssm get-parameters-by-path --no-cli-pager --with-decryption --output text --query 'Parameters[].[Name,Value]' --path /$APP/$ENV | sed -e "s/\/$(echo $APP)\/$(echo $ENV)\/\(\w*\)/\U\1/g" -e "s/\t/=/g" > .env

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

CLI version used

aws-cli/2.12.6 Python/3.11.4 Darwin/23.1.0 exe/x86_64 prompt/off

Environment details (OS name and version, etc.)

macOS 14.1.2

@stuft2 stuft2 added feature-request A feature should be added or improved. needs-triage This issue or PR still needs to be triaged. labels Mar 25, 2024
@tim-finnigan tim-finnigan self-assigned this May 10, 2024
@tim-finnigan
Copy link
Contributor

Thanks for the feature request regarding the get-parameters-by-path command.

Here is related documentation on AWS CLI command line options. The request to add a --output dotenv would likely not be considered because --output is a global parameter used for general formatting of outputs (json, table, text) and I don't think this use case is common or general enough to warrant a new output format.

The SSM service team owns the underlying GetParametersByPath API that is used here, so we could forward a feature request to them to provide the ability to return parameters in dotenv format.

I'll also share a utility script that I think accomplishes what you're trying to do:

#!/bin/bash

# Set the parameter path prefix
PARAMETER_PATH="/"

# Fetch parameters from the Parameter Store
PARAMETERS=$(aws ssm get-parameters-by-path --path "$PARAMETER_PATH" --recursive --with-decryption --query 'Parameters[*].[Name,Value]' --output text)

DOTENV_LINES=$(echo "$PARAMETERS" | awk -F $'\t' '{ printf "%s=%s\n", $1, $2 }')

# Write the dotenv lines to a file
echo "$DOTENV_LINES" > .env

echo "Environment variables exported to .env"

Also in a Boto3 script:

import boto3

# Set up AWS credentials/configurations
session = boto3.Session()
ssm_client = session.client('ssm')

# Define the parameter path prefix
parameter_path = '/'

# Fetch parameters from the Parameter Store
response = ssm_client.get_parameters_by_path(Path=parameter_path, Recursive=True, WithDecryption=True)

# Format parameters in dotenv format
dotenv_lines = []
for parameter in response['Parameters']:
    key = parameter['Name'].split('/')[-1]
    value = parameter['Value']
    dotenv_lines.append(f"{key}={value}")

# Write dotenv lines to a file
dotenv_file = '.env'
with open(dotenv_file, 'w') as f:
    f.write('\n'.join(dotenv_lines))

print(f"Environment variables exported to {dotenv_file}")

Hope that helps — please let us know if you'd also like us to reach out to the SSM team regarding this feature request.

@tim-finnigan tim-finnigan added ssm response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. p3 This is a minor priority issue and removed needs-triage This issue or PR still needs to be triaged. labels May 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request A feature should be added or improved. p3 This is a minor priority issue response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. ssm
Projects
None yet
Development

No branches or pull requests

2 participants