Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug when passing EC2 instance from module to CloudWatch metric module #12770

Open
ghost opened this issue Apr 10, 2020 · 1 comment
Open

Bug when passing EC2 instance from module to CloudWatch metric module #12770

ghost opened this issue Apr 10, 2020 · 1 comment
Labels
service/cloudwatch Issues and PRs that pertain to the cloudwatch service. service/ec2 Issues and PRs that pertain to the ec2 service. service/sns Issues and PRs that pertain to the sns service. waiting-response Maintainers are waiting on response from community or contributor.

Comments

@ghost
Copy link

ghost commented Apr 10, 2020

This issue was originally opened by @hbashary as hashicorp/terraform#24637. It was migrated here as a result of the provider split. The original body of the issue is below.


Terraform Version

Terraform v0.12.13

Terraform Configuration Files

module "seg-01-ec2-instance" {
  source                 = "../../modules/ec2"
  segment                = "test1"
  subnet_tag_name        = "*1a-private*"
  kms_alias              = "alias/sf-shoppers-test-us-east-1-master-kmskey"
  ec2_service_role       = "${var.ec2-instance-config["ec2_service_role"]}"
  ami                    = "${var.ec2-instance-config["ami"]}"
  instance_type          = "${var.ec2-instance-config["instance_type"]}"
  key_name               = "${var.ec2-instance-config["key_name"]}"
  security_groups        = concat("${data.aws_security_groups.shop-data-proxy-sg.ids}",
                                  "${data.aws_security_groups.shop-data-ec2-sg.ids}")
  vpc_security_group_ids = "${data.aws_security_groups.shop-data-base-sg.ids}"
  volume_size            = "${var.root-block-device-config["volume_size"]}"
  volume_type            = "${var.root-block-device-config["volume_type"]}"
  user_data              = "${file("user-data.sh")}"

  tags = {
    Name               = "${var.tags-config["Name"]}"
    owner              = "${var.tags-config["owner"]}"
    contact            = "${var.tags-config["contact"]}"
    cost-center        = "${var.tags-config["cost-center"]}"
    off-hours-shutdown = "${var.tags-config["off-hours-shutdown"]}"
    region             = "${var.tags-config["region"]}"
    ou                 = "${var.tags-config["ou"]}"
    env                = "${var.tags-config["env"]}"
    deployment         = "${var.tags-config["deployment"]}"
  }
}

module "seg-01-ec2-sns-alarms" {
    source              = "../../modules/sns-alarms"
    topic_name          = "${var.ec2-alarm-config["topic_name"]}"
    alarm_cnt           = "${var.ec2-alarm-config["alarm_cnt"]}"
    alarm_name          = "${var.ec2-alarm-config["alarm_name"]}"
    comparison_operator = "${var.ec2-alarm-config["comparison_operator"]}"
    evaluation_periods  = "${var.ec2-alarm-config["evaluation_periods"]}"
    metric_name         = "${var.ec2-alarm-config["metric_name"]}"
    namespace           = "${var.ec2-alarm-config["namespace"]}"
    period              = "${var.ec2-alarm-config["period"]}"
    statistic           = "${var.ec2-alarm-config["statistic"]}"
    unit                = "${var.ec2-alarm-config["unit"]}"
    threshold           = "${var.ec2-alarm-config["threshold"]}"
    alarm_description   = "${var.ec2-alarm-config["alarm_description"]}"

    dimensions          = {
      InstanceId = "${module.seg-01-ec2-instance[0].id}"
    }
}

# ------- EC2 Module 
# -------------------------------------------------------
# Resource      : "aws_instance" "shop-res-instance"
# Description   : Provision EC2 Instance
# -------------------------------------------------------
resource "aws_instance" "shop-res-instance" {
  count = "${length(data.aws_subnet_ids.shop-data-subnets.ids)}"

  ami                    = "${var.ami}"
  instance_type          = "${var.instance_type}"
  subnet_id              = "${element(tolist(data.aws_subnet_ids.shop-data-subnets.ids), count.index)}"
  iam_instance_profile   = "${data.aws_iam_role.shop-ec2-service-role.name}"
  key_name               = "${var.key_name}"
  security_groups        = "${var.security_groups}"
  vpc_security_group_ids = "${var.vpc_security_group_ids}"

  root_block_device {
    delete_on_termination = true
    encrypted             = true
    kms_key_id            = data.aws_kms_alias.shop-data-kms.arn
    volume_size           = "${var.volume_size}"
    volume_type           = "${var.volume_type}"
  }

  user_data              = "${var.user_data}"
  tags                   = "${var.tags}"
}

