bashaws-cliaws-ssm

ValidationException while executing bash


Here i am trying to retrieve latest ami id for k8s worker node.

i have written a bash script for the same, every time i run this script it return errors.

Please set VERSION=v1.23 before running script

note: when we run the command individually it works.

#!/bin/bash

VERSION=$1

SOURCE_AMI_ID=$(aws ssm get-parameter --name /aws/service/eks/optimized-ami/${VERSION}/amazon-linux-2/recommended/image_id --region us-west-2 --query "Parameter.Value" --output text)
SOURCE_AMI_NAME=$(aws ec2 describe-images --image-ids ${SOURCE_AMI_ID} | jq -r '.Images[0].Name')
AMI_NAME=$(echo "${SOURCE_AMI_NAME}" | sed 's/amazon/kube-infra/g')
jq -n \
        --arg source_ami_id "${SOURCE_AMI_ID}" \
        --arg source_ami_name "${SOURCE_AMI_NAME}" \
        --arg cp_ami_name "${CP_AMI_NAME}" \
        '{source_ami_id: $source_ami_id, source_ami_name: $source_ami_name, ami_name: $ami_name}'

error:

An error occurred (ValidationException) when calling the GetParameter operation: Parameter name: can't be prefixed with "ssm" (case-insensitive). If formed as a path, it can consist of sub-paths divided by slash symbol; each sub-path can be formed as a mix of letters, numbers and the following 3 symbols .-_

how i am running the above script

% ./build-versions.sh

An error occurred (ValidationException) when calling the GetParameter operation: Parameter name: can't be prefixed with "ssm" (case-insensitive). If formed as a path, it can consist of sub-paths divided by slash symbol; each sub-path can be formed as a mix of letters, numbers and the following 3 symbols .-_


Solution

  • Note that

    aws ssm get-parameter \
      --name /aws/service/eks/optimized-ami/v1.23/amazon-linux-2/recommended/image_id \
      --region us-west-2
    

    Gives me error (ParameterNotFound). I need to use "1.23" instead of "v1.23" to get a valid response, namely

    aws ssm get-parameter \
      --name /aws/service/eks/optimized-ami/1.23/amazon-linux-2/recommended/image_id \
      --region us-west-2
    

    Below I assume you will also use "1.23".


    Setting VERSION=1.23 before running the script is not useful in this case. In your script, VERSION=$1, which means it sets VERSION to the first argument of the script. Hence, you should run

    ./build-versions.sh 1.23
    

    Instead of

    VERSION=1.23
    ./build-versions.sh
    

    If you really want to set it outside of your script, then you need to set it with export VERSION=1.23, and remove the VERSION=$1 line.