CloudFormation Custom Resources Overview
- Purpose: Custom resources are used to define resources not natively supported by CloudFormation or to implement custom provisioning logic.
- Use Cases:
- Managing on-premises resources.
- Integrating third-party resources.
- Running custom scripts during stack lifecycle events (create, update, delete) via Lambda functions.
Common Exam Point
- Emptying S3 Bucket: A popular exam topic is using a Lambda function to empty an S3 bucket before deletion, as CloudFormation cannot delete a non-empty S3 bucket.
Defining a Custom Resource
- Resource Type: Custom resources are defined with the type
Custom::MyCustomResourceTypeName.
- Backed By: The custom resource can be backed by a Lambda function (most common) or an SNS topic.
- Location: The backing Lambda function or SNS topic must be in the same region as the custom resource.
Lambda Custom Resource
- Properties:
ServiceToken: The ARN of the Lambda function or SNS topic.
Input Data Parameters: Allows passing input values to the Lambda function.
Example Use Case
- Deleting S3 Bucket Contents:
- Problem: CloudFormation cannot delete a non-empty S3 bucket.
- Solution: Use a custom resource to delete all objects in the bucket first.
- Process:
- When the custom resource is deleted, it triggers a Lambda function.
- The Lambda function runs API calls to empty the S3 bucket.
- Once the bucket is empty, CloudFormation can proceed to delete it.
Diagram Explanation
- The diagram provided in the training likely illustrates the flow of events when a CloudFormation stack is deleted, showing how the custom resource interacts with the Lambda function to empty the S3 bucket before the bucket is deleted by CloudFormation.
Conclusion
- Custom resources in CloudFormation are powerful tools for extending its capabilities and integrating external logic and resources into your infrastructure management.
Next Steps
- Continue to the next lecture for more in-depth knowledge and practical examples of AWS CloudFormation.