Lab 80
In this lab a CDK stack will be created to provide a rekognition service and log result in DynamoDB. JavaScript will be used to create a cdk-app-stack.js to create constructs for S3 bucket to upload images, on Object Created in S3 a Lambda Event Source will trigger Lambda function
Services Covered
- AWS CDK
- CloudFormation
- CloudShell
Lab description
In this lab a CDK stack will be created to provide a rekognition service and log result in DynamoDB. JavaScript will be used to create a cdk-app-stack.js to create constructs for S3 bucket to upload images, on Object Created in S3 a Lambda Event Source will trigger Lambda function written in Python that will send that object to Amazon Rekognition and log results to DynamoDB table.
- Write CDK stack in JavaScript
- Deploy CDK stack using CloudShell
- Write Lambda app to send objects from S3 to Amazon Rekognition
Lab date
12-10-2021
Prerequisites
- AWS account
Lab steps
- Choose a region that supports both AWS CloudShell and Amazon Rekognition. Open CloudShell and run:
sudo npm install -g aws-cdk
This will install CDK.
- Create a folder for the app and into it:
mkdir cdk-app
cd cdk-app
- Initialize CDK project template for JavaScript:
cdk init --language javascript
- Check if everything got created:
cdk ls
- Install the necessary packages:
npm install @aws-cdk/aws-s3 @aws-cdk/aws-iam @aws-cdk/aws-lambda @aws-cdk/aws-lambda-event-sources @aws-cdk/aws-dynamodb
- Update the cdk-app-stack.js in lib directory
cd lib/
rm cdk-app-stack.js
touch cdk-app-stack.js
nano cdk-app-stack.js
And copy contents of the cdk-app-stack.js. If nano is not installed run
sudo yum install nano
This code will create constructs for S3 bucket, IAM Role, Lambda function, Lambda EventSource and DynamoDB.
- Leave the lib directory
cd ../
Create a new directory for lambda function
mkdir lambda
Create index.py file
touch index.py
And paste the code from index.py
nano index.py
- Go one level up from lambda
cd ../
then bootstrap the CDK applications:
cdk bootstrap
This will create a CloudFormation stack and deploy a S3 bucket.
- Time to deploy the CDK defined resources, a CloudFormation stack will be created yet again and resources deployed.
When CdkAppStack in CloudFormation gets created it’s time for testing
- Upload some images to the newly created S3 bucket. That worked very nicly for me, after uploading some images of books, myself, my dog and group of friends having dinner those results were logged into DynamoDB table:
- Time for clean-up. Empty the S3 bucket and then in CloudBash run:
cdk destroy
This will delete the CloudFormation stack and all the created resources.