Managing Applications and Infrastructure with Terraform-Deploying Infrastructure with Terraform-(2)Terraform for AWS-(9)Compute Part 1 - AMI Data, Key Pair, and the File Function
2018年10月04日
ec2-user:~/environment $ ls /home/ec2-user/.ssh
ec2-user:~/environment $ cat /home/ec2-user/.ssh/id_rsa.pub
Edit the compute/main.tf file.
In Terraform version 0.13, the argument "owners" is required in the data "aws_ami" "server_ami" block. In Terraform version 0.11, this is not required.
Edit the compute/variables.tf file.
ec2-user:~/environment/AWS $ cd AWS/compute
ec2-user:~/environment/AWS/compute $ terraform init
ec2-user:~/environment/AWS/compute $ terraform plan
References
file Function
*
Managing Applications and Infrastructure with Terraform-Deploying Infrastructure with Terraform
2. Terraform for AWS
9. Compute Part 1 - AMI Data, Key Pair, and the File Function
Close tabs for editing files in networking directory
ec2-user:~/environment $ ssh-keygen
Generating public/private rsa key pair. Enter file in which to save the key (/home/ec2-user/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/ec2-user/.ssh/id_rsa. Your public key has been saved in /home/ec2-user/.ssh/id_rsa.pub. The key fingerprint is: SHA256:yrmsUVfcg7htnsddZla6jTCjO7LHmTCK/SOOW****** ec2-user@ip-172-31-34-62 The key's randomart image is: +---[RSA 2048]----+ | | | o o | | . + o .| | + . ..| | . + S o + . =| | . + *oo + = O | | oo=. ++oo + .| | .o=+.o *o | | E++++oo=.. | +----[SHA256]-----+
ec2-user:~/environment $ ls /home/ec2-user/.ssh
authorized_keys id_rsa id_rsa.pub
ec2-user:~/environment $ cat /home/ec2-user/.ssh/id_rsa.pub
ssh-rsa ******* ec2-user@ip-172-31-34-62
Edit the compute/main.tf file.
#---compute/main.tf data "aws_ami" "server_ami" { most_recent = true owners = ["amazon"] filter { name = "owner-alias" values = ["amazon"] } filter { name = "name" values = ["amzn-ami-hvm*-x86_64-gp2"] } } resource "aws_key_pair" "tf_auth" { key_name = var.key_name public_key = file(var.public_key_path) }PS
In Terraform version 0.13, the argument "owners" is required in the data "aws_ami" "server_ami" block. In Terraform version 0.11, this is not required.
Edit the compute/variables.tf file.
#---compute/variables.tf variable "key_name" { default = "tfkey" } variable "public_key_path" { default = "/home/ec2-user/.ssh/id_rsa.pub" }
ec2-user:~/environment/AWS $ cd AWS/compute
ec2-user:~/environment/AWS/compute $ terraform init
Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/aws... - Installing hashicorp/aws v3.9.0... - Installed hashicorp/aws v3.9.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" 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/compute $ terraform plan
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 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. data.aws_ami.server_ami: Refreshing state... ------------------------------------------------------------------------ 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_key_pair.tf_auth will be created + resource "aws_key_pair" "tf_auth" { + arn = (known after apply) + fingerprint = (known after apply) + id = (known after apply) + key_name = "tfkey" + key_pair_id = (known after apply) + public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCzIeDhLeOVMfyYVV4ePCm/X4uRRmqkqq84TU2VsBDHWtMuFwBbez2ZmAm+WQ5zOyaZC/soSK17R8TsociZ+9wJBWT62aS3H8IHE2UxoakjlucF1QLM81oZaO5R4DCeKVJb0l/XfZ/fQkhYFLNM5622MbHP8MTTwfrbwE1+hjRFJYb0K364NCD0BLdgn+V7kfyEcSRRds8gh8zdiejJxFHRTaq9LRx+AQwFDkQYEYzk6ZGxIasKonCD18OtwAePdFgA1Mlho6Ajh9VyrgYWrEHKmfvDa/Rz7T/cCy5tkzdu5B04HWI7yBEthZeKm9QA8keOj1xU+yMWSNwUhiXGTg85 ec2-user@ip-10-0-0-44.us-west-2.compute.internal" } Plan: 1 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.
References
file Function