async function createAppSyncAPI()

in src/pipelineResources.js [454:606]


async function createAppSyncAPI() {      
    const iamClient = new IAMClient({region: REGION});

    startSpinner('Creating policy for Lambda invocation ...', true);
    let policyName = NAME+"LambdaInvokePolicy";
    let command = new CreatePolicyCommand({
        PolicyDocument: JSON.stringify({
          Version: "2012-10-17",
          Statement: [
            {
                Effect: "Allow",
                Action: "lambda:invokeFunction",            
                Resource: [
                    LAMBDA_ARN,
                    LAMBDA_ARN + ":*"
                ]            
            },
          ],
        }),
        PolicyName: policyName,
      }
    );
    let response = await iamClient.send(command);
    const policyARN = response.Policy.Arn;
    storeResource({LambdaInvokePolicy: policyARN});
    succeedSpinner('Lambda invocation policy created: ' + yellow(policyName), {logLevel: 'debug'});
    loggerInfo('Created lambda invoke policy');
    let roleName = NAME +"LambdaInvocationRole";
    let params = {
        AssumeRolePolicyDocument: JSON.stringify({
            Version: "2012-10-17",
            Statement: [                
                {
                    Effect: "Allow",
                    Principal: {
                        Service: "appsync.amazonaws.com"
                    },
                    Action: "sts:AssumeRole"
                }          
            ]
        }),
        RoleName: roleName
    };

    startSpinner('Creating role for Lambda invocation ...', true);
    response = await iamClient.send(new CreateRoleCommand(params));        
    const LAMBDA_INVOCATION_ROLE = response.Role.Arn;        
    storeResource({LambdaInvokeRole: roleName});
    sleep(5000);
    succeedSpinner('Lambda invocation role created: ' + yellow(roleName), {logLevel: 'debug'});
    loggerInfo('Created lambda invocation role');

    startSpinner('Attaching policy ...', true);
    params = {
        RoleName: roleName,
        PolicyArn: policyARN,
    };
    command = new AttachRolePolicyCommand(params);
    await iamClient.send(command);
    succeedSpinner('Attached policy to role', {logLevel: 'info'});
    // APPSync API
    const appSyncClient = new AppSyncClient({region: REGION});

    startSpinner('Creating AppSync API ...', true);
    params = {
        name: NAME + 'API',
        authenticationType: "API_KEY",      
        visibility: "GLOBAL",
        apiType: "GRAPHQL"        
    };
    command = new CreateGraphqlApiCommand(params);
    response = await appSyncClient.send(command);
    const apiId = response.graphqlApi.apiId;
    storeResource({AppSyncAPI: apiId});
    succeedSpinner('Created API: ' + yellow(NAME + 'API', {logLevel: 'debug'}));
    loggerInfo('Created App Sync API');

    // create Key
    startSpinner('Creating API key ...', true);
    command = new CreateApiKeyCommand({apiId: apiId});
    response = await appSyncClient.send(command);
    const apiKey = response.apiKey.id;
    succeedSpinner('Created API key', {logLevel: 'info'});

    // create datasource
    startSpinner('Creating DataSource ...', true);
    params = {
        apiId: apiId,
        name: NAME + 'DataSource',       
        type: "AWS_LAMBDA",
        serviceRoleArn: LAMBDA_INVOCATION_ROLE,
        lambdaConfig: {
            lambdaFunctionArn: LAMBDA_ARN, 
        },
    };    
    command = new CreateDataSourceCommand(params);
    response = await appSyncClient.send(command);
    succeedSpinner('Created DataSource: ' + yellow(NAME+'DataSource'), {logLevel: 'debug'});
    loggerInfo('Created datasource');

    // create function
    startSpinner('Creating Function ...', true);
    params = {
        apiId: apiId,
        name: NAME+'Function',       
        dataSourceName: NAME+'DataSource',
        runtime: {
            name: "APPSYNC_JS",
            runtimeVersion: "1.0.0",
        },
        code:
`import { util } from '@aws-appsync/utils';
export function request(ctx) {
    const {source, args} = ctx
    return {
        operation: 'Invoke',
        payload: {
            field: ctx.info.fieldName, 
            arguments: args,
            selectionSetGraphQL: ctx.info.selectionSetGraphQL,
            source 
        },
    };
}
    
export function response(ctx) {
    return ctx.result;
}`

    };
    command = new AppSyncCreateFunctionCommand(params);
    response = await appSyncClient.send(command);
    await sleep(5000);
    let functionId = response.functionConfiguration.functionId;    
    storeResource({AppSyncAPIFunction: functionId});
    succeedSpinner('Created Function: ' + yellow(NAME+'Function'), {logLevel: 'debug'});
    loggerInfo('Created function');

    // Upload schema
    startSpinner('Uploading schema ...', true);
    let encoder = new TextEncoder();
    let definition = encoder.encode(APPSYNC_SCHEMA);
    params = { 
        apiId: apiId,
        definition: definition,
      };
    command = new StartSchemaCreationCommand(params);
    response = await appSyncClient.send(command);    
    await sleep(5000);
    succeedSpinner('Added schema', {logLevel: 'info'});
    
    await attachResolvers(appSyncClient, apiId, functionId);
}