Deploy 2 different packages cause resources of 1st to be removed when applying 2nd one

Hi guys,

in my company we started using Terraform with New Relic provider in order to be maintain, reuse, deploy our monitoring checks. I never myself used it before but I tried to read as more documentation as possible. Just saying this propaedeutic to let you know that might be simply be caused by an improper usage of Terraform modules as I am a newbie :blush:.

Terraform Version

v0.13.2

Newrelic Version

v2.7.5

Affected Resource(s)

  • newrelic_synthetics_monitor
  • newrelic_synthetics_monitor_script
  • newrelic_alert_policy
  • newrelic_nrql_alert_condition
  • newrelic_alert_channel

Terraform Configuration Files

Attaching source code of the PROJECT.zip which is pretty simple. This way you should be able to reproduce the problem which includes:

  • check1 which is a Synthetic check API based
  • check2 which is a Synthetic check browser based

Expected Behavior

When check 1 is live (which means I’ve run terraform apply <DIRECTORY_CHECK1>) I should be able to deploy check 2 (terraform apply <DIRECTORY_CHECK2>) without destroying the resources of check 1 which are not in check 2 as I am using terraform apply <PATH>. For an unknown reason its seems it’s doing a kind of bisect.

Actual Behavior

By applying check 1 in a workspace called “staging”, when I

  • init
  • plan
  • apply

check 2 then check 1’s resources should not removed.

Steps to Reproduce

$ terraform workspace new staging
$ terraform workspace select staging
$ terraform init ./check1
$ terraform plan ./check1
$ terraform apply ./check1

module.staging_check1.newrelic_alert_policy.check_1: Creating...
module.common_resources.newrelic_alert_channel.quality_assurance_test_slack: Creating...
module.staging_check1.newrelic_synthetics_monitor.check_1: Creating...
module.common_resources.newrelic_alert_channel.production_email: Creating...
module.common_resources.newrelic_alert_channel.aurelien_email: Creating...
module.common_resources.newrelic_alert_channel.andrea_email: Creating...
module.common_resources.newrelic_alert_channel.quality_assurance_email: Creating...
module.staging_check1.newrelic_alert_policy.check_1: Creation complete after 1s [id=48526]
module.staging_check1.newrelic_nrql_alert_condition.bad_response_in_get_cities_api: Creating...
module.staging_check1.newrelic_nrql_alert_condition.bad_response_in_get_countries_api: Creating...
module.staging_check1.newrelic_synthetics_monitor.check_1: Creation complete after 1s [id=0bb2b655-17e5-4507-82da-0895ced44d13]
module.common_resources.newrelic_alert_channel.quality_assurance_email: Creation complete after 1s [id=128191]
module.staging_check1.newrelic_synthetics_monitor_script.check_1_script: Creating...
module.common_resources.newrelic_alert_channel.quality_assurance_test_slack: Creation complete after 2s [id=128190]
module.staging_check1.newrelic_alert_policy_channel.check_1: Creating...
module.staging_check1.newrelic_synthetics_monitor_script.check_1_script: Creation complete after 1s [id=0bb2b655-17e5-4507-82da-0895ced44d13]
module.common_resources.newrelic_alert_channel.production_email: Creation complete after 2s [id=128193]
module.common_resources.newrelic_alert_channel.andrea_email: Creation complete after 2s [id=128192]
module.common_resources.newrelic_alert_channel.aurelien_email: Creation complete after 3s [id=128194]
module.staging_check1.newrelic_nrql_alert_condition.bad_response_in_get_cities_api: Creation complete after 2s [id=48526:344284]
module.staging_check1.newrelic_nrql_alert_condition.bad_response_in_get_countries_api: Creation complete after 2s [id=48526:344285]
module.staging_check1.newrelic_alert_policy_channel.check_1: Creation complete after 2s [id=48526:128190:128191]

Apply complete! Resources: 11 added, 0 changed, 0 destroyed.


