DEV Community

Cover image for How to Launch an Amazon Linux EC2 Instance Using Terraform
Surya Shankar
Surya Shankar

Posted on • Edited on

How to Launch an Amazon Linux EC2 Instance Using Terraform

Terraform

  • Terraform is a tool for building, changing, and versioning infrastructure safely and efficiently.
  • Terraform can manage existing and popular service providers as well as custom in-house solutions which has been developed by HashiCorp.
  • A provider is responsible for understanding API interactions and exposing resources.
  • Most of the available providers correspond to one cloud or on-premises infrastructure platform and offers resource types that correspond to each of the features of that platform.

Advantages of Terraform:

- Support multiple platforms & has hundreds of providers
- Simple configuration language with fast learning curves
- Easy integration with configuration management tools like Ansible
- Easily extensible with plugins
- Free

Creating your AWS account user and setting up a directory for the terraform manifests

Create a user in your AWS account and note the access key and the security key. Create a directory wherein you will keep the terraform code and create a new file called first_ec2.tf. This will be the file where we will write out configuration into

Image description

Creating your terraform manifest

  • Considerations:
  • - Ensuring authentication to AWS
  • - Specifying a region for the resource
  • - Specifying the resource

Navigate to https://registry.terraform.io/ to look at the requirements to launch a resource. Navigate to Documentation found in the AWS provider . Here they provide example usage of the provider and resource blocks

Provider Block : Begin building the code for the provider block based of the example and signify the region:
Use your AWS users access-key and secret-key (acquired from IAM users):

Image description

Resource Block : Begin building the code for the resource block, this requires an ami and the instance type that you are interested in. You can choose the t2.micro free instance type here on the Amazon Linux 2 operating system. The ami must be selected according to the region and many more.

Image description

Full screen shown below

Image description

Preparing your terraform directory and executing your manifest with the terraform core commands

- terraform init
Enter fullscreen mode Exit fullscreen mode

The terraform init command read the configuration file that we used and found the provider to be aws and downloaded the terraform plugins needed for this. These can be found in the path .terraform\providers\registry.terraform.io\hashicorp

Each time you configure our provider, ensure to run terraform init

Image description

- terraform plan
Enter fullscreen mode Exit fullscreen mode

Perform this command in this directory to see what the configuration file (the manifest) will do and review it

Image description

- terraform apply
Enter fullscreen mode Exit fullscreen mode

Perform this to execute the manifest that you have.

Image description

Enter yes to execute it after it shows a review of what will happen

Image description

You can confirm this in the console

Image description

Now You can see your Ubuntu server launched Successfully.

Image description

Since this is a test, you can destroy your instance by running the command:

The destroy command can be used to destroy a complete set of cloud infrastructure or a targeted resource.

- terraform destroy
Enter fullscreen mode Exit fullscreen mode

To destroy a specific resources

terraform destroy -target RESOURCE_TYPE.NAME -target RESOURCE_TYPE2.NAME
Enter fullscreen mode Exit fullscreen mode

It terminates all the resources specified in your Terraform configuration file.

Here Resource_type is aws_instance and name is Ubuntu as shown below

Image description

terraform destroy -target aws_instance.Ubuntu
Enter fullscreen mode Exit fullscreen mode

Image description

Type yes

Image description

Now as shown below our resources terminated successfully

Image description

Image description

Visit this link ,If you want more information ..
https://spacelift.io/blog/how-to-destroy-terraform-resources

Top comments (0)