Managing Applications and Infrastructure with Terraform-Deploying Infrastructure with Terraform-(2)Terraform for AWS-(5)Storage Part 2: The Root Module

2018年10月04日


Managing Applications and Infrastructure with Terraform-Deploying Infrastructure with Terraform
2. Terraform for AWS
5. Storage Part 2: The Root Module

ec2-user:~/environment/AWS/storage $ pwd

/home/ec2-user/environment/AWS/storage

ec2-user:~/environment/AWS/storage $ cd ..

ec2-user:~/environment/AWS $ touch {main.tf,variables.tf,outputs.tf}

Edit the AWS/main.tf file.
provider "aws" {
    region = var.aws_region
}

# Deploy Storage Resource
module "storage" {
    source = "./storage"
    project_name = var.project_name
}

Edit the AWS/variables.tf file.
variable "aws_region" {}
variable "project_name" {}

ec2-user:~/environment/AWS $ touch terraform.tfvars

ec2-user:~/environment/AWS $ tree
.
├── compute
│   ├── main.tf
│   ├── outputs.tf
│   ├── userdata.tpl
│   └── variables.tf
├── main.tf
├── networking
│   ├── main.tf
│   ├── outputs.tf
│   └── variables.tf
├── outputs.tf
├── storage
│   ├── main.tf
│   ├── outputs.tf
│   ├── terraform.tfstate
│   └── variables.tf
├── terraform.tfvars
└── variables.tf

3 directories, 15 files

Edit the AWS/terraform.tfvars file.
aws_region = "us-west-2"
project_name = "la-terrafrom"

Edit the AWS/storage/variables.tf file.
variable "project_name" {}

ec2-user:~/environment/AWS $ terraform init
Initializing modules...
- storage in storage

Initializing the backend...

Initializing provider plugins...
- Finding latest version of hashicorp/aws...
- Finding latest version of hashicorp/random...
- Installing hashicorp/aws v3.9.0...
- Installed hashicorp/aws v3.9.0 (signed by HashiCorp)
- Installing hashicorp/random v3.0.0...
- Installed hashicorp/random v3.0.0 (signed by HashiCorp)

The following providers do not have any version constraints in configuration,
so the latest version was installed.

To prevent automatic upgrades to new major versions that may contain breaking
changes, we recommend adding version constraints in a required_providers block
in your configuration, with the constraint strings suggested below.

* hashicorp/aws: version = "~> 3.9.0"
* hashicorp/random: version = "~> 3.0.0"

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.

ec2-user:~/environment/AWS $ terraform plan
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.


------------------------------------------------------------------------

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # module.storage.aws_s3_bucket.tf_code will be created
  + resource "aws_s3_bucket" "tf_code" {
      + acceleration_status         = (known after apply)
      + acl                         = "private"
      + arn                         = (known after apply)
      + bucket                      = (known after apply)
      + bucket_domain_name          = (known after apply)
      + bucket_regional_domain_name = (known after apply)
      + force_destroy               = true
      + hosted_zone_id              = (known after apply)
      + id                          = (known after apply)
      + region                      = (known after apply)
      + request_payer               = (known after apply)
      + tags                        = {
          + "Name" = "tf_bucket"
        }
      + website_domain              = (known after apply)
      + website_endpoint            = (known after apply)

      + versioning {
          + enabled    = (known after apply)
          + mfa_delete = (known after apply)
        }
    }

  # module.storage.random_id.tf_bucket_id will be created
  + resource "random_id" "tf_bucket_id" {
      + b64_std     = (known after apply)
      + b64_url     = (known after apply)
      + byte_length = 2
      + dec         = (known after apply)
      + hex         = (known after apply)
      + id          = (known after apply)
    }

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

------------------------------------------------------------------------

Note: You didn't specify an "-out" parameter to save this plan, so Terraform
can't guarantee that exactly these actions will be performed if
"terraform apply" is subsequently run.

ec2-user:~/environment/AWS $ cd storage

ec2-user:~/environment/AWS/storage $ terraform destroy
var.project_name
  Enter a value: la-terrafrom

provider.aws.region
  The region where AWS operations will take place. Examples
  are us-east-1, us-west-2, etc.

  Enter a value: us-west-2

random_id.tf_bucket_id: Refreshing state... [id=nic]
aws_s3_bucket.tf_code: Refreshing state... [id=la-terraform-40487]

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  - destroy