# Output EC2 instance id
output "id" {
  description = "Output EC2 instance id"
  value       = "${aws_instance.shop-res-instance[*].id}"
}

# Output EC2 private ip
output "private_ip" {
  description = "Output EC2 private ip"
  value       = "${aws_instance.shop-res-instance[*].private_ip}"
}


# -------CloudWatch Module
# Data source to get sns topic arn
data "aws_sns_topic" "shop-data-sns-topic" {
  name = "${var.topic_name}"
}

# ----------------------------------------------------------------------------------
# Resource      : "aws_cloudwatch_metric_alarm" "shop-res-cloudwatch-metric-alarm"
# Description   : Create Cloudwatch metric alarms
# ----------------------------------------------------------------------------------
resource "aws_cloudwatch_metric_alarm" "shop-res-cloudwatch-metric-alarm" {
  count                     = "${var.alarm_cnt}"

  alarm_name                = "${element(var.alarm_name, count.index)}"
  comparison_operator       = "${element(var.comparison_operator, count.index)}"
  evaluation_periods        = "${element(var.evaluation_periods, count.index)}"
  metric_name               = "${element(var.metric_name, count.index)}"
  namespace                 = "${var.namespace}"
  period                    = "${element(var.period, count.index)}"
  statistic                 = "${element(var.statistic, count.index)}"
  unit                      = "${element(var.unit, count.index)}"
  threshold                 = "${element(var.threshold, count.index)}"
  alarm_description         = "${element(var.alarm_description, count.index)}"
  alarm_actions             = [ "${data.aws_sns_topic.shop-data-sns-topic.arn}" ]

  dimensions                = "${var.dimensions}"
}

Debug Output

