DVA-C02 practice question 19 of 35

A company wants to automate part of its deployment process. A developer needs to automate the process of checking for and deleting unused resources…

Full set: 598 questions

Domain Troubleshooting and Optimization

A company wants to automate part of its deployment process. A developer needs to automate the process of checking for and deleting unused resources that supported previously deployed stacks but that are no longer used. The company has a central application that uses the AWS Cloud Development Kit (AWS CDK) to manage all deployment stacks. The stacks are spread out across multiple accounts. The developer’s solution must integrate as seamlessly as possible within the current deployment process. Which solution will meet these requirements with the LEAST amount of configuration?
Answer options
A: In the central AWS CDK application, write a handler function in the code that uses AWS SDK calls to check for and delete unused resources. Create an AWS CloudPormation template from a JSON file. Use the template to attach the function code to an AWS Lambda function and lo invoke the Lambda function when the deployment slack runs.
B: In the central AWS CDK application. write a handler function in the code that uses AWS SDK calls to check for and delete unused resources. Create an AWS CDK custom resource Use the custom resource to attach the function code to an AWS Lambda function and to invoke the Lambda function when the deployment stack runs. Correct answer
C: In the central AWS CDK, write a handler function m the code that uses AWS SDK calls to check for and delete unused resources. Create an API in AWS Amplify Use the API to attach the function code to an AWS Lambda function and to invoke the Lambda function when the deployment stack runs.
D: In the AWS Lambda console write a handler function in the code that uses AWS SDK calls to check for and delete unused resources. Create an AWS CDK custom resource. Use the custom resource to import the Lambda function into the stack and to Invoke the Lambda function when the deployment stack runs.
Correct Answer:
B. In the central AWS CDK application. write a handler function in the code that uses AWS SDK calls to check for and delete unused resources. Create an AWS CDK custom resource Use the custom resource to attach the function code to an AWS Lambda function and to invoke the Lambda function when the deployment stack runs.
Explanation

The most seamless and efficient way to run custom logic during a deployment managed by the AWS Cloud Development Kit (AWS CDK) is to use a CDK custom resource. This approach allows the developer to define the Lambda function's handler code directly within the central CDK application. The custom resource construct then handles the creation of the Lambda function and its invocation during the CloudFormation stack lifecycle events (create, update, delete). This keeps all logic and infrastructure definition within a single, version-controlled application, meeting the requirements for seamless integration and minimal configuration.

Why Incorrect

A: Manually creating a CloudFormation template from a JSON file is redundant and counterintuitive when using the AWS CDK, which is designed to generate these templates programmatically. C: Introducing AWS Amplify and an API is overly complex for a task that needs to run during a deployment. This adds unnecessary components and configuration. D: Writing the Lambda function code in the console separates it from the infrastructure-as-code (IaC) in the CDK application, breaking the principle of a single source of truth and seamless integration.

References

1. AWS CDK Developer Guide - Custom Resources: "Sometimes you need to add AWS or third-party resources to your CDK app that aren't available in the AWS CloudFormation Registry. In these cases, you can use a custom resource. A custom resource is an AWS CloudFormation resource that is implemented by a Lambda function." This confirms that custom resources are the standard CDK mechanism for this scenario. (AWS Cloud Development Kit (AWS CDK) v2 Developer Guide, "Custom resources").

2. AWS CloudFormation User Guide - Custom Resources: "Custom resources enable you to write custom provisioning logic in templates that AWS CloudFormation runs during a stack operation... AWS CloudFormation invokes the function that's associated with the custom resource's service token." This explains the underlying mechanism that the CDK custom resource construct utilizes. (AWS CloudFormation User Guide, "Custom resources").

3. AWS CDK API Reference - aws-cdk-lib.customresources module: The documentation for the AwsCustomResource construct shows how it integrates with AWS SDK calls and is triggered on onCreate, onUpdate, or onDelete events, directly aligning with the question's need to run code when a stack is deployed. (AWS CDK API Reference, aws-cdk-lib.customresources module, AwsCustomResource class).

Scroll to Top