Terraform will perform the following actions:

  # aws_s3_bucket.tf_code will be destroyed
  - resource "aws_s3_bucket" "tf_code" {
      - acl                         = "private" -> null
      - arn                         = "arn:aws:s3:::la-terraform-40487" -> null
      - bucket                      = "la-terraform-40487" -> null
      - bucket_domain_name          = "la-terraform-40487.s3.amazonaws.com" -> null
      - bucket_regional_domain_name = "la-terraform-40487.s3.us-west-2.amazonaws.com" -> null
      - force_destroy               = true -> null
      - hosted_zone_id              = "Z3BJ6K6RIION7M" -> null
      - id                          = "la-terraform-40487" -> null
      - region                      = "us-west-2" -> null
      - request_payer               = "BucketOwner" -> null
      - tags                        = {
          - "Name" = "tf_bucket"
        } -> null

      - versioning {
          - enabled    = false -> null
          - mfa_delete = false -> null
        }
    }

  # random_id.tf_bucket_id will be destroyed
  - resource "random_id" "tf_bucket_id" {
      - b64_std     = "nic=" -> null
      - b64_url     = "nic" -> null
      - byte_length = 2 -> null
      - dec         = "40487" -> null
      - hex         = "9e27" -> null
      - id          = "nic" -> null
    }

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

Changes to Outputs:
  - bucketname = "la-terraform-40487" -> null

Do you really want to destroy all resources?
  Terraform will destroy all your managed infrastructure, as shown above.
  There is no undo. Only 'yes' will be accepted to confirm.

  Enter a value: yes

aws_s3_bucket.tf_code: Destroying... [id=la-terraform-40487]
aws_s3_bucket.tf_code: Destruction complete after 0s
random_id.tf_bucket_id: Destroying... [id=nic]
random_id.tf_bucket_id: Destruction complete after 0s

Destroy complete! Resources: 2 destroyed.

ec2-user:~/environment/AWS/storage $ cd ..

ec2-user:~/environment/AWS $ terraform plan
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.


------------------------------------------------------------------------

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # module.storage.aws_s3_bucket.tf_code will be created
  + resource "aws_s3_bucket" "tf_code" {
      + acceleration_status         = (known after apply)
      + acl                         = "private"
      + arn                         = (known after apply)
      + bucket                      = (known after apply)
      + bucket_domain_name          = (known after apply)
      + bucket_regional_domain_name = (known after apply)
      + force_destroy               = true
      + hosted_zone_id              = (known after apply)
      + id                          = (known after apply)
      + region                      = (known after apply)
      + request_payer               = (known after apply)
      + tags                        = {
          + "Name" = "tf_bucket"
        }
      + website_domain              = (known after apply)
      + website_endpoint            = (known after apply)

      + versioning {
          + enabled    = (known after apply)
          + mfa_delete = (known after apply)
        }
    }

  # module.storage.random_id.tf_bucket_id will be created
  + resource "random_id" "tf_bucket_id" {
      + b64_std     = (known after apply)
      + b64_url     = (known after apply)
      + byte_length = 2
      + dec         = (known after apply)
      + hex         = (known after apply)
      + id          = (known after apply)
    }

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

------------------------------------------------------------------------

Note: You didn't specify an "-out" parameter to save this plan, so Terraform
can't guarantee that exactly these actions will be performed if
"terraform apply" is subsequently run.

ec2-user:~/environment/AWS $ terraform apply
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # module.storage.aws_s3_bucket.tf_code will be created
  + resource "aws_s3_bucket" "tf_code" {
      + acceleration_status         = (known after apply)
      + acl                         = "private"
      + arn                         = (known after apply)
      + bucket                      = (known after apply)
      + bucket_domain_name          = (known after apply)
      + bucket_regional_domain_name = (known after apply)
      + force_destroy               = true
      + hosted_zone_id              = (known after apply)
      + id                          = (known after apply)
      + region                      = (known after apply)
      + request_payer               = (known after apply)
      + tags                        = {
          + "Name" = "tf_bucket"
        }
      + website_domain              = (known after apply)
      + website_endpoint            = (known after apply)

      + versioning {
          + enabled    = (known after apply)
          + mfa_delete = (known after apply)
        }
    }

  # module.storage.random_id.tf_bucket_id will be created
  + resource "random_id" "tf_bucket_id" {
      + b64_std     = (known after apply)
      + b64_url     = (known after apply)
      + byte_length = 2
      + dec         = (known after apply)
      + hex         = (known after apply)
      + id          = (known after apply)
    }

Plan: 2 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

module.storage.random_id.tf_bucket_id: Creating...
module.storage.random_id.tf_bucket_id: Creation complete after 0s [id=RlA]
module.storage.aws_s3_bucket.tf_code: Creating...
module.storage.aws_s3_bucket.tf_code: Creation complete after 2s [id=la-terrafrom-18000]

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

Edit the AWS/outputs.tf file.
#---outputs.tf---

output "Bucket_Name" {
    value = module.storage.bucketname
}

ec2-user:~/environment/AWS $ terraform apply
module.storage.random_id.tf_bucket_id: Refreshing state... [id=RlA]
module.storage.aws_s3_bucket.tf_code: Refreshing state... [id=la-terrafrom-18000]

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:

Terraform will perform the following actions:

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

Changes to Outputs:
  + Bucket_Name = "la-terrafrom-18000"

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


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

Outputs:

Bucket_Name = la-terrafrom-18000



Category: orchestration Tags: public

Upvote


Downvote