|
| 1 | +variables { |
| 2 | + vpc_id = "vpc-12345678" |
| 3 | + subnet_ids = ["subnet-12345678", "subnet-87654321"] |
| 4 | + stage = "test" |
| 5 | + namespace = "mp" |
| 6 | + name = "ssm-agent" |
| 7 | + region = "us-east-1" |
| 8 | + availability_zones = ["us-east-1a"] |
| 9 | + nat_gateway_enabled = true |
| 10 | + ipv6_enabled = true |
| 11 | +} |
| 12 | + |
| 13 | +run "verify_session_logging" { |
| 14 | + command = plan |
| 15 | + |
| 16 | + variables { |
| 17 | + session_logging_enabled = true |
| 18 | + session_logging_bucket_name = "" |
| 19 | + session_logging_encryption_enabled = true |
| 20 | + cloudwatch_retention_in_days = 365 |
| 21 | + } |
| 22 | + |
| 23 | + assert { |
| 24 | + condition = aws_cloudwatch_log_group.session_logging[0].retention_in_days == 365 |
| 25 | + error_message = "CloudWatch log retention days not set correctly when variables passed in." |
| 26 | + } |
| 27 | + |
| 28 | + assert { |
| 29 | + condition = length(aws_iam_role_policy.session_logging) > 0 |
| 30 | + error_message = "Session logging IAM policy not created when variables passed in." |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +run "verify_launch_template" { |
| 35 | + command = plan |
| 36 | + |
| 37 | + variables { |
| 38 | + instance_type = "c6g.nano" |
| 39 | + monitoring_enabled = true |
| 40 | + associate_public_ip_address = false |
| 41 | + metadata_imdsv2_enabled = true |
| 42 | + namespace = "mp" |
| 43 | + stage = "test" |
| 44 | + name = "ssm-agent" |
| 45 | + } |
| 46 | + |
| 47 | + assert { |
| 48 | + condition = aws_launch_template.default.instance_type == "c6g.nano" |
| 49 | + error_message = "Launch template instance type does not match" |
| 50 | + } |
| 51 | + |
| 52 | + assert { |
| 53 | + condition = aws_launch_template.default.monitoring[0].enabled == true |
| 54 | + error_message = "Instance monitoring not enabled" |
| 55 | + } |
| 56 | + |
| 57 | + assert { |
| 58 | + condition = aws_launch_template.default.metadata_options[0].http_tokens == "required" |
| 59 | + error_message = "IMDSv2 not enforced in launch template" |
| 60 | + } |
| 61 | + |
| 62 | + assert { |
| 63 | + condition = aws_launch_template.default.iam_instance_profile[0].name == "mp-test-ssm-agent-role" |
| 64 | + error_message = "IAM instance profile name does not match expected value" |
| 65 | + } |
| 66 | + |
| 67 | + assert { |
| 68 | + condition = aws_launch_template.default.iam_instance_profile[0].name == aws_iam_instance_profile.default.name |
| 69 | + error_message = "Launch template IAM instance profile name does not match the created instance profile" |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +run "verify_autoscaling_group" { |
| 74 | + command = plan |
| 75 | + |
| 76 | + variables { |
| 77 | + max_size = 2 |
| 78 | + min_size = 1 |
| 79 | + desired_capacity = 1 |
| 80 | + subnet_ids = ["subnet-12345678"] |
| 81 | + } |
| 82 | + |
| 83 | + assert { |
| 84 | + condition = aws_autoscaling_group.default.max_size == 2 |
| 85 | + error_message = "ASG max size not set correctly" |
| 86 | + } |
| 87 | + |
| 88 | + assert { |
| 89 | + condition = aws_autoscaling_group.default.min_size == 1 |
| 90 | + error_message = "ASG min size not set correctly" |
| 91 | + } |
| 92 | + |
| 93 | + assert { |
| 94 | + condition = aws_autoscaling_group.default.desired_capacity == 1 |
| 95 | + error_message = "ASG desired capacity not set correctly" |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | + |
| 100 | +run "verify_s3_bucket_configuration" { |
| 101 | + command = plan |
| 102 | + |
| 103 | + variables { |
| 104 | + session_logging_enabled = true |
| 105 | + } |
| 106 | + |
| 107 | + assert { |
| 108 | + condition = module.logs_bucket.enabled == true |
| 109 | + error_message = "S3 bucket session logging bucket isn't enabled when set to enabled." |
| 110 | + } |
| 111 | +} |
0 commit comments