function makeTranscribeStatemachineLambdas()

in lib/cdk-stack.ts [383:615]


function makeTranscribeStatemachineLambdas(
  scope: cdk.Construct,
  id: string,
  props: makeTranscribeStatemachineLambdasProps
): transcribeStatemachineHandlers {
  const uploadPodcastId = id + 'UploadPodcast';
  const startTranscriptionId = id + 'StartTranscription';
  const checkTranscriptionId = id + 'CheckTranscription';
  const processTranscriptionId = id + 'CleanupTranscription';

  const commonProps = {
    environment: {
      [ENV_KEY_PODCAST_EPISODE_TABLE_NAME]: props.podcastEpisodeTable.tableName,
      [ENV_KEY_PODCAST_DATA_BUCKET_NAME]: props.podcastBucket.bucketName,
      [ENV_KEY_TRANSCRIBE_ACCESS_ROLE_ARN]: props.transcribeAccessRole.roleArn,
      ...commonStaticLambdaEnvs,
    },
    memorySize: 1024,
    timeout: cdk.Duration.seconds(60),
  };

  // Lambda handler implementations common across all languages
  const updateEpisodeStatus = new lambda.Function(
    scope,
    id + 'UpdateEpisodeStatus',
    {
      runtime: lambda.Runtime.GO_1_X,
      handler: 'main',
      code: lambda.Code.fromAsset('lambda/go/update-episode-status'),
      ...commonProps,
    }
  );

  const startTranscription = new lambda.Function(scope, startTranscriptionId, {
    runtime: lambda.Runtime.GO_1_X,
    handler: 'main',
    code: lambda.Code.fromAsset('lambda/go/start-transcription'),
    ...commonProps,
  });

  const checkTranscription = new lambda.Function(scope, checkTranscriptionId, {
    runtime: lambda.Runtime.GO_1_X,
    handler: 'main',
    code: lambda.Code.fromAsset('lambda/go/check-transcription'),
    ...commonProps,
  });

  const processTranscription = new lambda.Function(
    scope,
    processTranscriptionId,
    {
      runtime: lambda.Runtime.GO_1_X,
      handler: 'main',
      code: lambda.Code.fromAsset('lambda/go/process-transcription'),
      ...commonProps,
    }
  );

  let handlers: transcribeStatemachineHandlers;
  switch (props.workshopLanguage) {
    case WorkshopLanguage.Go:
      handlers = {
        // Common handlers
        updateEpisodeStatus: updateEpisodeStatus,
        startTranscription: startTranscription,
        checkTranscription: checkTranscription,
        processTranscription: processTranscription,

        // language specific handlers
        uploadPodcast: new lambda.Function(scope, uploadPodcastId, {
          runtime: lambda.Runtime.GO_1_X,
          handler: 'main',
          code: lambda.Code.fromAsset('lambda/go/upload-podcast'),
          ...commonProps,
        }),
      };
      break;

    case WorkshopLanguage.Python:
      handlers = {
        // Common handlers
        updateEpisodeStatus: updateEpisodeStatus,
        startTranscription: startTranscription,
        checkTranscription: checkTranscription,
        processTranscription: processTranscription,

        // language specific handlers
        uploadPodcast: new lambda.Function(scope, uploadPodcastId, {
          runtime: lambda.Runtime.PYTHON_3_8,
          handler: 'upload_podcast.lambda_handler',
          code: lambda.Code.fromAsset('lambda/python'),
          ...commonProps,
        }),
      };
      break;

    case WorkshopLanguage.Java:
      handlers = {
        // Common handlers
        updateEpisodeStatus: updateEpisodeStatus,
        startTranscription: startTranscription,
        checkTranscription: checkTranscription,
        processTranscription: processTranscription,

        // language specific handlers
        uploadPodcast: new lambda.Function(scope, uploadPodcastId, {
          runtime: lambda.Runtime.JAVA_11,
          handler: 'com.amazonaws.workshop.UploadPodcast::handleRequest',
          code: lambda.Code.fromAsset('lambda/java'),
          ...commonProps,
        }),
      };
      break;

    case WorkshopLanguage.Javascript:
      handlers = {
        // Common handlers
        updateEpisodeStatus: updateEpisodeStatus,
        startTranscription: startTranscription,
        checkTranscription: checkTranscription,
        processTranscription: processTranscription,

        // language specific handlers
        uploadPodcast: new lambda_nodejs.NodejsFunction(scope, uploadPodcastId, {
          entry: 'lambda/javascript/upload-podcast.js',
          bundling: getNodejsBundlingOptions(uploadPodcastId),
          ...commonProps,
        }),
      };
      break;

    default:
      throw new Error(`unknown workshop language, ${props.workshopLanguage}`);
  }

  //------------------------------
  // Update Episode Status
  //------------------------------
  handlers.updateEpisodeStatus.addToRolePolicy(
    iamUtils.makePolicyStatement({
      effect: iam.Effect.ALLOW,
      actions: ['dynamodb:UpdateItem'],
      resources: [props.podcastEpisodeTable.tableArn],
    })
  );

  //------------------------------
  // Upload Podcast
  //------------------------------
  handlers.uploadPodcast.addToRolePolicy(
    iamUtils.makePolicyStatement({
      effect: iam.Effect.ALLOW,
      actions: [
        's3:AbortMultipartUpload',
        's3:GetBucketLocation',
        's3:GetObject',
        's3:ListBucket',
        's3:ListBucketMultipartUploads',
        's3:PutObject',
      ],
      resources: [
        props.podcastBucket.bucketArn,
        props.podcastBucket.bucketArn + '/*',
      ],
    })
  );

  //------------------------------
  // Start Transcription
  //------------------------------
  handlers.startTranscription.addToRolePolicy(
    iamUtils.makePolicyStatement({
      effect: iam.Effect.ALLOW,
      actions: ['transcribe:StartTranscriptionJob'],
      resources: ['*'],
    })
  );
  handlers.startTranscription.addToRolePolicy(
    iamUtils.makePolicyStatement({
      effect: iam.Effect.ALLOW,
      actions: ['iam:PassRole'],
      resources: ['*'],
    })
  );

  //------------------------------
  // Check Transcription
  //------------------------------
  handlers.checkTranscription.addToRolePolicy(
    iamUtils.makePolicyStatement({
      effect: iam.Effect.ALLOW,
      actions: ['transcribe:GetTranscriptionJob'],
      resources: ['*'],
    })
  );

  //------------------------------
  // Process Transcription
  //------------------------------
  handlers.processTranscription.addToRolePolicy(
    iamUtils.makePolicyStatement({
      effect: iam.Effect.ALLOW,
      actions: ['transcribe:GetTranscriptionJob'],
      resources: ['*'],
    })
  );
  handlers.processTranscription.addToRolePolicy(
    iamUtils.makePolicyStatement({
      effect: iam.Effect.ALLOW,
      actions: [
        's3:AbortMultipartUpload',
        's3:GetBucketLocation',
        's3:GetObject',
        's3:ListBucket',
        's3:ListBucketMultipartUploads',
        's3:PutObject',
      ],
      resources: [
        props.podcastBucket.bucketArn,
        props.podcastBucket.bucketArn + '/*',
      ],
    })
  );
  handlers.processTranscription.addToRolePolicy(
    iamUtils.makePolicyStatement({
      effect: iam.Effect.ALLOW,
      actions: ['dynamodb:PutItem'],
      resources: [props.podcastEpisodeTable.tableArn],
    })
  );

  return handlers;
}