Aleksander Gurin
Aleksander Gurin

Reputation: 153

How to create-hosted-configuration-version for aws appconfig

In AWS AppConfig I created application Testing and configuration profile TestingFlags. I want to create hosted configuration version using CLI.

aws appconfig create-hosted-configuration-version --application-id APP_ID --configuration-profile-id PROF_ID --content eyJ0ZXN0IjogeyJlbmFibGVkIjogZmFsc2V9fQ== --content-type "application/json" out

As a result I got the following error:

An error occurred (BadRequestException) when calling the CreateHostedConfigurationVersion operation: Error invoking extension AppConfig Feature Flags Helper: Invalid 'Content' data

P.S. eyJ0ZXN0IjogeyJlbmFibGVkIjogZmFsc2V9fQ== is the base64 decoded string {"test": {"enabled": false}}

Probably content should be structure in some specific way. I read this guide https://docs.aws.amazon.com/appconfig/latest/userguide/appconfig-creating-configuration-and-profile.html#appconfig-creating-feature-flag-configuration-commandline

I will appreciate any help.

Upvotes: 2

Views: 2270

Answers (2)

Supun Sandaruwan
Supun Sandaruwan

Reputation: 2418

The full appConfig deployment process shell file includes the create-hosted-configuration-version step. This is 100% working for me with no issues.

#!/bin/bash
app_config_app_name="my_app"

# Function to read a specific value from the config.yaml file
get_config_value() {
    local key=$1
    grep "^$key:" config.yaml | awk -F': ' '{print $2}'
}

# Read values from config.yaml
config_profile=$(get_config_value "configuration_profile") 
config_path=$(get_config_value "configuration_path") # dev/config.yaml
config_env=${config_path%%/*} #dev

echo "Fetched [configuration_profile:'${config_profile}'], [configuration_path:'${config_path}'], \
[config_env:'${config_env}'] from yaml file."

# Retrieve Application ID
APPLICATION_ID=$(aws appconfig list-applications \
                  --query "Items[?Name=='${app_config_app_name}'].Id" \
                  --output text)

if [[ -z "$APPLICATION_ID" ]]; then
    echo "Application ${app_config_app_name} does not exist. Please create it before proceeding."
    exit 1
fi

echo "AppConfig app:${app_config_app_name}, id:${APPLICATION_ID}"

# Check if Configuration Profile exists
CONFIG_PROFILE_ID=$(aws appconfig list-configuration-profiles \
                    --application-id "$APPLICATION_ID" \
                    --query "Items[?Name=='${config_profile}'].Id" \
                    --output text)

if [[ -z "$CONFIG_PROFILE_ID" ]]; then
  echo "Configuration Profile '${config_profile}' does not exist. Creating it now..."

  CONFIG_PROFILE_ID=$(aws appconfig create-configuration-profile \
  --application-id "$APPLICATION_ID" \
  --name "$config_profile" \
  --location-uri "hosted" \
  --query "Id" --output text)

  if [[ -z "$CONFIG_PROFILE_ID" ]]; then
      echo "Failed to create Configuration Profile."
      exit 1
  fi
  echo "Configuration Profile '${config_profile}' created successfully with ID: $CONFIG_PROFILE_ID."
else
    echo "Configuration Profile '${config_profile}' exists with id:${CONFIG_PROFILE_ID}"
fi

# Retrieve Environment ID
ENVIRONMENT_ID=$(aws appconfig list-environments \
                  --application-id "$APPLICATION_ID" \
                  --query "Items[?Name=='${config_env}'].Id" \
                  --output text)

if [[ -z "$ENVIRONMENT_ID" ]]; then
    echo "Environment '${config_env}' does not exist. Please create it before proceeding."
    exit 1
fi
echo "Environment ${config_env}, found with id:${ENVIRONMENT_ID}"

# Base64 encode the configuration content
BASE64_CONTENT=$(base64 "$config_path") # This step important

OUTPUT_FILE="output_file.yml"

# Create new configuration version
HOSTED_RES=$(aws appconfig create-hosted-configuration-version \
              --application-id "$APPLICATION_ID" \
              --configuration-profile-id "$CONFIG_PROFILE_ID" \
              --content "$BASE64_CONTENT" \
              --content-type "application/x-yaml" \
              "$OUTPUT_FILE")

if [[ -z "$HOSTED_RES" ]]; then
    echo "Failed to create a new configuration version."
    exit 1
fi
echo "New configuration version response:${HOSTED_RES}"

# Extract the VersionNumber from the hosted response
NEW_DEPLOYED_VERSION=$(echo "$HOSTED_RES" | jq -r '.VersionNumber')
echo "New configuration version:${NEW_DEPLOYED_VERSION}"

# Retrieve the Deployment Strategy ID for "Quick"
QUICK_DEPLOYMENT_STRATEGY_ID=$(aws appconfig list-deployment-strategies \
                                --query "Items[?Name=='Quick'].Id" \
                                --output text)

# Check if the strategy ID was found
if [ -z "$QUICK_DEPLOYMENT_STRATEGY_ID" ]; then
    echo "Error: Deployment strategy with Name 'Quick' not found."
    exit 1
fi
echo "Quick Deployment Strategy ID: $QUICK_DEPLOYMENT_STRATEGY_ID"

# Deploy the new configuration
DEPLOYMENT_NUMBER=$(aws appconfig start-deployment \
                --application-id "$APPLICATION_ID" \
                --environment-id "$ENVIRONMENT_ID" \
                --configuration-profile-id "$CONFIG_PROFILE_ID" \
                --configuration-version "$NEW_DEPLOYED_VERSION" \
                --deployment-strategy-id "$QUICK_DEPLOYMENT_STRATEGY_ID" \
                --query "DeploymentNumber" \
                --output text)

if [[ -z "$DEPLOYMENT_NUMBER" ]]; then
    echo "Failed to start deployment."
    exit 1
fi
echo "Deployment started with Deployment Number: $DEPLOYMENT_NUMBER."

# Monitor deployment
while true; do
    DEPLOYMENT_INFO=$(aws appconfig get-deployment \
                       --application-id "$APPLICATION_ID" \
                       --environment-id "$ENVIRONMENT_ID" \
                       --deployment-number "$DEPLOYMENT_NUMBER" \
                       --query "{State: State, PercentageComplete: PercentageComplete}" \
                       --output json)

    # Extract values
    STATUS=$(echo "$DEPLOYMENT_INFO" | jq -r '.State')
    COMPLETED_PERCENTAGE=$(echo "$DEPLOYMENT_INFO" | jq -r '.PercentageComplete')

    echo "Deployment Status: ${STATUS}, Completed:${COMPLETED_PERCENTAGE}%"
    if [[ "$STATUS" == "COMPLETE" ]]; then
        echo "Deployment completed for version:${NEW_DEPLOYED_VERSION} under Deployment Number:${DEPLOYMENT_NUMBER} successfully!"
        break
    elif [[ "$STATUS" == "FAIL" ]]; then
        echo "Deployment failed."
        exit 1
    fi
    sleep 2
done

Upvotes: 0

Aleksander Gurin
Aleksander Gurin

Reputation: 153

Documentation example was incorrect. Correct form is:

aws appconfig create-hosted-configuration-version --application-id APP_ID --configuration-profile-id PROF_ID --cont
ent BASE_64_CONTENT --content-type application/json output.json

Where BASE_64_CONTENT is base64 encoded data, in a form:

{
  "flags": {
    "flagkey": {
      "name": "flagkey"
    }
  },
  "values": {
    "flagkey": {
      "enabled": false
    }
  },
  "version": "1"
}

Upvotes: 4

Related Questions