module.seg-01-ebs-volumes.aws_ebs_volume.shop-res-ebs-volume[0]: Creating...
module.seg-01-ebs-volumes.aws_ebs_volume.shop-res-ebs-volume[1]: Creating...
module.seg-01-ec2-sns-alarms.aws_cloudwatch_metric_alarm.shop-res-cloudwatch-metric-alarm[0]: Creating...
module.seg-01-ec2-instance.aws_instance.shop-res-instance[0]: Creating...
module.seg-01-ebs-volumes.aws_ebs_volume.shop-res-ebs-volume[0]: Still creating... [10s elapsed]
module.seg-01-ebs-volumes.aws_ebs_volume.shop-res-ebs-volume[1]: Still creating... [10s elapsed]
module.seg-01-ec2-sns-alarms.aws_cloudwatch_metric_alarm.shop-res-cloudwatch-metric-alarm[0]: Still creating... [10s elapsed]
module.seg-01-ec2-instance.aws_instance.shop-res-instance[0]: Still creating... [10s elapsed]
module.seg-01-ebs-volumes.aws_ebs_volume.shop-res-ebs-volume[1]: Creation complete after 13s [id=vol-0d94d462564708003]
module.seg-01-ebs-volumes.aws_ebs_volume.shop-res-ebs-volume[0]: Creation complete after 13s [id=vol-080ca25d31a2b2969]
module.seg-01-ec2-sns-alarms.aws_cloudwatch_metric_alarm.shop-res-cloudwatch-metric-alarm[0]: Still creating... [20s elapsed]
module.seg-01-ec2-instance.aws_instance.shop-res-instance[0]: Still creating... [20s elapsed]
module.seg-01-ec2-sns-alarms.aws_cloudwatch_metric_alarm.shop-res-cloudwatch-metric-alarm[0]: Still creating... [30s elapsed]
module.seg-01-ec2-instance.aws_instance.shop-res-instance[0]: Still creating... [30s elapsed]
module.seg-01-ec2-instance.aws_instance.shop-res-instance[0]: Creation complete after 31s [id=i-04304990a50a35c25]
module.seg-01-ebs-volumes.aws_volume_attachment.shop-res-ebs-attach[1]: Creating...
module.seg-01-ebs-volumes.aws_volume_attachment.shop-res-ebs-attach[0]: Creating...
module.seg-01-ec2-instance-record-set.aws_route53_record.shop-res-route53-record: Creating...
module.seg-01-ec2-sns-alarms.aws_cloudwatch_metric_alarm.shop-res-cloudwatch-metric-alarm[0]: Still creating... [40s elapsed]
module.seg-01-ebs-volumes.aws_volume_attachment.shop-res-ebs-attach[1]: Still creating... [10s elapsed]
module.seg-01-ebs-volumes.aws_volume_attachment.shop-res-ebs-attach[0]: Still creating... [10s elapsed]
module.seg-01-ec2-instance-record-set.aws_route53_record.shop-res-route53-record: Still creating... [10s elapsed]
module.seg-01-ec2-sns-alarms.aws_cloudwatch_metric_alarm.shop-res-cloudwatch-metric-alarm[0]: Still creating... [50s elapsed]
module.seg-01-ebs-volumes.aws_volume_attachment.shop-res-ebs-attach[1]: Still creating... [20s elapsed]
module.seg-01-ec2-instance-record-set.aws_route53_record.shop-res-route53-record: Still creating... [20s elapsed]
module.seg-01-ebs-volumes.aws_volume_attachment.shop-res-ebs-attach[0]: Still creating... [20s elapsed]
module.seg-01-ebs-volumes.aws_volume_attachment.shop-res-ebs-attach[0]: Creation complete after 24s [id=vai-1117887475]
module.seg-01-ebs-volumes.aws_volume_attachment.shop-res-ebs-attach[1]: Creation complete after 24s [id=vai-313197564]
module.seg-01-ec2-sns-alarms.aws_cloudwatch_metric_alarm.shop-res-cloudwatch-metric-alarm[0]: Still creating... [1m0s elapsed]
module.seg-01-ec2-instance-record-set.aws_route53_record.shop-res-route53-record: Still creating... [30s elapsed]
module.seg-01-ec2-sns-alarms.aws_cloudwatch_metric_alarm.shop-res-cloudwatch-metric-alarm[0]: Still creating... [1m10s elapsed]
module.seg-01-ec2-instance-record-set.aws_route53_record.shop-res-route53-record: Still creating... [40s elapsed]
module.seg-01-ec2-sns-alarms.aws_cloudwatch_metric_alarm.shop-res-cloudwatch-metric-alarm[0]: Still creating... [1m20s elapsed]
module.seg-01-ec2-instance-record-set.aws_route53_record.shop-res-route53-record: Still creating... [50s elapsed]
module.seg-01-ec2-sns-alarms.aws_cloudwatch_metric_alarm.shop-res-cloudwatch-metric-alarm[0]: Still creating... [1m30s elapsed]
module.seg-01-ec2-instance-record-set.aws_route53_record.shop-res-route53-record: Still creating... [1m0s elapsed]
module.seg-01-ec2-instance-record-set.aws_route53_record.shop-res-route53-record: Creation complete after 1m8s [id=Z04286331F61LJQQ4X1W4_discovery.shoppers.test.ic1.statefarm._A]

Error: configuration for module.seg-01-ec2-sns-alarms.aws_cloudwatch_metric_alarm.shop-res-cloudwatch-metric-alarm[0] still contains unknown values during apply (this is a bug in Terraform; please report it!)

Crash Output

Expected Behavior

A CloudWatch alert should have been created.

Actual Behavior

EC2 instance gets created but no Cloudwatch alert.

Steps to Reproduce

  1. terraform init
  2. terraform apply

Additional Context

References

@ghost ghost added service/cloudwatch Issues and PRs that pertain to the cloudwatch service. service/ec2 Issues and PRs that pertain to the ec2 service. service/sns Issues and PRs that pertain to the sns service. labels Apr 10, 2020
@github-actions github-actions bot added the needs-triage Waiting for first response or review from a maintainer. label Apr 10, 2020
@justinretzolk
Copy link
Member

Hey @hbashary 👋 Thank you for taking the time to file this. Given that there's been a number of AWS Provider releases since you initially filed it, can you confirm if you're still experiencing this behavior?

@justinretzolk justinretzolk added waiting-response Maintainers are waiting on response from community or contributor. and removed needs-triage Waiting for first response or review from a maintainer. labels Oct 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
service/cloudwatch Issues and PRs that pertain to the cloudwatch service. service/ec2 Issues and PRs that pertain to the ec2 service. service/sns Issues and PRs that pertain to the sns service. waiting-response Maintainers are waiting on response from community or contributor.
Projects
None yet
Development

No branches or pull requests

1 participant