CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-pulumi--aws

A Pulumi package for creating and managing Amazon Web Services (AWS) cloud resources with infrastructure-as-code.

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

rekognition.mddocs/services/

Amazon Rekognition

Amazon Rekognition makes it easy to add image and video analysis to your applications using machine learning.

Package

import * as aws from "@pulumi/aws";

Overview

Amazon Rekognition provides pre-trained and customizable computer vision capabilities.

Key Features

Image Analysis

  • Object and Scene Detection: Identify thousands of objects
  • Facial Analysis: Detect faces and analyze attributes
  • Face Comparison: Compare faces across images
  • Celebrity Recognition: Identify famous people
  • Text in Images: Extract text from images (OCR)
  • Inappropriate Content Detection: Detect unsafe content

Video Analysis

  • Real-Time Streaming: Analyze video streams
  • Stored Video: Analyze stored video files
  • Person Tracking: Track people through video
  • Activity Detection: Identify activities and events

Common Patterns

Face Collection

// Create IAM role for Rekognition
const rekognitionRole = new aws.iam.Role("rekognition-role", {
    assumeRolePolicy: JSON.stringify({
        Version: "2012-10-17",
        Statement: [{
            Action: "sts:AssumeRole",
            Effect: "Allow",
            Principal: {
                Service: "rekognition.amazonaws.com",
            },
        }],
    }),
});

new aws.iam.RolePolicy("rekognition-s3", {
    role: rekognitionRole.id,
    policy: JSON.stringify({
        Version: "2012-10-17",
        Statement: [{
            Effect: "Allow",
            Action: [
                "s3:GetObject",
                "s3:ListBucket",
            ],
            Resource: [
                bucket.arn,
                pulumi.interpolate`${bucket.arn}/*`,
            ],
        }],
    }),
});

Lambda Integration for Image Analysis

const imageAnalysisLambda = new aws.lambda.Function("image-analysis", {
    runtime: "python3.11",
    handler: "index.handler",
    role: lambdaRole.arn,
    code: new pulumi.asset.AssetArchive({
        "index.py": new pulumi.asset.StringAsset(`
import boto3
import json

rekognition = boto3.client('rekognition')

def handler(event, context):
    bucket = event['bucket']
    key = event['key']
    
    # Detect labels
    response = rekognition.detect_labels(
        Image={
            'S3Object': {
                'Bucket': bucket,
                'Name': key
            }
        },
        MaxLabels=10,
        MinConfidence=90
    )
    
    return {
        'labels': response['Labels']
    }
        `),
    }),
});

// Grant Rekognition permissions
new aws.iam.RolePolicy("lambda-rekognition", {
    role: lambdaRole.id,
    policy: JSON.stringify({
        Version: "2012-10-17",
        Statement: [{
            Effect: "Allow",
            Action: [
                "rekognition:DetectLabels",
                "rekognition:DetectFaces",
                "rekognition:DetectText",
                "rekognition:DetectModerationLabels",
            ],
            Resource: "*",
        }],
    }),
});

S3 Trigger for Automatic Analysis

const s3Trigger = new aws.s3.BucketNotification("image-upload", {
    bucket: bucket.id,
    lambdaFunctions: [{
        lambdaFunctionArn: imageAnalysisLambda.arn,
        events: ["s3:ObjectCreated:*"],
        filterSuffix: ".jpg",
    }],
});

new aws.lambda.Permission("s3-invoke", {
    action: "lambda:InvokeFunction",
    function: imageAnalysisLambda.arn,
    principal: "s3.amazonaws.com",
    sourceArn: bucket.arn,
});

Video Analysis with SNS Notifications

const videoAnalysisRole = new aws.iam.Role("video-analysis", {
    assumeRolePolicy: JSON.stringify({
        Version: "2012-10-17",
        Statement: [{
            Action: "sts:AssumeRole",
            Effect: "Allow",
            Principal: {
                Service: "rekognition.amazonaws.com",
            },
        }],
    }),
});

new aws.iam.RolePolicy("video-sns-publish", {
    role: videoAnalysisRole.id,
    policy: JSON.stringify({
        Version: "2012-10-17",
        Statement: [{
            Effect: "Allow",
            Action: "sns:Publish",
            Resource: snsTopic.arn,
        }],
    }),
});

Custom Labels for Custom Object Detection

Train custom models for your specific use case.

const customLabelsRole = new aws.iam.Role("custom-labels", {
    assumeRolePolicy: JSON.stringify({
        Version: "2012-10-17",
        Statement: [{
            Action: "sts:AssumeRole",
            Effect: "Allow",
            Principal: {
                Service: "rekognition.amazonaws.com",
            },
        }],
    }),
});

Use Cases

  • Content Moderation: Filter inappropriate images/videos
  • Face Authentication: Verify user identity
  • Photo Organization: Auto-tag and organize photos
  • Security Surveillance: Analyze security camera feeds
  • Retail Analytics: Track customer behavior in stores
  • Manufacturing QA: Detect defects in products
  • Media Analysis: Tag and search video content

Best Practices

  1. Optimize Image Quality: Use high-resolution images for better accuracy
  2. Implement Caching: Cache results for frequently accessed images
  3. Handle Rate Limits: Implement retry logic with exponential backoff
  4. Monitor Costs: Track API calls to manage expenses
  5. Use Custom Labels: Train models for domain-specific objects

Related Services

  • S3 - Store images and videos
  • Lambda - Process analysis results
  • Kinesis Video Streams - Stream video analysis
  • SNS - Notifications for video analysis completion

Install with Tessl CLI

npx tessl i tessl/npm-pulumi--aws@7.16.0

docs

index.md

quickstart.md

README.md

tile.json