A Pulumi package for creating and managing Amazon Web Services (AWS) cloud resources with infrastructure-as-code.
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Amazon Rekognition makes it easy to add image and video analysis to your applications using machine learning.
import * as aws from "@pulumi/aws";Amazon Rekognition provides pre-trained and customizable computer vision capabilities.
// 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}/*`,
],
}],
}),
});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: "*",
}],
}),
});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,
});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,
}],
}),
});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",
},
}],
}),
});Install with Tessl CLI
npx tessl i tessl/npm-pulumi--aws