I want to develop a Python Regex that can choose a complete Terraform resource block. I want to choose each resource block separately when there are several of them in a file (see example below).
The following regexes have been tested. When there are numerous closing brackets in the code, the first one becomes tangled. The second one simply chooses the entire file.
1) match = re.search(r'resource.*?\{(.*?)\}', code, re.DOTALL)
2) match = re.search(r'resource.*?\{(.*)\}', code, re.DOTALL)
Sample file:
resource "aws_s3_bucket_notification" "aws-lambda-trigger" {
bucket = aws_s3_bucket.newbucket.id
lambda_function {
lambda_function_arn = aws_lambda_function.test_lambda.arn
events = ["s3:ObjectCreated:*"]
filter_prefix = var.prefix
filter_suffix = var.suffix
}
}
resource "aws_s3_bucket" "newbucket" {
bucket = var.bucket_name
force_destroy = true
acl = var.acl_value
}