aws-amplify / amplify-cli

The AWS Amplify CLI is a toolchain for simplifying serverless web and mobile development.
Apache License 2.0
2.82k stars 820 forks source link

Amplify Console Build Failure - auth resource was imported from 'undefined' region #6582

Closed edmond-io closed 3 years ago

edmond-io commented 3 years ago

Describe the bug Unable to build through the amplify console when the auth resource is imported from an existing Congito User Pool and Identity Pool. It works perfectly fine when running in local with amplify push.

The error message is:

Error: The previously imported 'authappafe49611' auth resource was imported from 'undefined' region and the new environment's region is: 'ap-northeast-1', import a new resource to finish the creation of the new environment.

The auth configuration during amplify import set up:

> amplify import auth
Using service: Cognito, provided by: awscloudformation
✔ What type of auth resource do you want to import? · Cognito User Pool and Identity Pool
✔ Select the User Pool you want to import: · ap-northeast-1_xxxxx
✔ Only one Web app client found: 'web-client' was automatically selected.
✔ Only one Native app client found: 'native-app' was automatically selected.
✔ Federated identity providers are not configured, no OAuth configuration needed.
✔ Only one Identity Pool resource found: 'App Prod Identity Pool' (ap-northeast-1:655xxxxxx) was automatically selected.

✅ Cognito User Pool 'app-prod-user-pool' and Identity Pool 'App Prod Identity Pool' was successfully imported.

Amplify CLI Version 4.42.0

Log output

2021-02-10T15:45:21.880Z [INFO]: # Retrieved environment cache
2021-02-10T15:45:21.881Z [INFO]: # Ensuring NPM package '@aws-amplify/cli' is version: 'latest'
2021-02-10T15:45:29.219Z [INFO]: # Patching NPM package '@aws-amplify/cli' from 4.29.4 to 4.42.0...
2021-02-10T15:46:24.184Z [INFO]: # Done patching NPM package '@aws-amplify/cli'
2021-02-10T15:46:30.895Z [INFO]: ## Starting Backend Build
                                 # Starting phase: preBuild
                                 # Executing command: chmod u+x ./scripts/amplify-push.sh
2021-02-10T15:46:30.897Z [INFO]: # Completed phase: preBuild
                                 # Starting phase: build
                                 # Executing command: ./scripts/amplify-push.sh
2021-02-10T15:46:30.899Z [INFO]: # Initial value of ENV: , AWS_BRANCH: master, USER_BRANCH: prod, POSITIONAL
2021-02-10T15:46:30.902Z [INFO]: # Getting Amplify CLI Cloud-Formation stack info from environment cache
2021-02-10T15:46:30.949Z [INFO]: # Start initializing Amplify environment: prod
2021-02-10T15:46:30.949Z [INFO]: # Initializing new Amplify environment: prod (amplify init)
2021-02-10T15:46:33.397Z [INFO]: Note: It is recommended to run this command from the root of your app directory
2021-02-10T15:46:39.659Z [WARNING]: - Initializing your environment: prod
2021-02-10T15:46:40.084Z [WARNING]: ✔ Initialized provider successfully.
2021-02-10T15:46:40.135Z [WARNING]: ✖ There was an error initializing your environment.
2021-02-10T15:46:40.146Z [INFO]: init failed
2021-02-10T15:46:40.147Z [INFO]: Error: The previously imported 'authappafe49611' auth resource was imported from 'undefined' region and the new environment's region is: 'ap-northeast-1', import a new resource to finish the creation of the new environment.
                                     at headlessImport (/root/.nvm/versions/node/v12.19.0/lib/node_modules/@aws-amplify/cli/node_modules/amplify-category-auth/src/provider-utils/awscloudformation/import/index.ts:1135:11)
                                     at exports.importedAuthEnvInit (/root/.nvm/versions/node/v12.19.0/lib/node_modules/@aws-amplify/cli/node_modules/amplify-category-auth/src/provider-utils/awscloudformation/import/index.ts:903:18)
                                     at processTicksAndRejections (internal/process/task_queues.js:97:5)
                                     at updateConfigOnEnvInit (/root/.nvm/versions/node/v12.19.0/lib/node_modules/@aws-amplify/cli/node_modules/amplify-category-auth/src/provider-utils/awscloudformation/index.js:51:72)
                                     at /root/.nvm/versions/node/v12.19.0/lib/node_modules/@aws-amplify/cli/node_modules/amplify-category-auth/src/index.js:300:22