$ terraform init ./check2
$ terraform plan ./check2
$ terraform apply ./check2

module.staging_check1.newrelic_alert_policy_channel.check_1: Destroying... [id=48526:128190:128191]
module.staging_check1.newrelic_synthetics_monitor_script.check_1_script: Destroying... [id=0bb2b655-17e5-4507-82da-0895ced44d13]
module.staging_check2.newrelic_alert_policy.check_2: Creating...
module.staging_check2.newrelic_synthetics_monitor.check_2: Creating...
module.staging_check1.newrelic_nrql_alert_condition.bad_response_in_get_cities_api: Destroying... [id=48526:344284]
module.staging_check1.newrelic_nrql_alert_condition.bad_response_in_get_countries_api: Destroying... [id=48526:344285]
module.staging_check2.newrelic_alert_policy.check_2: Creation complete after 0s [id=48527]
module.staging_check2.newrelic_alert_policy_channel.check_2: Creating...
module.staging_check2.newrelic_nrql_alert_condition.wrong_leave_a_comment: Creating...
module.staging_check2.newrelic_nrql_alert_condition.wrong_blog_categories: Creating...
module.staging_check1.newrelic_nrql_alert_condition.bad_response_in_get_countries_api: Destruction complete after 0s
module.staging_check1.newrelic_nrql_alert_condition.bad_response_in_get_cities_api: Destruction complete after 1s
module.staging_check1.newrelic_synthetics_monitor_script.check_1_script: Destruction complete after 1s
module.staging_check1.newrelic_synthetics_monitor.check_1: Destroying... [id=0bb2b655-17e5-4507-82da-0895ced44d13]
module.staging_check2.newrelic_alert_policy_channel.check_2: Creation complete after 1s [id=48527:128190:128191]
module.staging_check2.newrelic_synthetics_monitor.check_2: Creation complete after 1s [id=7f3bc260-19bb-489d-b3e7-0f20ce63c364]
module.staging_check1.newrelic_alert_policy_channel.check_1: Destruction complete after 1s
module.staging_check2.newrelic_synthetics_monitor_script.check_2_script: Creating...
module.staging_check1.newrelic_alert_policy.check_1: Destroying... [id=48526]
module.staging_check2.newrelic_nrql_alert_condition.wrong_blog_categories: Creation complete after 1s [id=48527:344287]
module.staging_check2.newrelic_nrql_alert_condition.wrong_leave_a_comment: Creation complete after 1s [id=48527:344286]
module.staging_check1.newrelic_alert_policy.check_1: Destruction complete after 0s
module.staging_check1.newrelic_synthetics_monitor.check_1: Destruction complete after 1s
module.staging_check2.newrelic_synthetics_monitor_script.check_2_script: Creation complete after 1s [id=7f3bc260-19bb-489d-b3e7-0f20ce63c364]

Apply complete! Resources: 6 added, 0 changed, 6 destroyed.


Apply complete! Resources: 6 added, 0 changed, 6 destroyed.

$ terraform state list
module.common_resources.newrelic_alert_channel.andrea_email
module.common_resources.newrelic_alert_channel.aurelien_email
module.common_resources.newrelic_alert_channel.production_email
module.common_resources.newrelic_alert_channel.quality_assurance_email
module.common_resources.newrelic_alert_channel.quality_assurance_test_slack
module.staging_check2.data.template_file.check_2
module.staging_check2.newrelic_alert_policy.check_2
module.staging_check2.newrelic_alert_policy_channel.check_2
module.staging_check2.newrelic_nrql_alert_condition.wrong_blog_categories
module.staging_check2.newrelic_nrql_alert_condition.wrong_leave_a_comment
module.staging_check2.newrelic_synthetics_monitor.check_2
module.staging_check2.newrelic_synthetics_monitor_script.check_2_script

Note.
as far as I read it’s possible to apply a single directory or to use target option. I tried both but got the same behavior in both cases.

Could you please help me?
Best
Aurélien