The purpose of infrastructure as code (IaC) is to create and execute code to define, create, modify, and delete computing, storage and networking infrastructure, with consistency.
- Basics with Terraform HCL
$ terraform init $ terraform plan $ terraform apply
-
Ensure login key configuration is made with the
aws configure
for the user with sufficient Policy permissions
NB. Below is performed with the group set to the policy AmazonEC2FullAccess -
main.tf
provider "aws" { region = "us-east-2" } resource "aws_instance" "vm-solo-01" { ami = "ami-00c03f7f7f2ec15c3" instance_type = "t2.micro" count = 1 }
-
Run
terraform init
$ terraform init Initializing the backend... Initializing provider plugins... - Checking for available provider plugins... - Downloading plugin for provider "aws" (hashicorp/aws) 2.29.0... 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, it is recommended to add version = "..." constraints to the corresponding provider blocks in configuration, with the constraint strings suggested below. * provider.aws: version = "~> 2.29" 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.
-
Run
terraform apply
(ordinarily afterterraform plan
to validate the configuration)$ 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: # aws_instance.vm-solo-01 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.vm-solo-01: Creating... aws_instance.vm-solo-01: Still creating... [10s elapsed] aws_instance.vm-solo-01: Still creating... [20s elapsed] aws_instance.vm-solo-01: Still creating... [30s elapsed] aws_instance.vm-solo-01: Still creating... [40s elapsed] aws_instance.vm-solo-01: Creation complete after 50s [id=i-0b11a0cdff48a7308] Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
-
Run
aws ec2 describe-instances
$ aws ec2 describe-instances --region us-east-2 --query 'Reservations[*].Instances[*].[Tags[?Key==\`Name\`]|[0].Value,InstanceId,PrivateIpAddress,PublicIpAddress,Placement.AvailabilityZone,State.Name]' --output text" None i-0b11a0cdff48a7308 172.31.44.122 18.220.211.66 us-east-2c running
-
Set the AWS VM name tag by adding the "tags" stanza to the terraform
main.tf
file within the "resource" stanzatags = { Name = "vm-solo-01" }
-
Run
terraform plan
$ 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. aws_instance.vm-solo-01: Refreshing state... [id=i-0b11a0cdff48a7308] ------------------------------------------------------------------------ An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols: ~ update in-place Terraform will perform the following actions: # aws_instance.vm-solo-01 will be updated in-place ... Plan: 0 to add, 1 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.
-
Run
terraform apply
to create the VM$ terraform apply aws_instance.vm-solo-01: Refreshing state... [id=i-0b11a0cdff48a7308] An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols: ~ update in-place Terraform will perform the following actions: # aws_instance.vm-solo-01 will be updated in-place ... 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.vm-solo-01: Modifying... [id=i-0b11a0cdff48a7308] aws_instance.vm-solo-01: Still modifying... [id=i-0b11a0cdff48a7308, 10s elapsed] aws_instance.vm-solo-01: Modifications complete after 13s [id=i-0b11a0cdff48a7308] Apply complete! Resources: 0 added, 1 changed, 0 destroyed.
-
Run
aws ec2 describe-instances
$ aws ec2 describe-instances --region us-east-2 --query 'Reservations[*].Instances[*].[Tags[?Key==\`Name\`]|[0].Value,InstanceId,PrivateIpAddress,PublicIpAddress,Placement.AvailabilityZone,State.Name]' --output text" vm-solo-01 i-0b11a0cdff48a7308 172.31.44.122 18.220.211.66 us-east-2c running
-
Run
terraform destroy
to delete (terminate) the VM$ terraform destroy aws_instance.vm-solo-01: Refreshing state... [id=i-0b11a0cdff48a7308] 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_instance.vm-solo-01 will be destroyed ... Plan: 0 to add, 0 to change, 1 to destroy. 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_instance.vm-solo-01: Destroying... [id=i-0b11a0cdff48a7308] aws_instance.vm-solo-01: Still destroying... [id=i-0b11a0cdff48a7308, 10s elapsed] aws_instance.vm-solo-01: Still destroying... [id=i-0b11a0cdff48a7308, 20s elapsed] aws_instance.vm-solo-01: Destruction complete after 25s Destroy complete! Resources: 1 destroyed.
-
Run
aws ec2 describe-instances
$ aws ec2 describe-instances --region us-east-2 --query 'Reservations[*].Instances[*].[Tags[?Key==\`Name\`]|[0].Value,InstanceId,PrivateIpAddress,PublicIpAddress,Placement.AvailabilityZone,State.Name]' --output text" vm-solo-01 i-0b11a0cdff48a7308 None None us-east-2c terminated