2021-02-10T15:46:40.163Z [ERROR]: !!! Build failed
2021-02-10T15:46:40.163Z [ERROR]: !!! Non-Zero Exit Code detected
2021-02-10T15:46:40.163Z [INFO]: # Starting environment caching...
2021-02-10T15:46:40.164Z [INFO]: # Environment caching completed
Terminating logging...
SwaySway commented 3 years ago

@edmond-io Few questions here

edmond-io commented 3 years ago

@SwaySway

$ amplify init
Note: It is recommended to run this command from the root of your app directory
? Do you want to use an existing environment? Yes
? Choose the environment you would like to use: prod
? Choose your default editor: IntelliJ IDEA
Using default provider  awscloudformation

For more information on AWS Profiles, see:
https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-profiles.html

? Do you want to use an AWS profile? Yes
? Please choose the profile you want to use default
✔ Initialized provider successfully.
The previously imported 'stockappxxxxx' auth resource was imported from 'undefined' region and the new environment's region is: 'ap-northeast-1', import a new resource to finish the creation of the new environment.
✔ Select the User Pool you want to import: · ap-northeast-1_49xxxxxx
✔ Only one Web app client found: 'web-client' was automatically selected.
✔ Only one Native app client found: 'native-app' was automatically selected.
✔ Federated identity providers are not configured, no OAuth configuration needed.
Initialized your environment successfully.

Your project has been successfully initialized and connected to the cloud!

Some next steps:
"amplify status" will show you what you've added already and if it's locally configured or deployed
"amplify add <category>" will allow you to add features like user login or a backend API
"amplify push" will build all your local backend resources and provision it in the cloud
"amplify console" to open the Amplify Console and view your project status
"amplify publish" will build all your local backend and frontend resources (if you have hosting category added) and provision it in the cloud

Pro tip:
Try "amplify add api" to create a backend API and then "amplify publish" to deploy everything

amplify/backend/backend-config.json

{
"function": "...",
"hosting":"...",
"auth": {
    "stockappxxxxx": {
      "service": "Cognito",
      "serviceType": "imported",
      "providerPlugin": "awscloudformation",
      "dependsOn": [],
      "customAuth": false
    }
  },
"api":"..."
}
SwaySway commented 3 years ago

@edmond-io Are you using your own custom amplifyPush script? If so could you provide the script you are using.

edmond-io commented 3 years ago

@SwaySway This is the custom amplifyPush script.

#!/usr/bin/env bash
set -e
IFS='|'

help_output () {
    echo "usage: amplify-push <--environment|-e <name>> <--simple|-s>"
    echo "  --environment  The name of the Amplify environment to use"
    echo "  --simple  Optional simple flag auto-includes stack info from env cache"
    exit 1
}

init_env () {
    ENV=$1
    AMPLIFY=$2
    PROVIDERS=$3
    CODEGEN=$4
    AWSCONFIG=$5
    CATEGORIES=$6

    echo "# Start initializing Amplify environment: ${ENV}"
    if [[ -z ${STACKINFO} ]];
    then
        echo "# Initializing new Amplify environment: ${ENV} (amplify init)"
        [[ -z ${CATEGORIES} ]] && amplify init --amplify ${AMPLIFY} --providers ${PROVIDERS} --codegen ${CODEGEN} --yes || amplify init --amplify ${AMPLIFY} --providers ${PROVIDERS} --codegen ${CODEGEN} --categories ${CATEGORIES} --yes
        echo "# Environment ${ENV} details:"
        amplify env get --name ${ENV}
    else
        echo "STACKINFO="${STACKINFO}
        echo "# Importing Amplify environment: ${ENV} (amplify env import)"
        amplify env import --name ${ENV} --config "${STACKINFO}" --awsInfo ${AWSCONFIG} --yes;
        echo "# Initializing existing Amplify environment: ${ENV} (amplify init)"
        [[ -z ${CATEGORIES} ]] && amplify init --amplify ${AMPLIFY} --providers ${PROVIDERS} --codegen ${CODEGEN} --yes || amplify init --amplify ${AMPLIFY} --providers ${PROVIDERS} --codegen ${CODEGEN} --categories ${CATEGORIES} --yes
        echo "# Environment ${ENV} details:"
        amplify env get --name ${ENV}
    fi
    echo "# Done initializing Amplify environment: ${ENV}"
}

ENV=""
IS_SIMPLE=false
POSITIONAL=()

while [[ $# -gt 0 ]]
    do
    key="$1"
    case ${key} in
        -e|--environment)
        ENV=$2
        shift
        ;;
        -r|--region)
        REGION=$2
        shift
        ;;
        -s|--simple)
        IS_SIMPLE=true
        shift
        ;;
        *)
        POSITIONAL+=("$1")
        shift
        ;;
    esac
