1

I have a bash script running with each deployment to elastic beanstalk (inside the hook folder). I am trying to figure out how can I remove the path from the variable's names. All the environment variables have the same path /project/development/ and I wanna remove the /project/development/ before moving the variables to the env file. This is the code I am currently using to fetch the variables.

#!/usr/bin/env bash

# https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/platforms-linux-extend.html

echo ".platform/confighooks/postdeploy/map_parameters_to_env_vars.sh executing"
echo "Running script to fetch parameter store values and add them to /opt/elasticbeanstalk/deployment/env file."

# We need to check the Elastic Beanstalk environment properties to find out
# what the path is to use for the parameter store values to fetch.
# Only the parameters under that path will be fetched, allowing each Beanstalk
# config to specify a different path if desired.
readarray eb_env_vars < /opt/elasticbeanstalk/deployment/env

for i in ${eb_env_vars[@]}
do
  if [[ $i == *"parameter_store_path"* ]]; then
    parameter_store_path=$(echo $i | grep -Po "([^\=]*$)")
  fi
done

if [ -z ${parameter_store_path+x} ]; then
  echo "Error: parameter_store_path is unset on the Elastic Beanstalk environment properties.";
  echo "You must add a property named parameter_store_path with the path prefix to your SSM parameters.";
else
  echo "Success: parameter_store_path is set to '$parameter_store_path'";

  TOKEN=`curl -X PUT http://169.254.169.254/latest/api/token -H "X-aws-ec2-metadata-token-ttl-seconds:21600"`
  AWS_DEFAULT_REGION=`curl -H "X-aws-ec2-metadata-token:$TOKEN" -v http://169.254.169.254/latest/meta-data/placement/region`

  export AWS_DEFAULT_REGION

  #Create a copy of the environment variable file.
  cp /opt/elasticbeanstalk/deployment/env /opt/elasticbeanstalk/deployment/custom_env_var

  # Add values to the custom file
  echo "AWS_DEFAULT_REGION=$AWS_DEFAULT_REGION" >> /opt/elasticbeanstalk/deployment/custom_env_var

  jq_actions=$(echo -e ".Parameters | .[] | [.Name, .Value] | \042\(.[0])=\(.[1])\042 | sub(\042${parameter_store_path}/\042; \042\042)")

  aws ssm get-parameters-by-path \
  --path $parameter_store_path \
  --with-decryption \
  --region us-west-1 \
  | jq -r "$jq_actions" >> /opt/elasticbeanstalk/deployment/custom_env_var

  cp /opt/elasticbeanstalk/deployment/custom_env_var /opt/elasticbeanstalk/deployment/env

  #Remove temporary working file.
  rm -f /opt/elasticbeanstalk/deployment/custom_env_var

  #Remove duplicate files upon deployment.
  rm -f /opt/elasticbeanstalk/deployment/*.bak

fi

Is it possible to add some "substring cut" to this line? I just wanna remove this part of the string for each variable retrieved "/project/development/"

jq_actions=$(echo -e ".Parameters | .[] | [.Name, .Value] | \042\(.[0])=\(.[1])\042 | sub(\042${parameter_store_path}/\042; \042\042)")
R0bertinski
  • 517
  • 6
  • 12
  • What do you mean by _removing the path from a variable_? Is your problem just "how to remove a substring from a bash variable" (in your case, the string would be _/project/development/_)? – user1934428 Oct 28 '22 at 06:22
  • Yes i want to remove a substring from the variables , a common substring for all the variables "/project/development/' I can easy do in php ,js or even python, but I have not idea about bash :) Thanks – R0bertinski Oct 28 '22 at 08:37
  • Hi, I can not do like this, the AWS method returns the variables in a Json format, and I need to substract the mentioned substring for each variable received. I do not know how to do in the code I posted above. I think it could be added in here jq_actions=$(echo -e ".Parameters | .[] | [.Name, .Value] | \042\(.[0])=\(.[1])\042 | sub(\042${parameter_store_path}/\042; \042\042)") But I don't know how to do. – R0bertinski Oct 28 '22 at 11:24
  • In your question's title you mention replacement of strings in **environment variables**, and this is what my answer refers to. I suggest that you ask a new question (because editing a question in a way that makes valid answers invalid is discouraged). Also make sure that you write a precise problem definition, ideally with a small, reproducible example, because from your comment, at least I don't understand what you are up to. – user1934428 Oct 28 '22 at 11:28
  • Yes, they are environment variables, who understand how AWS SSM parameter store works, will understand my post. Thanks anyway. – R0bertinski Oct 28 '22 at 12:16

1 Answers1

0

From the bash man-page, section Parameter Expansion :

${parameter/pattern/string}

Pattern substitution. The pattern is expanded to produce a pattern just as in pathname expansion. Parameter is expanded and the longest match of pattern against its value is replaced with string. If pattern begins with /, all matches of pattern are replaced with string. Normally only the first match is replaced. If pattern begins with #, it must match at the beginning of the expanded value of parameter. If pattern begins with %, it must match at the end of the expanded value of parameter.

Therefore:

VAR=foo/bar/project/development/baz
VARX=${VAR/\/project\/development\///} # -> /foo/bar/baz
user1934428
  • 19,864
  • 7
  • 42
  • 87
  • Hi, yes I saw this before here https://devhints.io/bash, but how can I implement this in my script? This is the point. Regards – R0bertinski Oct 28 '22 at 11:03
  • According to your comment, you wanted to know how to do substring replacement. You can do this with each variable in your script which should undergo this replacement. Just use your variable name instead of mine. – user1934428 Oct 28 '22 at 11:10
  • Ok, following your previous suggestion, I have created a new post here https://stackoverflow.com/questions/74244353/remove-substring-from-a-json-retrieved-in-a-bash-script , I hope now the question is a bit more clear. Thanks – R0bertinski Oct 29 '22 at 10:06