Skip to content

Adding targets attribute#23

Merged
PhillypHenning merged 7 commits intomainfrom
target
Feb 17, 2023
Merged

Adding targets attribute#23
PhillypHenning merged 7 commits intomainfrom
target

Conversation

@PhillypHenning
Copy link
Copy Markdown
Contributor

Examples of use

bitops.config.yaml

terraform:
    cli:
        stack-action: apply
        source-target: aws_instance.server
    options: {}

Logs

+ [[ -n aws_instance.server ]]
+ SOURCE_TARGET='-target aws_instance.server'
+ echo 'Running Terraform Plan, Targetting: [aws_instance.server]'
Running Terraform Plan, Targetting: [aws_instance.server]
+ bash /opt/bitops/scripts/installed_plugins/terraform/scripts/terraform_plan.sh '' '-target aws_instance.server'
Running terraform plan...
terraform plan -target aws_instance.server 
+ echo 'Running Terraform Apply, Targetting: [aws_instance.server]'
Running Terraform Apply, Targetting: [aws_instance.server]
+ bash /opt/bitops/scripts/installed_plugins/terraform/scripts/terraform_apply.sh '' '-target aws_instance.server'
Running terraform apply...```

# Results
Enhancement performed exactly as required

@PhillypHenning
Copy link
Copy Markdown
Contributor Author

BitOps doc update to support change
link bitovi/bitops#395

@mickmcgrath13
Copy link
Copy Markdown
Contributor

can we do a list of targets?

@PhillypHenning
Copy link
Copy Markdown
Contributor Author

No, and I doubt it would be overly hard to implement. Will take a look

@PhillypHenning
Copy link
Copy Markdown
Contributor Author

Logs

+ '[' -n '' ']'
+ '[' -n 'aws_instance.server resource.resouce2' ']'
+ '[' plan == destroy ']'
+ '[' '' == true ']'
+ targets=($BITOPS_TF_TARGETS)
+ for target in "${targets[@]}"
+ SOURCE_TARGET='-target aws_instance.server'
+ echo 'Running Terraform Plan, Targetting: [aws_instance.server]'
Running Terraform Plan, Targetting: [aws_instance.server]
+ echo 'bash /opt/bitops/scripts/installed_plugins/terraform/scripts/terraform_plan.sh "" "-target aws_instance.server"'
bash /opt/bitops/scripts/installed_plugins/terraform/scripts/terraform_plan.sh "" "-target aws_instance.server"
+ echo 'Runnng Terraform Apply, Targetting: [aws_instance.server]'
Runnng Terraform Apply, Targetting: [aws_instance.server]
+ echo 'bash /opt/bitops/scripts/installed_plugins/terraform/scripts/terraform_apply.sh "" "-target aws_instance.server"'
bash /opt/bitops/scripts/installed_plugins/terraform/scripts/terraform_apply.sh "" "-target aws_instance.server"
+ for target in "${targets[@]}"
+ SOURCE_TARGET='-target resource.resouce2'
+ echo 'Running Terraform Plan, Targetting: [resource.resouce2]'
Running Terraform Plan, Targetting: [resource.resouce2]
+ echo 'bash /opt/bitops/scripts/installed_plugins/terraform/scripts/terraform_plan.sh "" "-target resource.resouce2"'
bash /opt/bitops/scripts/installed_plugins/terraform/scripts/terraform_plan.sh "" "-target resource.resouce2"
+ echo 'Runnng Terraform Apply, Targetting: [resource.resouce2]'
Runnng Terraform Apply, Targetting: [resource.resouce2]
+ echo 'bash /opt/bitops/scripts/installed_plugins/terraform/scripts/terraform_apply.sh "" "-target resource.resouce2"'
bash /opt/bitops/scripts/installed_plugins/terraform/scripts/terraform_apply.sh "" "-target resource.resouce2"

@PhillypHenning
Copy link
Copy Markdown
Contributor Author

PhillypHenning commented Feb 1, 2023

Testing results

If using terraform plan we expect to see the terraform exit normally.

Result

🟢

│ Error: Invalid count argument
│ 
│   on .terraform/modules/efs/main.tf line 135, in resource "aws_security_group" "this":
│  135:   count = var.create && var.create_security_group && length(var.mount_targets) > 0 ? 1 : 0
│ 
│ The "count" value depends on resource attributes that cannot be determined
│ until apply, so Terraform cannot predict how many instances will be
│ created. To work around this, use the -target argument to first apply only
│ the resources that the count depends on.

Multi resources

🟢
Above logs show an example of two resources being targetting prior to terraform apply

Actual use case

🟢

Running Terraform Plan, Targetting: [aws_instance.server]
+ bash /opt/bitops/scripts/installed_plugins/terraform/scripts/terraform_plan.sh '' '-target aws_instance.server'
Running terraform plan...
...
Note: You didn't use the -out option to save this plan, so Terraform can't
guarantee to take exactly these actions if you run "terraform apply" now.
+ '[' apply '!=' plan ']'
+ echo 'Runnng Terraform Apply, Targetting: [aws_instance.server]'
Runnng Terraform Apply, Targetting: [aws_instance.server]
+ bash /opt/bitops/scripts/installed_plugins/terraform/scripts/terraform_apply.sh '' '-target aws_instance.server'
Running terraform apply...
data.aws_ami.ubuntu: Reading...
data.aws_ami.ubuntu: Read complete after 0s [id=ami-0dae3a932d090b3de]

Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # aws_iam_instance_profile.ec2_profile will be created
  + resource "aws_iam_instance_profile" "ec2_profile" {
      + arn         = (known after apply)
      + create_date = (known after apply)
      + id          = (known after apply)
      + name        = "github-action-deploy-nfs-testing"
      + path        = "/"
      + role        = "github-action-deploy-nfs-testing"
      + tags_all    = {
          + "AWSResourceIdentifier"     = "github-action-deploy-nfs-testing"
          + "GitHubAction"              = "bitovi/github-actions-node-app-to-aws-vm"
          + "GitHubBranchName"          = (known after apply)
          + "GitHubOrgName"             = "github-actions-deploy-stackstorm-testing"
          + "GitHubRepoName"            = "github-actions-deploy-stackstorm-testing"
          + "OperationsRepo"            = "bitovi/github-actions-node-app-to-aws-vm/operations/deployment"
          + "OperationsRepoEnvironment" = "deployment"
          + "created_with"              = "terraform"
        }
      + unique_id   = (known after apply)
    }

  # aws_iam_role.ec2_role will be created
  + resource "aws_iam_role" "ec2_role" {
      + arn                   = (known after apply)
      + assume_role_policy    = jsonencode(
            {
              + Statement = [
                  + {
                      + Action    = "sts:AssumeRole"
                      + Effect    = "Allow"
                      + Principal = {
                          + Service = "ec2.amazonaws.com"
                        }
                      + Sid       = ""
                    },
                ]
              + Version   = "2012-10-17"
            }
        )
      + create_date           = (known after apply)
      + force_detach_policies = false
      + id                    = (known after apply)
      + managed_policy_arns   = (known after apply)
      + max_session_duration  = 3600
      + name                  = "github-action-deploy-nfs-testing"
      + name_prefix           = (known after apply)
      + path                  = "/"
      + tags_all              = {
          + "AWSResourceIdentifier"     = "github-action-deploy-nfs-testing"
          + "GitHubAction"              = "bitovi/github-actions-node-app-to-aws-vm"
          + "GitHubBranchName"          = (known after apply)
          + "GitHubOrgName"             = "github-actions-deploy-stackstorm-testing"
          + "GitHubRepoName"            = "github-actions-deploy-stackstorm-testing"
          + "OperationsRepo"            = "bitovi/github-actions-node-app-to-aws-vm/operations/deployment"
          + "OperationsRepoEnvironment" = "deployment"
          + "created_with"              = "terraform"
        }
      + unique_id             = (known after apply)

      + inline_policy {
          + name   = (known after apply)
          + policy = (known after apply)
        }
    }

  # aws_instance.server will be created
  + resource "aws_instance" "server" {
      + ami                                  = "ami-0dae3a932d090b3de"
      + arn                                  = (known after apply)
      + associate_public_ip_address          = true
      + 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                 = "github-action-deploy-nfs-testing"
      + id                                   = (known after apply)
      + instance_initiated_shutdown_behavior = (known after apply)
      + instance_state                       = (known after apply)
      + instance_type                        = "t2.small"
      + ipv6_address_count                   = (known after apply)
      + ipv6_addresses                       = (known after apply)
      + key_name                             = "github-action-deploy-nfs-testing"
      + monitoring                           = true
      + 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                      = [
          + "github-action-deploy-nfs-testing",
        ]
      + source_dest_check                    = true
      + subnet_id                            = (known after apply)
      + tags                                 = {
          + "Name" = "github-action-deploy-nfs-testing - Instance"
        }
      + tags_all                             = {
          + "AWSResourceIdentifier"     = "github-action-deploy-nfs-testing"
          + "GitHubAction"              = "bitovi/github-actions-node-app-to-aws-vm"
          + "GitHubBranchName"          = (known after apply)
          + "GitHubOrgName"             = "github-actions-deploy-stackstorm-testing"
          + "GitHubRepoName"            = "github-actions-deploy-stackstorm-testing"
          + "Name"                      = "github-action-deploy-nfs-testing - Instance"
          + "OperationsRepo"            = "bitovi/github-actions-node-app-to-aws-vm/operations/deployment"
          + "OperationsRepoEnvironment" = "deployment"
          + "created_with"              = "terraform"
        }
      + 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)

      + capacity_reservation_specification {
          + capacity_reservation_preference = (known after apply)

          + capacity_reservation_target {
              + capacity_reservation_id                 = (known after apply)
              + capacity_reservation_resource_group_arn = (known after apply)
            }
        }

      + ebs_block_device {
          + delete_on_termination = (known after apply)
          + device_name           = (known after apply)
          + encrypted             = (known after apply)
          + iops                  = (known after apply)
          + kms_key_id            = (known after apply)
          + snapshot_id           = (known after apply)
          + tags                  = (known after apply)
          + throughput            = (known after apply)
          + volume_id             = (known after apply)
          + volume_size           = (known after apply)
          + volume_type           = (known after apply)
        }

      + enclave_options {
          + enabled = (known after apply)
        }

      + ephemeral_block_device {
          + device_name  = (known after apply)
          + no_device    = (known after apply)
          + virtual_name = (known after apply)
        }

      + maintenance_options {
          + auto_recovery = (known after apply)
        }

      + metadata_options {
          + http_endpoint               = (known after apply)
          + http_put_response_hop_limit = (known after apply)
          + http_tokens                 = (known after apply)
          + instance_metadata_tags      = (known after apply)
        }

      + network_interface {
          + delete_on_termination = (known after apply)
          + device_index          = (known after apply)
          + network_card_index    = (known after apply)
          + network_interface_id  = (known after apply)
        }

      + private_dns_name_options {
          + enable_resource_name_dns_a_record    = (known after apply)
          + enable_resource_name_dns_aaaa_record = (known after apply)
          + hostname_type                        = (known after apply)
        }

      + root_block_device {
          + delete_on_termination = (known after apply)
          + device_name           = (known after apply)
          + encrypted             = (known after apply)
          + iops                  = (known after apply)
          + kms_key_id            = (known after apply)
          + tags                  = (known after apply)
          + throughput            = (known after apply)
          + volume_id             = (known after apply)
          + volume_size           = (known after apply)
          + volume_type           = (known after apply)
        }
    }

  # aws_key_pair.aws_key will be created
  + resource "aws_key_pair" "aws_key" {
      + arn             = (known after apply)
      + fingerprint     = (known after apply)
      + id              = (known after apply)
      + key_name        = "github-action-deploy-nfs-testing"
      + key_name_prefix = (known after apply)
      + key_pair_id     = (known after apply)
      + key_type        = (known after apply)
      + public_key      = (known after apply)
      + tags_all        = {
          + "AWSResourceIdentifier"     = "github-action-deploy-nfs-testing"
          + "GitHubAction"              = "bitovi/github-actions-node-app-to-aws-vm"
          + "GitHubBranchName"          = (known after apply)
          + "GitHubOrgName"             = "github-actions-deploy-stackstorm-testing"
          + "GitHubRepoName"            = "github-actions-deploy-stackstorm-testing"
          + "OperationsRepo"            = "bitovi/github-actions-node-app-to-aws-vm/operations/deployment"
          + "OperationsRepoEnvironment" = "deployment"
          + "created_with"              = "terraform"
        }
    }

  # aws_security_group.ec2_security_group will be created
  + resource "aws_security_group" "ec2_security_group" {
      + arn                    = (known after apply)
      + description            = "SG for github-action-deploy-nfs-testing"
      + egress                 = [
          + {
              + cidr_blocks      = [
                  + "0.0.0.0/0",
                ]
              + description      = ""
              + from_port        = 0
              + ipv6_cidr_blocks = []
              + prefix_list_ids  = []
              + protocol         = "-1"
              + security_groups  = []
              + self             = false
              + to_port          = 0
            },
        ]
      + id                     = (known after apply)
      + ingress                = (known after apply)
      + name                   = "github-action-deploy-nfs-testing"
      + name_prefix            = (known after apply)
      + owner_id               = (known after apply)
      + revoke_rules_on_delete = false
      + tags                   = {
          + "Name" = "github-action-deploy-nfs-testing"
        }
      + tags_all               = {
          + "AWSResourceIdentifier"     = "github-action-deploy-nfs-testing"
          + "GitHubAction"              = "bitovi/github-actions-node-app-to-aws-vm"
          + "GitHubBranchName"          = (known after apply)
          + "GitHubOrgName"             = "github-actions-deploy-stackstorm-testing"
          + "GitHubRepoName"            = "github-actions-deploy-stackstorm-testing"
          + "Name"                      = "github-action-deploy-nfs-testing"
          + "OperationsRepo"            = "bitovi/github-actions-node-app-to-aws-vm/operations/deployment"
          + "OperationsRepoEnvironment" = "deployment"
          + "created_with"              = "terraform"
        }
      + vpc_id                 = (known after apply)
    }

  # tls_private_key.key will be created
  + resource "tls_private_key" "key" {
      + algorithm                     = "RSA"
      + ecdsa_curve                   = "P224"
      + id                            = (known after apply)
      + private_key_openssh           = (sensitive value)
      + private_key_pem               = (sensitive value)
      + private_key_pem_pkcs8         = (sensitive value)
      + public_key_fingerprint_md5    = (known after apply)
      + public_key_fingerprint_sha256 = (known after apply)
      + public_key_openssh            = (known after apply)
      + public_key_pem                = (known after apply)
      + rsa_bits                      = 4096
    }

Plan: 6 to add, 0 to change, 0 to destroy.

@PhillypHenning PhillypHenning force-pushed the target branch 4 times, most recently from ea9ee6c to 5a42d05 Compare February 1, 2023 23:44
@PhillypHenning PhillypHenning changed the title Adding source_target Adding targets attribute Feb 1, 2023
export_env: TF_TARGET
targets:
type: list
parameter: targets
Copy link
Copy Markdown
Member

@arm4b arm4b Feb 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
parameter: targets
parameter: target

The key name is targets, but the parameter name should be target, as in CLI it's -target A -target B -target C

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh I didn't realize that was the intended usage of that property.

Let me update quickly

@PhillypHenning PhillypHenning merged commit a36aa70 into main Feb 17, 2023
@arm4b arm4b deleted the target branch February 20, 2023 23:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants