How to update launch configurations using AWS CLI?

Hi,

I’m trying to update my launch configurations by using the aws autoscaling describe-launch-configurations command, manipulating the fields with jq in bash, and then creating a new launch configuration with aws autoscaling create-launch-configurations --cli-input-json.

So far, it’s actually worked pretty well except for the userData field which needs to be decoded from base64 to utf8 but once I do so, it has the shebang on a new line and creates a mess in the JSON. Has anyone successfully used the --cli-input-json command with written userData?

Please help!

PS If anyone has a better way to update launch configurations via the CLI, let me know!


Related discussions:

That sounds so very painful… Can you import the resources into terraform and manipulate there?

Do you have a sample command?

Most of my launch configurations are inside a cloudformation stack. When I need to update the launch config, I update the cloudformation stack and it handles the messy stuff inside.

For example, if I have a new AMI for a deployment, I update the AMI parameter on the CF stack and it creates the new LC and updates the active LC on the scaling group.

I’m not currently using terraform yet :confused: looking into it though!

Hmm that’s normally what i would do but i’m trying to automate the process a little more so that i dont have to manually update the actual cloudformation template. Plus, wouldnt that involve tearing down the stack and then standing it back up? Maybe 10-15 mins?

so that i dont have to manually update the actual cloudformation template

No, use parameters.

Plus, wouldnt that involve tearing down the stack and then standing it back up? Maybe 10-15 mins?

No, it’s a stack update. Takes like 10 seconds to update a launch conf with a new AMI.

If you manipulate the json in the shell, you can do further manipulations there.

sed ‘/^\s*$/d’

strips all empty lines lines from the file. You may need to adjust a bit depending on the actual json.

Thats what i figured but doesnt the shebang have to be on it’s own line for a bash script?

I’ll try it! Thanks :slight_smile:

Not sure what you mean, but

“UserData”: “#!/bin/bash
echo hello!”,
“Nextparameter”: “…”

would place the hashbang on a separate line in the script that is actually executed on the instance.

Is this true?? I was under the impression that you can’t have new lines in a json string for some reason. I’ll try it! Thank you!

Can you change the user data to download/clone a script from a repo and run that? Then you can easily iterate that script and keep the user data script static.

i use python for launch configurations … though i generate them as needed.

python can parse the json to data structures you can easily manipulate then generate json, again, from new data.

You’re very much doing this the hard way currently. Terraform/CloudFormation are the correct answers

That’s actually what the current user data does. The problem is that it looks like this

#!/bin/bash
/usr/bin/curl https://github.com/…

I’m trying to run it through jq so basically

echo ${launchConfiguration} | jq -r --arg user_data \
${user_data}' .UserData = $user_data'

and the launchConfiguration might look something like these examples

The problem is coming from jq with the below error

jq: error: syntax error, unexpected ‘/’, expecting $end (Unix shell
quoting issues?) at , line 1:
/usr/bin/curl
jq: 1 compile error

So below is the user data script that I’d be trying to load in

#!/bin/bash
/usr/bin/curl https://github.com/...

I’m trying to run it through jq so basically

echo ${launchConfiguration} | jq -r --arg user_data \
${user_data}' .UserData = $user_data'

and the launchConfiguration might look something like these examples

The problem is coming from jq with the below error

jq: error: syntax error, unexpected ‘/’, expecting $end (Unix shell
quoting issues?) at , line 1:

user-data can be string value or file path value starting with “file://”. i’d suggest decoding redix format straight to file buffer and pass that buffer file as user-data instead of this decode-then-re-encode base64 to plain text back into base64 nonsense.

I wasn’t sure if you can pass the file path in when using the --cli-input-flag. I thought everything had to be “raw” json data? There’s not much docs about it :confused:

you just have to make sure userData is missing in cli-input-json data. cli-input-json doesn’t generally clobber other command arguments, but i would be lying if that’s the case for all (emr is notriously known for being the black sheep of awscli).