brikis98 / terraform-up-and-running-code

Code samples for the book "Terraform: Up & Running" by Yevgeniy Brikman
http://www.terraformupandrunning.com/
MIT License
2.93k stars 1.93k forks source link

Chapter 2, 3rd Edition: one-webserver ended up with 2 running instances #90

Open mnguyen92646 opened 1 year ago

mnguyen92646 commented 1 year ago

Chapter 2, 3rd Edition: Finished one-server, added a custom tag, tf applied okay

In the next section, one-webserver, the book examples show that it should destroy the old instance so that it can build a new one and run the EOF userdata section, but tf plan and apply ended up creating a 2nd instance instead of doing anything with the existing instance.

Not sure if it is user error (certainly not ruling that out as i'm just starting to learn), but I don't think I strayed from the book.

Console output in next comment

mnguyen92646 commented 1 year ago

MNMBP:one-server minguyen$ terraform apply

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

Terraform will perform the following actions:

aws_instance.example will be created

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

Do you want to perform these actions? Terraform will perform the actions described above. Only 'yes' will be accepted to approve.

Enter a value: yes

aws_instance.example: Creating... aws_instance.example: Still creating... [10s elapsed] aws_instance.example: Still creating... [20s elapsed] aws_instance.example: Creation complete after 22s [id=i-0bb3c2191c397dff2]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed. MNMBP:one-server minguyen$ terraform apply aws_instance.example: Refreshing state... [id=i-0bb3c2191c397dff2]

No changes. Your infrastructure matches the configuration.

Terraform has compared your real infrastructure against your configuration and found no differences, so no changes are needed.

Apply complete! Resources: 0 added, 0 changed, 0 destroyed. MNMBP:one-server minguyen$ terraform apply aws_instance.example: Refreshing state... [id=i-0bb3c2191c397dff2]

No changes. Your infrastructure matches the configuration.

Terraform has compared your real infrastructure against your configuration and found no differences, so no changes are needed.

Apply complete! Resources: 0 added, 0 changed, 0 destroyed. MNMBP:one-server minguyen$ terraform plan

^CStopping operation...

Interrupt received. Please wait for Terraform to exit or data loss may occur. Gracefully shutting down...

╷ │ Error: error configuring Terraform AWS Provider: error validating provider credentials: error calling sts:GetCallerIdentity: operation error STS: GetCallerIdentity, request canceled, context canceled │ │ with provider["registry.terraform.io/hashicorp/aws"], │ on main.tf line 12, in provider "aws": │ 12: provider "aws" { │ ╵ MNMBP:one-server minguyen$ terraform plan ^C Interrupt received. Please wait for Terraform to exit or data loss may occur. Gracefully shutting down...

Stopping operation... ╷ │ Error: error configuring Terraform AWS Provider: error validating provider credentials: error calling sts:GetCallerIdentity: operation error STS: GetCallerIdentity, request canceled, context canceled │ │ with provider["registry.terraform.io/hashicorp/aws"], │ on main.tf line 12, in provider "aws": │ 12: provider "aws" { │ ╵ MNMBP:one-server minguyen$ terraform plan aws_instance.example: Refreshing state... [id=i-0bb3c2191c397dff2]

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: ~ update in-place

Terraform will perform the following actions:

aws_instance.example will be updated in-place

~ resource "aws_instance" "example" { id = "i-0bb3c2191c397dff2" ~ tags = {

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

────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

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. MNMBP:one-server minguyen$ terraform apply aws_instance.example: Refreshing state... [id=i-0bb3c2191c397dff2]

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: ~ update in-place

Terraform will perform the following actions:

aws_instance.example will be updated in-place

~ resource "aws_instance" "example" { id = "i-0bb3c2191c397dff2" ~ tags = {

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

Do you want to perform these actions? Terraform will perform the actions described above. Only 'yes' will be accepted to approve.

Enter a value: yes

aws_instance.example: Modifying... [id=i-0bb3c2191c397dff2] aws_instance.example: Modifications complete after 2s [id=i-0bb3c2191c397dff2]

Apply complete! Resources: 0 added, 1 changed, 0 destroyed. MNMBP:one-server minguyen$ git init Initialized empty Git repository in /Users/minguyen/Documents/Personal/tf-up-and-running/terraform-up-and-running-code-master/code/terraform/02-intro-to-terraform-syntax/one-server/.git/ MNMBP:one-server minguyen$ git add main.tf .terraform.lock.hcl MNMBP:one-server minguyen$ git commit -m "Initial commit

" [main (root-commit) 8c5559d] Initial commit 2 files changed, 50 insertions(+) create mode 100644 .terraform.lock.hcl create mode 100644 main.tf MNMBP:one-server minguyen$ #!/bin/bash MNMBP:one-server minguyen$ cd .. MNMBP:02-intro-to-terraform-syntax minguyen$ ls one-server one-webserver one-webserver-with-vars webserver-cluster MNMBP:02-intro-to-terraform-syntax minguyen$ cd one-webserver MNMBP:one-webserver minguyen$ ls README.md main.tf MNMBP:one-webserver minguyen$ terraform plan ╷ │ Error: Required plugins are not installed │ │ The installed provider plugins are not consistent with the packages selected in the dependency lock file: │ - registry.terraform.io/hashicorp/aws: there is no package for registry.terraform.io/hashicorp/aws 4.19.0 cached in .terraform/providers │ │ Terraform uses external plugins to integrate with a variety of different infrastructure services. To download the │ plugins required for this configuration, run: │ terraform init ╵ MNMBP:one-webserver minguyen$ terraform init

Initializing the backend...

Initializing provider plugins...

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure. All Terraform commands should now work.

If you ever set or change modules or backend configuration for Terraform, rerun this command to reinitialize your working directory. If you forget, other commands will detect it and remind you to do so if necessary. MNMBP:one-webserver minguyen$ terraform graph digraph { compound = "true" newrank = "true" subgraph "root" { "[root] aws_instance.example (expand)" [label = "aws_instance.example", shape = "box"] "[root] aws_security_group.instance (expand)" [label = "aws_security_group.instance", shape = "box"] "[root] provider[\"registry.terraform.io/hashicorp/aws\"]" [label = "provider[\"registry.terraform.io/hashicorp/aws\"]", shape = "diamond"] "[root] var.security_group_name" [label = "var.security_group_name", shape = "note"] "[root] aws_instance.example (expand)" -> "[root] aws_security_group.instance (expand)" "[root] aws_security_group.instance (expand)" -> "[root] provider[\"registry.terraform.io/hashicorp/aws\"]" "[root] aws_security_group.instance (expand)" -> "[root] var.security_group_name" "[root] output.public_ip (expand)" -> "[root] aws_instance.example (expand)" "[root] provider[\"registry.terraform.io/hashicorp/aws\"] (close)" -> "[root] aws_instance.example (expand)" "[root] root" -> "[root] output.public_ip (expand)" "[root] root" -> "[root] provider[\"registry.terraform.io/hashicorp/aws\"] (close)" } }

MNMBP:one-webserver minguyen$ terraform plan

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

Terraform will perform the following actions:

aws_instance.example will be created

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

Changes to Outputs:

────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

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. MNMBP:one-webserver minguyen$ terraform plan

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

Terraform will perform the following actions:

aws_instance.example will be created

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

Changes to Outputs:

────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

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. MNMBP:one-webserver minguyen$ terraform apply

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

Terraform will perform the following actions:

aws_instance.example will be created

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

Changes to Outputs:

Do you want to perform these actions? Terraform will perform the actions described above. Only 'yes' will be accepted to approve.

Enter a value: yes

aws_security_group.instance: Creating... aws_security_group.instance: Creation complete after 3s [id=sg-0046861b446d2e744] aws_instance.example: Creating... aws_instance.example: Still creating... [10s elapsed] aws_instance.example: Still creating... [20s elapsed] aws_instance.example: Creation complete after 22s [id=i-0a6bcc0919b133d57]

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

Outputs:

public_ip = "redacted" MNMBP:one-webserver minguyen$ terraform plan aws_security_group.instance: Refreshing state... [id=sg-0046861b446d2e744] aws_instance.example: Refreshing state... [id=i-0a6bcc0919b133d57]

No changes. Your infrastructure matches the configuration.

Terraform has compared your real infrastructure against your configuration and found no differences, so no changes are needed. MNMBP:one-webserver minguyen$

brikis98 commented 1 year ago

Could you put the log output into several code fences, one for each thing you ran? It's very hard to read it otherwise, as a lot of the syntax is being interpreted as Markdown, the content is wrapping, etc.