/plushcap/analysis/spacelift/terraform-tutorial

Terraform Tutorial – Getting Started With Terraform on AWS

What's this blog post about?

In this tutorial, we will learn about the basics of using Terraform to manage resources in Amazon Web Services (AWS). We will create an EC2 instance and then destroy it. Along the way, we will also discuss various concepts like variables, state files, backend configuration etc. Prerequisites: Terraform installed on your system AWS account with administrative access Step 1: Install Terraform and AWS CLI We have already discussed this in detail in the previous tutorial. If you haven't done so yet, please refer to that for detailed instructions. Step 2: Setup AWS Provider Terraform uses providers to interact with different cloud platforms like AWS, Azure, GCP etc. To use any of these cloud providers, we need to configure them in our Terraform code. This is done by adding a provider block at the top level of our configuration file. Let's create a new file named provider.tf and add the following content to it. ``` terraform { required_providers { aws = { source = "hashicorp/aws" version = "~> 4.18.0" } }\ } ``` Here, we are using the AWS provider from HashiCorp and specifying its minimum required version as 4.18.0. This means that any version of this provider above or equal to 4.18.0 will be compatible with our configuration file. Step 3: Create EC2 instance Now let's create a new file named main.tf and add the following content to it. ``` resource "aws_instance" "my_vm" { ami = "ami-0a253c64f002f85fc" instance_type = "t2.micro" } ``` Here, we are creating an EC2 instance using the AWS provider. We have specified the Amazon Machine Image (AMI) id as ami-0a253c64f002f85fc and the instance type as t2.micro. These values can be changed according to your requirements. Step 4: Initialize Terraform Environment Before we can apply our configuration, we need to initialize our Terraform environment using the terraform init command. This command downloads all necessary plugins for the configured provider(s). Run this command from the root directory of our project. ``` sumeetninawe@Sumeets-MacBook-Pro tf-tuts % terraform init Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/aws... - Installing hashicorp/aws v4.18.0... * provider.aws: version = "~> 4.18.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. sumeetninawe@Sumeets-MacBook-Pro tf-tuts % If you see the output as above, it means we have successfully initialized our Terraform environment and downloaded all necessary plugins for the AWS provider. Step 5: Apply Configuration Now let's apply our configuration using the terraform apply command. This command creates real-world cloud entities based on our configuration file(s). Run this command from the root directory of our project. ``` sumeetninawe@Sumeets-MacBook-Pro tf-tuts % terraform apply Terraform will perform the actions described above. Only 'yes' will be accepted to approve. Enter a value: yes aws_instance.my_vm: Creating... aws_instance.my_vm: Still creating... [10s elapsed] aws_instance.my_vm (ami-0a253c64f002f85fc): Associating address 18.184.50.169 with instance ID i-0a253c64f002f85fc aws_instance.my_vm: Creation complete after 20s [id=i-0a253c64f002f85fc] Apply complete! Resources: 1 added, 0 changed, 0 destroyed. sumeetninawe@Sumeets-MacBook-Pro tf-tuts % If you see the output as above, it means we have successfully applied our configuration and created an EC2 instance in AWS. The details of this instance are displayed at the end of the output. Step 6: Verify Creation To verify that our EC2 instance has been created, log in to the AWS console and navigate to the EC2 dashboard. You should see your newly created instance listed there. Step 7: Destroy Resource Now let's destroy the resource we just created using the terraform destroy command. This command deletes real-world cloud entities based on our configuration file(s). Run this command from the root directory of our project. ``` sumeetninawe@Sumeets-MacBook-Pro tf-tuts % terraform destroy Terraform will perform the actions described above. Only 'yes' will be accepted to approve. Enter a value: yes aws_instance.my_vm: Destroying... [id=i-0a253c64f002f85fc] aws_instance.my_vm: Still destroying... [id=i-0a253c64f002f85fc, 10s elapsed] aws_instance.my_vm: Destruction complete after 20s Destroy complete! Resources: 1 destroyed. sumeetninawe@Sumeets-MacBook-Pro tf-tuts % If you see the output as above, it means we have successfully destroyed our EC2 instance in AWS. The details of this destruction are displayed at the end of the output. Step 8: Verify Destruction To verify that our EC2 instance has been deleted, log in to the AWS console and navigate to the EC2 dashboard. You should no longer see your instance listed there. Conclusion In this tutorial, we have learned about the basics of using Terraform to manage resources in Amazon Web Services (AWS). We created an EC2 instance and then destroyed it. Along the way, we also discussed various concepts like variables, state files, backend configuration etc. If you need more help with Terraform, I encourage you to check the following blog posts: How to Automate Terraform Deployments, and 12 Terraform Best Practices. Terraform Management Made EasySpacelift effectively manages Terraform state, more complex workflows, supports policy as code, programmatic configuration, context sharing, drift detection, resource visualization and includes many more features. Start free trialWritten bySumeet NinaweSumeet has over ten years of overall experience in IT and has worked with cloud and DevOps technologies for the last four years. He is a Certified System Administrator and TOGAF® 9. He specializes in writing IaC using Terraform. In his free time, Sumeet maintains a blog at LetsDoTech.ProductDocumentationHow it worksSpacelift TutorialPricingCustomer Case StudiesIntegrationsSecuritySystem Status Product UpdatesCompanyAbout UsCareersContact SalesPartnersLearnBlogSpacelift vs AtlantisSpacelift vs Terraform CloudSpacelift for AWS© 2022 Spacelift, Inc. All rights reservedPrivacy PolicyTerms of Service``` SUMMARY:

Company
Spacelift

Date published
July 12, 2022

Author(s)
Sumeet Ninawe

Word count
7483

Language
English

Hacker News points
None found.


By Matt Makai. 2021-2024.