Managing Applications and Infrastructure with Terraform-Deploying Infrastructure with Terraform-(2)Terraform for AWS-(4)Storage Part 1: The S3 Bucket and Random ID

2018年10月04日


Managing Applications and Infrastructure with Terraform-Deploying Infrastructure with Terraform
2. Terraform for AWS
4. Storage Part 1: The S3 Bucket and Random ID

Edit file -> storage, main.tf in Cloud9


For how to create a random ID under Terraform, refer to blog post
Further will be discuessed here, Terraform-Deploy S3 Bucket with a Random ID.

#---storage/main.tf---

# Create a random id

resource "random_id" "tf_bucket_id" {
    byte_length = 2
}

# Create the bucket

resource "aws_s3_bucket" "tf_code" {
    bucket = "${var.project_name}-${random_id.tf_bucket_id.dec}"
    acl = "private"
    force_destroy = true
    
    tags = {
        Name = "tf_bucket"
    }
}
NB
dec - The generated id presented in non-padded decimal digits.

Edit file -> storage, variables.tf in Cloud9
variable "project_name" {
    default = "la-terraform"
}

Edit file -> storage, outputs.tf in Cloud9
output "bucketname" {
    value = aws_s3_bucket.tf_code.id
}

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

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

ec2-user:~/environment/AWS/storage $ terraform init
Initializing the backend...

Initializing provider plugins...
- Finding latest version of hashicorp/random...
- Finding latest version of hashicorp/aws...
- Installing hashicorp/random v3.0.0...
- Installed hashicorp/random v3.0.0 (signed by HashiCorp)
- Installing hashicorp/aws v3.10.0...
- Installed hashicorp/aws v3.10.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.10.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/storage $ terraform apply
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


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:

  # 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)
        }
    }

  # 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

random_id.tf_bucket_id: Creating...
random_id.tf_bucket_id: Creation complete after 0s [id=dnU]
aws_s3_bucket.tf_code: Creating...
aws_s3_bucket.tf_code: Creation complete after 2s [id=la-terraform-30325]

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

Outputs:

bucketname = la-terraform-30325

The S3 bucket (i.e. la-terraform-30325) has been created.

Category: orchestration Tags: public

Upvote


Downvote