async function checkPipeline()

in src/pipelineResources.js [145:236]


async function checkPipeline() {
    // Checking if Role, Lambda and AppSync API is already created.
    const iamClient = new IAMClient({region: REGION});
    const lambdaClient = new LambdaClient({region: REGION});
    const appSyncClient = new AppSyncClient({region: REGION});

    let lambdaExists = false;
    let appSyncExists = false;
    let roleExists = false;

    loggerInfo('Checking pipeline resources', {toConsole: true});
    startSpinner('Checking for lambda...');
    try {
        const command = new GetFunctionCommand({FunctionName: NAME +'LambdaFunction'});
        await lambdaClient.send(command);
        lambdaExists = true;
        succeedSpinner('Found lambda', {logLevel: 'info'});
    } catch (error) {
        lambdaExists = false;
        const text = 'Lambda not found';
        warnSpinner(text);
        loggerInfo(text);
        loggerDebug("checkPipeline GetFunctionCommand: " + JSON.stringify(error));
    }

    startSpinner('Checking for API...');
    const notFound = 'API not found';
    try {
        // set maxResults to max allowed value as workaround until https://github.com/aws/amazon-neptune-for-graphql/issues/39 is addressed
        const command = new ListGraphqlApisCommand({apiType: "GRAPHQL", maxResults: 25});
        const response = await appSyncClient.send(command);
        response.graphqlApis.forEach(element => {
            if (element.name == NAME + 'API') {
                //APPSYNC_API_ID = element.apiId;
                appSyncExists = true;
            }
        });
        if (appSyncExists) {
            succeedSpinner('API found', {logLevel: 'info'});
        } else {
            warnSpinner(notFound);
            loggerInfo(notFound);
        }
    } catch (error) {
        warnSpinner(notFound);
        loggerInfo(notFound);
        loggerError('Error checking for API', error);
        appSyncExists = false;
    }

    startSpinner('Checking for lambda execution role...');
    try {
        const command = new GetRoleCommand({ RoleName: NAME + "LambdaExecutionRole" });
        const response = await iamClient.send(command);
        LAMBDA_ROLE = response.Role.Arn;
        roleExists = true;
        succeedSpinner('Lambda execution role found', {logLevel: 'info'});
    } catch (error) {
        const text = 'Lambda execution role not found';
        warnSpinner(text);
        loggerInfo(text);
        loggerDebug("checkPipeline GetRoleCommand: " + JSON.stringify(error));
        roleExists = false;
    }
    
    if (lambdaExists && appSyncExists && roleExists) {
        loggerInfo('Pipeline exists.', {toConsole: true});
        pipelineExists = true;
    } else {
        loggerInfo('Pipeline does not exist.', {toConsole: true});
    }

    if (lambdaExists && appSyncExists && roleExists) return;
    if (!lambdaExists && !appSyncExists && !roleExists) return;

    loggerError('One or more pipeline resources are missing.');

    if (!lambdaExists) {
        loggerError('Lambda ' + yellow(NAME) + 'LambdaFunction is  missing.');
    }

    if (!roleExists) {
        loggerError('Role ' + yellow(NAME) + 'LambdaExecutionRole is  missing.');
    }

    if (!appSyncExists) {
        loggerError('AppSync ' + yellow(NAME) + 'API is  missing.');
    }

    loggerError('Fix the issue manually or create the pipeline resources with a new name.');
    process.exit(1);
}