constructor()

in lib/ml-oci-stack.ts [27:125]


  constructor(scope: Construct, id: string, props?: StackProps) {
    super(scope, id, props);

    const dockerFile = path.join(__dirname, "../ml-images/oci");
    const lambdaFunc = new lambda.DockerImageFunction(this, "MlLambdaOci", {
      code: lambda.DockerImageCode.fromImageAsset(dockerFile),
      environment: {
        ["POWERTOOLS_METRICS_NAMESPACE"]: "ServerlessMlBertOci",
        ["POWERTOOLS_SERVICE_NAME"]: "inference",
      },
      memorySize: 6000,
      timeout: Duration.seconds(180),
      description: "Pytorch BERT NLP Model deployed as a Docker Image (OCI)",
    });

    // Deploy APIGW
    const api = new apigw.CfnApi(this, "MlApiOci", {
      name: "mlociapi",
      description: "ML API Inference OCI",
      protocolType: "HTTP",
    });

    const integration = new apigw.CfnIntegration(this, "MlIntegOci", {
      apiId: api.ref,
      integrationType: "AWS_PROXY",
      integrationUri:
        "arn:aws:apigateway:" +
        props?.env?.region +
        ":lambda:path/2015-03-31/functions/" +
        lambdaFunc.functionArn +
        "/invocations",
      payloadFormatVersion: "2.0",
      integrationMethod: "POST",
    });

    const routes = new apigw.CfnRoute(this, "MlOciRoutes", {
      apiId: api.ref,
      routeKey: "POST /",
      target: "integrations/" + integration.ref,
    });

    const stage = new apigw.CfnStage(this, "MlOciStage", {
      apiId: api.ref,
      stageName: "develop",
      autoDeploy: true,
      description: "Stage for ml models (OCI)",
    });

    // Add permission to Lambda function
    const servicePrincipal = new iam.ServicePrincipal(
      "apigateway.amazonaws.com"
    );
    const apiArn =
      "arn:aws:execute-api:" +
      props?.env?.region +
      ":" +
      props?.env?.account +
      ":" +
      api.ref +
      "/*/*/";

    lambdaFunc.addPermission("api-invocation", {
      principal: servicePrincipal,
      sourceArn: apiArn,
      sourceAccount: props?.env?.account,
    });

    // Output Lambda info
    new CfnOutput(this, "LambdaNameOci", {
      description: "Lambda Function Name (OCI)",
      value: lambdaFunc.functionName,
    });

    // Output HTTP URL
    new CfnOutput(this, "ApiUrlOci", {
      description: "API Gateway Endpoint URL for ML Inference OCI",
      value: [
        "https://",
        api.ref,
        ".execute-api.",
        props?.env?.region,
        ".amazonaws.com/",
        stage.stageName,
        "/",
      ].join(""),
    });

    // Sample Request
    new CfnOutput(this, "CurlRequestOCI", {
      description: "Manual curl request",
      value:
        "curl --location --request POST 'https://" +
        api.ref +
        ".execute-api." +
        props?.env?.region +
        '.amazonaws.com/develop/\' --header \'Content-Type: application/json\' --data-raw \'{"model_type": "nlp1","question": "When was the car invented?","context": "Cars came into global use during the 20th century, and developed economies depend on them. The year 1886 is regarded as the birth year of the modern car when German inventor Karl Benz patented his Benz Patent-Motorwagen. Cars became widely available in the early 20th century. One of the first cars accessible to the masses was the 1908 Model T, an American car manufactured by the Ford Motor Company. Cars were rapidly adopted in the US, where they replaced animal-drawn carriages and carts, but took much longer to be accepted in Western Europe and other parts of the world."}\'',
    });

  }