How to Launch an Amazon Linux EC2 Instance Using Terraform
Tags: aws, terraform, azure, javascript
Published: 2022-08-29 08:58:00

## **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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tw3n55wiv3700v8gcy6f.png)

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ft3c2qlauuv0xd2lqwwm.png)

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/thczretzktwt6dz6pfg7.png)

**Full screen shown below**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b1wdzhorverir7x77fgq.png)

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

```
- terraform init
```
**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7bbkzdj4w0vtiof05eq5.png)

```
- terraform plan
```
**Perform this command in this directory to see what the configuration file (the manifest) will do and review it**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/exv5ayyxmrxlfec6jzkw.png)

```
- terraform apply
```
**Perform this to execute the manifest that you have.**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zseqbdymvfft7yv892l3.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9ol51yg20lezd207bqhv.png)

**You can confirm this in the console**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0gl8wlxivz6vjole8fj6.png)

**Now You can see your Ubuntu server launched Successfully.**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kv4v0i1t3updi6gc9h2f.png)

**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
```
**To destroy a specific resources**

```
terraform destroy -target RESOURCE_TYPE.NAME -target RESOURCE_TYPE2.NAME
```
**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/esnhu1ryahoh8x8155b2.png)

```
terraform destroy -target aws_instance.Ubuntu
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l6luvqdbeytfo2qeymwn.png)

**Type yes**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/06gezhqw5xx7fo0jto8b.png)

**Now as shown below our resources terminated successfully**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h2z41quovn4d376zbfwv.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4azydmxoxb3frhxw6lxf.png)

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