You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Get latest AMI ID for Amazon Linux2 OS# Get Latest AWS AMI ID for Amazon2 Linux
data "aws_ami""amzlinux2" {
most_recent = true
owners = [ "amazon" ]
filter {
name = "name"values = [ "amzn2-ami-hvm-*-gp2" ]
}
filter {
name = "root-device-type"values = [ "ebs" ]
}
filter {
name = "virtualization-type"values = [ "hvm" ]
}
filter {
name = "architecture"values = [ "x86_64" ]
}
}
Reference the datasource in c5-ec2instance.tf file
# Reference Datasource to get the latest AMI ID
ami = data.aws_ami.amzlinux2.id
# Terraform Output Values
output "instance_publicip" {
description = "EC2 Instance Public IP"
value = aws_instance.myec2vm.public_ip
}
output "instance_publicdns" {
description = "EC2 Instance Public DNS"
value = aws_instance.myec2vm.public_dns
}
Step-07: Execute Terraform Commands
# Terraform Initialize
terraform init
Observation:
1) Initialized Local Backend
2) Downloaded the provider plugins (initialized plugins)
3) Review the folder structure ".terraform folder"# Terraform Validate
terraform validate
Observation:
1) If any changes to files, those will come as printed in stdout (those file names will be printed in CLI)
# Terraform Plan
terraform plan
Observation:
1) Verify the latest AMI ID picked and displayed in plan
2) Verify the number of resources that going to get created
3) Verify the variable replacements worked as expected
# Terraform Apply
terraform apply
[or]
terraform apply -auto-approve
Observations:
1) Create resources on cloud
2) Created terraform.tfstate file when you run the terraform apply command
3) Verify the EC2 Instance AMI ID which got created
# Terraform Destroy
terraform plan -destroy # You can view destroy plan using this command
terraform destroy
# Clean-Up Files
rm -rf .terraform*
rm -rf terraform.tfstate*