Synthesizing the template – Programmatic Approach to IaC with AWS CDK

The synthesizing process generates a valid CloudFormation YAML template. At this point, you could also deploy it directly within your AWS environment, or instruct CDK to do so. For now, let’s trigger template synthetization with the cdk synth command:

The 40-odd lines of code we added to the lib/chapter-6-stack.ts file resulted in a template 10 times its initial size. That’s the power of abstraction that AWS CDK offers you. As you may have noticed, several things are happening behind the scenes that would otherwise require the user to code all of this manually in CloudFormation. Starting from defining resource dependencies, and creating IAM roles, to uploading the Lambda functions in a CDK-managed S3 bucket, CDK took a lot of plumbing tasks away from us. In addition to displaying the YAML formatted template over STDOUT, you can also check the JSON output in the cdk.out/Chapter6Stack.template.json file.

Deploying the CDK stack into an AWS account

As the final step of our hands-on exercise, let’s go ahead and deploy the application in an AWS account using the cdk deploy command:

Once the deployment completes, we can check the relevant resources that were created as part of the CloudFormation deployment. The most important ones for us are the S3 bucket and the DynamoDB table. If you go to the CloudFormation service in your account, you will find a stack called Chapter6Stack.

After clicking on the stack’s name, you can check the resources, as shown in Figure 6.3:

Figure 6.3 – Stack resources deployed as part of the CDK deployment

Having completed the deployment, let’s go ahead and test the end-to-end workflow.

Testing the image analysis workflow

The test application we’ve deployed in the AWS account expects an image to be uploaded in the S3 bucket, which then triggers the Lambda function that interacts with the Amazon Rekognition service. The image I used for testing the workflow can be found at https://tinyurl.com/3z9ecjpb. You might want to upload it or use a different one.

Once the upload completes, the S3 event notification will fire off the Lambda function. The image label analysis results will be available in the DynamoDB table, as shown in Figure 6.4:

Figure 6.4 – Image labeling results persisted in DynamoDB

The response from Rekognition includes a JSON payload that defines the matched categories and respective confidence scores:

This completes our testfor the image labeling automation that we built using CDK.

Note

If you face any issues with the workflow, or the labeling results are not available in DynamoDB, you might want to check CloudWatch Logs. If you remember, we gave permissions to our Lambda function to be able to create a CloudWatch log group and publish the logging events into the log group for every invocation. It’s a good place to start debugging any unexpected scenarios.

Leave a Comment