lab80_diagram
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

  • CDK AWS CDK
  • CloudFormation CloudFormation
  • CLI 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

  1. 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.

  2. Create a folder for the app and into it:
    mkdir cdk-app
    cd cdk-app
  3. Initialize CDK project template for JavaScript:
    cdk init --language javascript
  4. Check if everything got created:
    cdk ls

  5. 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
  6. 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.

  7. 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
  8. 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.

  9. 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

  10. 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:

  11. 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.


Lab files


Acknowledgements

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *