New Resource: aws_custom_resource #10096
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Supersedes #3361.
Introduction
Lambda-backed Custom resources are indispensable for bridging the gap of infrastructure not currently supported by Terraform, without having to go through the effort of writing a custom plugin or extending existing providers. Previously, custom resources are supported through CloudFormation but not Terraform, so the old workaround was to call a CloudFormation script from Terraform thats calls the custom resource. This code provides an adapter for the Custom Resource making it a first class Terraform managed resource.
How to Use
Argument Reference
The following arguments are supported:
service_token
- (Required) The service token (an Amazon SNS topic or AWS Lambda function Amazon Resource Name) that is obtained from the custom resource provider to access the service.resource_type
- (Required) The developer-chosen resource type of the custom resourceresource_properties
- (Optional) This field contains the contents of the Properties object sent by the Terraform. Its contents are defined by the custom resource provider.Attribute Reference
In addition to all arguments above, the following attributes are exported:
id
- A random id that is unique to this resource.old_resource_properties
- Used only for Update requests. Contains the resource properties that were declared previous to the update request.data
- The custom resource provider-defined name-value pairs sent with the response.This is an example of how the quick certificate could be used:
When you run "terraform apply", your lambda function will be invoked with a JSON event as follows. RequestType will be one of Create||Update||Delete, and it is up to you to decide how to handle that. In this case, it is "Create". In addition, you will receive the ResourceType, which is defined by you, and can be used to have a single lambda function responsible for handling the creation of multiple custom resources. Finally, you will receive the ResourceProperties which is a map of input parameters.
Similarly, if you change the resource properties and run terraform apply, your Lambda function will get called with an "Update" event that contains information about the OldResourceProperties, as well as the new ResourceProperties. In this example, b was changed from 1 to 2, and this is the event that was generated
Delete is the final event that is created, and is run on "terraform destroy". It is up to you to figure out how to clean up the resources you created. Below is an example of a "Delete" event.
In your lambda function, you are expected to return a response to the with a JSON object like follows. Status is required and must be either "SUCCESS" or "FAILURE". If "FAILURE", then a Reason must be specified, which will be printed in the terraform console as an error message. Finally, Data is a map[string]string that will be saved and can be used as an output from the module. i.e
aws_custom_resource.test.data[“out1”]
.Below is a trivial example of the source code for a lambda function that handles the customresource.
Output from acceptance testing: