Saving a url to AWS parameter store with aws-cli
Asked Answered
S

5

15

Alright, so I'm trying to programmatically store my Serverless generated API endpoint in parameter store for another project to ingest.

Just for an example, I'm going to try to store google.com.

aws ssm put-parameter --name /dev/someStore --value https://google.com --type String

This fails, understandably so.

Error parsing parameter '--value': Unable to retrieve https://google.com: received non 200 status code of 301

However, if I wrap the URL in quotes...

aws ssm put-parameter --name /dev/someStore --value "https://google.com" --type String

It still fails with the same error. Is there any way to stop the cli from trying to evaluate the URL and just save the goddamn string?

Sardella answered 31/10, 2018 at 22:49 Comment(2)
This. This is why we can't have nice things :)Pedant
The discussion about this 'feature' on github ticketCongregate
L
20

This is happening because of a questionable behavior by awscli v1. When it sees a URL, it invokes an HTTP GET for a result. This does not happen in awscli v2.

You can work around this behavior as follows:

aws ssm put-parameter --cli-input-json '{
  "Name": "/dev/someStore",
  "Value": "https://google.com",
  "Type": "String"
}'

Or you can store the JSON in a file named params.json and invoke:

aws ssm put-parameter --cli-input-json file://params.json

The underlying issue was reported at aws/aws-cli/issues/2507.

Locklear answered 31/10, 2018 at 23:5 Comment(1)
Oh man, you legend. I was tearing my hair out. Thanks for your help.Sardella
P
13

By default AWS CLI follows any string parameters that start with https:// or http://. These URLs are fetched, and the downloaded content is used as the parameter instead of URL.

To make CLI not treat strings prefixed with https:// or http:// any differently than normal string parameters run:

aws configure set cli_follow_urlparam false

cli_follow_urlparam controls whether or not the CLI will attempt to follow URL links in parameters that start with either prefix https:// or http://.

See https://docs.aws.amazon.com/cli/latest/topic/config-vars.html

Problem:

aws ssm put-parameter --name /config/application/some-url --value http://google.com --type String --region eu-central-1 --overwrite

Error parsing parameter '--value': Unable to retrieve http://google.com: received non 200 status code of 301

Solution:

aws configure set cli_follow_urlparam false
aws ssm put-parameter --name /config/application/some-url --value http://google.com --type String --region eu-central-1 --overwrite

{
    "Version": 1
}
Pathfinder answered 26/11, 2019 at 10:13 Comment(1)
amazing answer. Helped me enormouslyAvignon
A
5

The GitHub discussion on this topic, linked by @jarmod, also had another solution for this. I'll replicate it here for others to avoid scanning through the whole thread.

Add the following to your ~/.aws/config along with any other settings present.

[default]
cli_follow_urlparam = false

P.S. Seems that it is also mentioned in the AWS documentation under "Loading Parameters from a File" section.

Annitaanniversary answered 27/4, 2019 at 13:10 Comment(3)
Do we have to repeat this line with every profile in the file?!?Congregate
@JohnMee Haven't tried it myself, but it is one of the the options mentioned in global settings for the config file. I'd assume that'll set the parameter globally for all profiles.Annitaanniversary
@JohnMee Yes, you have to set it in every profile (just tried it, you can't set it globally)Liquorice
C
1

Another option to make this work is to not include the https protocol in the value and just the domain name or the path. After retrieval add the protocol appropriate. some times we wanted to use https or http or even ssh. Take git url for example. Multiple protocols for accessing the resource with appropriate ports where the path is the required value

Clayson answered 1/11, 2018 at 17:3 Comment(2)
That's actually a good idea. It might work well for my circumstances cause it's tricky to build the json dynamically. Thanks for contributing!Sardella
Sure try it out and let me know. You can use jq while retrieval to filter the exact valueClayson
A
1

To complement @jarmod answers, here is an example showing how one can deal with Overwrite file, url in bash variable and making the json multi-line string.

URL='https://www.some.url.com'

json_params='{' 
json_params+='"Name": "/param/path",'
json_params+='"Value": "'${URL}'",'
json_params+='"Type": "String",'
json_params+='"Overwrite": true'
json_params+='}'


aws ssm put-parameter \
     --cli-input-json "${json_params}"
Altorelievo answered 28/3, 2020 at 9:18 Comment(1)
best answer hereWaaf

© 2022 - 2024 — McMap. All rights reserved.