variable "ebs_encryption" { type = bool validation { condition = var.ebs_encryption == true error_message = "EBS Should Be Encrypted" } description = "A Boolean of EBS Encryption in the EC2 Instances" }
╷ │ Error: Invalid value for variable │ │ on variables.tf line 1: │ 1: variable "ebs_encryption" { │ ├──────────────── │ │ var.ebs_encryption is false │ │ EBS Should Be Encrypted │ │ This was checked by the validation rule at variables.tf:3,3-13.
╷ │ Error: Resource precondition failed │ │ on main.tf line 18, in resource "aws_instance""example": │ 18: condition = data.aws_ec2_instance_type.example.free_tier_eligible │ ├──────────────── │ │ data.aws_ec2_instance_type.example.free_tier_eligible is false │ │ This instance type is not free in AWS
lifecycle { postcondition { condition = length(self.availability_zones) > 1 error_message = "You need to choose more than 1 AZ to ensure high availability" } } }
planをしてみましょう。この時点でAZはknown after applyとあるので、エラーは捕捉されずにplan自体は通ります。
Terraform will perform the following actions:
# aws_autoscaling_group.example will be created + resource "aws_autoscaling_group""example" { + arn = (known after apply) + availability_zones = (known after apply) + default_cooldown = (known after apply) + desired_capacity = (known after apply) + force_delete = false + force_delete_warm_pool = false + health_check_grace_period = 300 + health_check_type = (known after apply) + id = (known after apply) + ignore_failed_scaling_activities = false + launch_configuration = (known after apply) + load_balancers = (known after apply) + max_size = 1 + metrics_granularity = "1Minute" + min_size = 1 + name = "ASG" + name_prefix = (known after apply) + predicted_capacity = (known after apply) + protect_from_scale_in = false + service_linked_role_arn = (known after apply) + target_group_arns = (known after apply) + vpc_zone_identifier = [ + "subnet-xxxxxxx", ] + wait_for_capacity_timeout = "10m" + warm_pool_size = (known after apply) }
# aws_launch_configuration.example will be created + resource "aws_launch_configuration""example" { + arn = (known after apply) + associate_public_ip_address = (known after apply) + ebs_optimized = (known after apply) + enable_monitoring = true + id = (known after apply) + image_id = "ami-020283e959651b381" + instance_type = "t2.micro" + key_name = (known after apply) + name = (known after apply) + name_prefix = (known after apply) }
Plan: 2 to add, 0 to change, 0 to destroy.
この状態でapplyすると完了後に下記のエラーが出ます。
╷ │ Error: Resource postcondition failed │ │ on main.tf line 38, in resource "aws_autoscaling_group""example": │ 38: condition = length(self.availability_zones) > 1 │ ├──────────────── │ │ self.availability_zones is set of string with 1 element │ │ You need to choose more than 1 AZ to ensure high availability
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: + create
Terraform planned the following actions, but then encountered a problem:
# aws_instance.example will be created + resource "aws_instance""example" { + ami = "ami-0f7b55661ecbbe44c" + arn = (known after apply) + associate_public_ip_address = (known after apply) + availability_zone = (known after apply) + cpu_core_count = (known after apply) + cpu_threads_per_core = (known after apply) + disable_api_stop = (known after apply) + disable_api_termination = (known after apply) + ebs_optimized = (known after apply) + get_password_data = false + host_id = (known after apply) + host_resource_group_arn = (known after apply) + iam_instance_profile = (known after apply) + id = (known after apply) + instance_initiated_shutdown_behavior = (known after apply) + instance_lifecycle = (known after apply) + instance_state = (known after apply) + instance_type = "c5.xlarge" + ipv6_address_count = (known after apply) + ipv6_addresses = (known after apply) + key_name = (known after apply) + monitoring = (known after apply) + outpost_arn = (known after apply) + password_data = (known after apply) + placement_group = (known after apply) + placement_partition_number = (known after apply) + primary_network_interface_id = (known after apply) + private_dns = (known after apply) + private_ip = (known after apply) + public_dns = (known after apply) + public_ip = (known after apply) + secondary_private_ips = (known after apply) + security_groups = (known after apply) + source_dest_check = true + spot_instance_request_id = (known after apply) + subnet_id = "subnet-xxxxxxx" + tags_all = (known after apply) + tenancy = (known after apply) + user_data = (known after apply) + user_data_base64 = (known after apply) + user_data_replace_on_change = false + vpc_security_group_ids = (known after apply) }
Plan: 1 to add, 0 to change, 0 to destroy. ╷ │ Error: Resource postcondition failed │ │ on main.tf line 11, in resource "aws_instance""example": │ 11: condition = data.aws_ec2_instance_type.example.free_tier_eligible │ ├──────────────── │ │ data.aws_ec2_instance_type.example.free_tier_eligible is false │ │ This instance type is not free in AWS
エラーメッセージはpreconditionとほぼ変わらないのですが、postconditionでは plan によるリソース出力が成功します(preconditionではplanが失敗してリソース出力がされませんでした)。