awslabs / aws-solutions-constructs

The AWS Solutions Constructs Library is an open-source extension of the AWS Cloud Development Kit (AWS CDK) that provides multi-service, well-architected patterns for quickly defining solutions
https://docs.aws.amazon.com/solutions/latest/constructs/
Apache License 2.0
1.22k stars 247 forks source link

New Pattern: lambda-rekognition #98

Open knihit opened 3 years ago

knihit commented 3 years ago

Based on which feature and service of Rekognition you plan to use, Rek requires permission policy setup so that lambda functions can access them. Some of the API endpoints in Rek also require an S3 bucket to be setup for input and ouput. Rek's best practices recommend not to use the default buckets provided by Rek but to build your S3 buckets. These buckets need a policy too so that Rek can access the assets stored in those buckets for analysis

Use Case

In my use case I use Rek to analyze images and may be videos too in the future. The solution creates a new bucket and enables policies that allow Rek to access the contents of the bucket for images. This bucket is isolated from the other data buckets since we only want Rek to be able to read the data that it requires (least privilege and only allow what is required)

Proposed Solution

  1. Provide the flexibility to the user to specify which Amazon Rek services does he plan to integrate with
  2. Build the service role permissions required to access lambda
  3. If the service selected requires S3 bucket, create the bucket too and update the bucket permissions as required by Rek to access it

Other

I currently use this pattern in https://aws.amazon.com/solutions/implementations/discovering-hot-topics-using-machine-learning/


This is a :rocket: Feature Request

hnishar commented 3 years ago

Overall the pattern seems like a good candidate

Jacco commented 2 years ago

Was looking for a Solution Construct to work on.

Created this README to discuss the details:

aws-lambda-rekognition module


Stability: Experimental

All classes are under active development and subject to non-backward compatible changes or removal in any future version. These are not subject to the Semantic Versioning model. This means that while you may use them, you may need to update your source code when upgrading to a newer version of this package.


Reference Documentation: https://docs.aws.amazon.com/solutions/latest/constructs/
Language Package
Python Logo Python aws_solutions_constructs.aws_lambda_rekognition
Typescript Logo Typescript @aws-solutions-constructs/aws-lambda-rekognition
Java Logo Java software.amazon.awsconstructs.services.lambdarekognition

This AWS Solutions Construct implements an AWS Lambda function connected to an Amazon Rekognition and a S3 bucket for the files shared with Rekognition.

Here is a minimal deployable pattern definition in Typescript:

import { LambdaToRekognition, LambdaToRekognitionProps } from "@aws-solutions-constructs/aws-lambda-rekognition";

new LambdaToRekognition(this, 'LambdaToRekognitionPattern', {
    lambdaFunctionProps: {
        runtime: lambda.Runtime.NODEJS_14_X,
        handler: 'index.handler',
        code: lambda.Code.fromAsset(`${__dirname}/lambda`)
    }
});

Initializer

new LambdaToRekognition(scope: Construct, id: string, props: LambdaToRekognitionProps);

Parameters

Pattern Construct Props

Name Type Description
existingLambdaObj? lambda.Function An optional, existing Lambda function to be used instead of the default function. Providing both this and lambdaFunctionProps will cause an error.
lambdaFunctionProps? lambda.FunctionProps Optional user-provided properties to override the default properties for the Lambda function.
existingVpc? ec2.IVpc An optional, existing VPC into which this pattern should be deployed. When deployed in a VPC, the Lambda function will use ENIs in the VPC to access network resources and an Gateway or Interface Endpoint will be created in the VPC for Amazon S3. If an existing VPC is provided, the deployVpc property cannot be true. This uses ec2.IVpc to allow clients to supply VPCs that exist outside the stack using the ec2.Vpc.fromLookup() method.
vpcProps? ec2.VpcProps Optional user-provided properties to override the default properties for the new VPC. enableDnsHostnames, enableDnsSupport, natGateways and subnetConfiguration are set by the pattern, so any values for those properties supplied here will be overrriden. If deployVpc is not true then this property will be ignored.
deployVpc? boolean Whether to create a new VPC based on vpcProps into which to deploy this pattern. Setting this to true will deploy the minimal, most private VPC to run the pattern:
  • One isolated subnet in each Availability Zone used by the CDK program
  • enableDnsHostnames and enableDnsSupport will both be set to true
If this property is true then existingVpc cannot be specified. Defaults to false.
bucketEnvironmentVariableName? string Optional name for the S3 bucket environment variable set for the Lambda function.
existingBucketObj? s3.IBucket Existing instance of S3 Bucket object. If this is provided, then also providing bucketProps is an error.
bucketProps? s3.BucketProps Optional user provided props to override the default props for the S3 Bucket.
bucketPermissions? string[] Optional bucket permissions to grant to the Lambda function. One or more of the following may be specified: Delete, Put, Read, ReadWrite, Write.

Pattern Properties

Name Type Description
lambdaFunction lambda.Function Returns an instance of the Lambda function created by the pattern.
vpc? ec2.IVpc Returns an interface on the VPC used by the pattern (if any). This may be a VPC created by the pattern or the VPC supplied to the pattern constructor.
s3Bucket? s3.Bucket Returns an instance of the S3 bucket created by the pattern.

Default settings

Out of the box implementation of the Construct without any override will set the following defaults:

AWS Lambda Function

Amazon S3 Bucket

TODO:

Architecture

Architecture Diagram


© Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.