done
echo "# Initial value of ENV: ${ENV}, AWS_BRANCH: ${AWS_BRANCH}, USER_BRANCH: ${USER_BRANCH}, POSITIONAL ${POSITIONAL[@]}"
set -- "${POSITIONAL[@]}"

# if no provided environment name, use default env variable, then user override
if [[ ${ENV} = "" ]];
then
    ENV=${AWS_BRANCH}
fi

if [[ ${USER_BRANCH} != "" ]];
then
    ENV=${USER_BRANCH}
fi

# strip slashes, limit to 10 chars
ENV=$(echo ${ENV} | sed 's;\\;;g' | sed 's;\/;;g' | cut -c -10)

# Check valid environment name
if [[ -z ${ENV} || "${ENV}" =~ [^a-zA-Z0-9\-]+ ]] ; then help_output ; fi

AWSCONFIG="{\
\"configLevel\":\"project\",\
\"useProfile\":true,\
\"profileName\":\"default\",\
\"AmplifyAppId\":\"${AWS_APP_ID}\"\
}"
AMPLIFY="{\
\"envName\":\"${ENV}\",\
\"appId\":\"${AWS_APP_ID}\"\
}"
PROVIDERS="{\
\"awscloudformation\":${AWSCONFIG}\
}"
CODEGEN="{\
\"generateCode\":false,\
\"generateDocs\":false\
}"
CATEGORIES=""

if [[ -z ${AMPLIFY_FACEBOOK_CLIENT_ID} && -z ${AMPLIFY_GOOGLE_CLIENT_ID} && -z ${AMPLIFY_AMAZON_CLIENT_ID} ]]; then
    AUTHCONFIG="{\
    \"userPoolId\":\"${AMPLIFY_USERPOOL_ID}\",\
    \"webClientId\":\"${AMPLIFY_WEBCLIENT_ID}\",\
    \"nativeClientId\":\"${AMPLIFY_NATIVECLIENT_ID}\",\
    \"identityPoolId\":\"${AMPLIFY_IDENTITYPOOL_ID}\"\
    }"
    CATEGORIES="{\
    \"auth\":$AUTHCONFIG\
    }"
else
    AUTHCONFIG="{\
    \"facebookAppIdUserPool\":\"${AMPLIFY_FACEBOOK_CLIENT_ID}\",\
    \"facebookAppSecretUserPool\":\"${AMPLIFY_FACEBOOK_CLIENT_SECRET}\",\
    \"googleAppIdUserPool\":\"${AMPLIFY_GOOGLE_CLIENT_ID}\",\
    \"googleAppSecretUserPool\":\"${AMPLIFY_GOOGLE_CLIENT_SECRET}\",\
    \"amazonAppIdUserPool\":\"${AMPLIFY_AMAZON_CLIENT_ID}\",\
    \"amazonAppSecretUserPool\":\"${AMPLIFY_AMAZON_CLIENT_SECRET}\"\
    }"
    CATEGORIES="{\
    \"auth\":$AUTHCONFIG\
    }"
fi
# Handle old or new config file based on simple flag
if [[ ${IS_SIMPLE} ]];
then
    echo "# Getting Amplify CLI Cloud-Formation stack info from environment cache"
    export STACKINFO="$(envCache --get stackInfo)"
    init_env ${ENV} ${AMPLIFY} ${PROVIDERS} ${CODEGEN} ${AWSCONFIG} ${CATEGORIES}
    echo "# Store Amplify CLI Cloud-Formation stack info in environment cache"
    STACKINFO="$(amplify env get --json --name ${ENV})"
    envCache --set stackInfo ${STACKINFO}
    echo "STACKINFO="${STACKINFO}
else
    # old config file, above steps performed outside of this script
    init_env ${ENV} ${AMPLIFY} ${PROVIDERS} ${CODEGEN} ${AWSCONFIG} ${CATEGORIES}
fi
viraxslot commented 3 years ago

Hey, guys @edmond-io @SwaySway. I faced the same problem, any solutions found? It would be really helpful. Thanks!

viraxslot commented 3 years ago

Ok, the solution: restore/create parameters.json file in amplify/backend/auth/<APP_PREFIX><GUID>.

File content:

{
    "authSelections": "userPoolOnly",
    "resourceName": "<APP_PREFIX><GUID>",
    "serviceType": "imported",
    "region": "<NEEDED_REGION>"
}

In the log above ="authappafe49611"

hirenumradia commented 3 years ago

@viraxslot This didn't work for me :( I can't seem to build my app.

josefaidt commented 3 years ago

Hey y'all :wave: are you still experiencing this issue with the latest version of the Amplify CLI?

github-actions[bot] commented 3 years ago

This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels for those types of questions.