Terraform With AWS-3
Terraform With AWS-3
Before we see Terraform in action, first let’s check if we have EC2 instance using below steps,
https://www.linkedin.com/in/themr255/
Terraform with AWS
Step 3: Here you will see if you have any instances present in your account and which of them are
running and other info. And look for the region which is selected, you can change the region if it is
present in other region or you can check EC2 Global View.
So now, we’ll create a EC2 instance using terraform and then we’ll see if gets created or not using above
steps.
https://www.linkedin.com/in/themr255/
Terraform with AWS
Terraform In Action
Step 1: Install terraform on your machine and complete the setup.
Step 2: In order to connect to AWS using Terraform, terraform has to successfully authenticate. It is
done with the help of Programmatic API Keys (Access Key and Secret). So we’ll need to create or use the
existing ones for authentication. To do that please check steps given in below doc.
aws_setup_progra
mmatic_access.txt
Step 3: Now we need to set these variables on our CLI, so we can use those for authentication.
If you are using Windows, then use below commands to set these variables -
set AWS_ACCESS_KEY_ID=JKAb************
set AWS_SECRET_ACCESS_KEY=Zasdak***************
If you are using Linux, then use below commands to set these variables -
export AWS_ACCESS_KEY_ID=JKAb************
export AWS_SECRET_ACCESS_KEY=Zasdak***************
export_variables_cm
d_output.txt
Step 4: Now create a new file with tf extension, for e.g. main.tf. You can create this file in your current
directory or desired directory.
aws_ec2.tf
https://www.linkedin.com/in/themr255/
Terraform with AWS
Step 5: Now run terraform init command. It initializes a new or existing Terraform configuration. It
downloads the necessary provider plugins and sets up the backend configuration.
terraform_init_cmd_
output.txt
Step 6: Now run terraform plan command. It will create an execution plan that shows the changes
Terraform will make to the infrastructure. It compares the desired state in the Terraform configuration
with the current state and displays a list of actions that will be taken.
terraform_plan_cmd
_output.txt
Step 7: Now run terraform apply command. It will apply the changes described in the execution plan.
Creates, modifies, or destroys resources to match the desired state defined in the configuration.
terraform_apply_cm
d_output.txt
Step 8: Again check the first steps mentioned in top section of document to verify if resource got
created or not.
Goto EC2 service window. And now you can see the EC2 instance got created, so click on it.
https://www.linkedin.com/in/themr255/
Terraform with AWS
And here you can see EC2 instance got created with name ec_instance which we gave in main.tf file
Don’t try to connect to this EC2 instance, as the SSH rule is not added in NSG.
https://www.linkedin.com/in/themr255/