Create a user in aws

Give the username and programmatic access

download access keys, secret keys

Choose the required ami

[root@node1 ~]# wget https://releases.hashicorp.com/terraform/0.11.10/terraform_0.11.10_linux_amd64.zip
[root@node1 ~]# unzip terraform_0.11.10_linux_amd64.zip
Archive: terraform_0.11.10_linux_amd64.zip
inflating: terraform
[root@node1 ~]# cp terraform /usr/bin/
[root@node1 ~]# which terraform
/usr/bin/terraform
root@node1 ~]# terraform -h
Usage: terraform [-version] [-help] <command> [args]
The available commands for execution are listed below.
The most common, useful commands are shown first, followed by
less common or more advanced commands. If you’re just getting
started with Terraform, stick with the common commands. For the
other commands, please read the help and docs before usage.
Common commands:
apply Builds or changes infrastructure
console Interactive console for Terraform interpolations
destroy Destroy Terraform-managed infrastructure
env Workspace management
fmt Rewrites config files to canonical format
get Download and install modules for the configuration
graph Create a visual graph of Terraform resources
import Import existing infrastructure into Terraform
init Initialize a Terraform working directory
output Read an output from a state file
plan Generate and show an execution plan
providers Prints a tree of the providers used in the configuration
push Upload this Terraform module to Atlas to run
refresh Update local state file against real resources
show Inspect Terraform state or plan
taint Manually mark a resource for recreation
untaint Manually unmark a resource as tainted
validate Validates the Terraform files
version Prints the Terraform version
workspace Workspace management
All other commands:
debug Debug output management (experimental)
force-unlock Manually unlock the terraform state
state Advanced state management
[root@node1 ~]# mkdir test-tf
[root@node1 ~]# cd test-tf/
[root@node1 test-tf]# vim aws.tf
provider “aws” {
access_key = “XXXXXXXXXXXXX”
secret_key = “XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX”
region = “us-east-1”
}
resource “aws_instance” “example” {
ami = “ami-04ab2c6566a35dfe1”
instance_type = “t2.micro”
tags {
Name = “your-instance”
}
}
[root@node1 test-tf]# terraform plan
Plugin reinitialization required. Please run “terraform init”.
Reason: Could not satisfy plugin requirements.
Plugins are external binaries that Terraform uses to access and manipulate
resources. The configuration provided requires plugins which can’t be located,
don’t satisfy the version constraints, or are otherwise incompatible.
1 error(s) occurred:
* provider.aws: no suitable version installed
version requirements: “(any version)”
versions installed: none
Terraform automatically discovers provider requirements from your
configuration, including providers used in child modules. To see the
requirements and constraints from each module, run “terraform providers”.
Error: error satisfying plugin requirements
[root@node1 test-tf]# terraform init
Initializing provider plugins…
– Checking for available provider plugins on https://releases.hashicorp.com…
– Downloading plugin for provider “aws” (1.41.0)…
The following providers do not have any version constraints in configuration,
so the latest version was installed.
To prevent automatic upgrades to new major versions that may contain breaking
changes, it is recommended to add version = “…” constraints to the
corresponding provider blocks in configuration, with the constraint strings
suggested below.
* provider.aws: version = “~> 1.41”
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.
If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
[root@node1 test-tf]# terraform plan
Refreshing Terraform state in-memory prior to plan…
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.
————————————————————————
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
+ create
Terraform will perform the following actions:
+ aws_instance.example
id: <computed>
ami: “ami-04ab2c6566a35dfe1”
arn: <computed>
associate_public_ip_address: <computed>
availability_zone: <computed>
cpu_core_count: <computed>
cpu_threads_per_core: <computed>
ebs_block_device.#: <computed>
ephemeral_block_device.#: <computed>
get_password_data: “false”
instance_state: <computed>
instance_type: “t2.micro”
ipv6_address_count: <computed>
ipv6_addresses.#: <computed>
key_name: <computed>
network_interface.#: <computed>
network_interface_id: <computed>
password_data: <computed>
placement_group: <computed>
primary_network_interface_id: <computed>
private_dns: <computed>
private_ip: <computed>
public_dns: <computed>
public_ip: <computed>
root_block_device.#: <computed>
security_groups.#: <computed>
source_dest_check: “true”
subnet_id: <computed>
tags.%: “1”
tags.Name: “your-instance”
tenancy: <computed>
volume_tags.%: <computed>
vpc_security_group_ids.#: <computed>
Plan: 1 to add, 0 to change, 0 to destroy.
————————————————————————
Note: You didn’t specify an “-out” parameter to save this plan, so Terraform
can’t guarantee that exactly these actions will be performed if
“terraform apply” is subsequently run.

[root@node1 test-tf]# terraform apply
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
+ create
Terraform will perform the following actions:
+ aws_instance.example
id: <computed>
ami: “ami-04ab2c6566a35dfe1”
arn: <computed>
associate_public_ip_address: <computed>
availability_zone: <computed>
cpu_core_count: <computed>
cpu_threads_per_core: <computed>
ebs_block_device.#: <computed>
ephemeral_block_device.#: <computed>
get_password_data: “false”
instance_state: <computed>
instance_type: “t2.micro”
ipv6_address_count: <computed>
ipv6_addresses.#: <computed>
key_name: <computed>
network_interface.#: <computed>
network_interface_id: <computed>
password_data: <computed>
placement_group: <computed>
primary_network_interface_id: <computed>
private_dns: <computed>
private_ip: <computed>
public_dns: <computed>
public_ip: <computed>
root_block_device.#: <computed>
security_groups.#: <computed>
source_dest_check: “true”
subnet_id: <computed>
tags.%: “1”
tags.Name: “your-instance”
tenancy: <computed>
volume_tags.%: <computed>
vpc_security_group_ids.#: <computed>
Plan: 1 to add, 0 to change, 0 to destroy.
Do you want to perform these actions?
Terraform will perform the actions described above.
Only ‘yes’ will be accepted to approve.
Enter a value: yes
aws_instance.example: Creating…
ami: “” => “ami-04ab2c6566a35dfe1”
arn: “” => “<computed>”
associate_public_ip_address: “” => “<computed>”
availability_zone: “” => “<computed>”
cpu_core_count: “” => “<computed>”
cpu_threads_per_core: “” => “<computed>”
ebs_block_device.#: “” => “<computed>”
ephemeral_block_device.#: “” => “<computed>”
get_password_data: “” => “false”
instance_state: “” => “<computed>”
instance_type: “” => “t2.micro”
ipv6_address_count: “” => “<computed>”
ipv6_addresses.#: “” => “<computed>”
key_name: “” => “<computed>”
network_interface.#: “” => “<computed>”
network_interface_id: “” => “<computed>”
password_data: “” => “<computed>”
placement_group: “” => “<computed>”
primary_network_interface_id: “” => “<computed>”
private_dns: “” => “<computed>”
private_ip: “” => “<computed>”
public_dns: “” => “<computed>”
public_ip: “” => “<computed>”
root_block_device.#: “” => “<computed>”
security_groups.#: “” => “<computed>”
source_dest_check: “” => “true”
subnet_id: “” => “<computed>”
tags.%: “” => “1”
tags.Name: “” => “your-instance”
tenancy: “” => “<computed>”
volume_tags.%: “” => “<computed>”
vpc_security_group_ids.#: “” => “<computed>”
aws_instance.example: Still creating… (10s elapsed)
aws_instance.example: Still creating… (20s elapsed)
aws_instance.example: Still creating… (30s elapsed)
aws_instance.example: Creation complete after 37s (ID: i-06bad98e885e1142d)
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
[root@node1 test-tf]# terraform plan
Refreshing Terraform state in-memory prior to plan…
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.
aws_instance.example: Refreshing state… (ID: i-06bad98e885e1142d)
————————————————————————
No changes. Infrastructure is up-to-date.
This means that Terraform did not detect any differences between your
configuration and real physical resources that exist. As a result, no
actions need to be performed.


[root@node1 test-tf]# ls -l
total 12
-rw-r–r–. 1 root root 260 Oct 24 23:56 aws.tf
-rw-r–r–. 1 root root 3958 Oct 24 23:58 terraform.tfstate
-rw-r–r–. 1 root root 318 Oct 24 23:58 terraform.tfstate.backup
[root@node1 test-tf]# cat terraform.tfstate
{
“version”: 3,
“terraform_version”: “0.11.10”,
“serial”: 2,
“lineage”: “31fce12f-ed57-76b7-9045-a3b90c165044”,
“modules”: [
{
“path”: [
“root”
],
“outputs”: {},
“resources”: {
“aws_instance.example”: {
“type”: “aws_instance”,
“depends_on”: [],
“primary”: {
“id”: “i-06bad98e885e1142d”,
“attributes”: {
“ami”: “ami-04ab2c6566a35dfe1”,
“arn”: “arn:aws:ec2:us-east-1:536751915275:instance/i-06bad98e885e1142d”,
“associate_public_ip_address”: “true”,
“availability_zone”: “us-east-1a”,
“cpu_core_count”: “1”,
“cpu_threads_per_core”: “1”,
“credit_specification.#”: “1”,
“credit_specification.0.cpu_credits”: “standard”,
“disable_api_termination”: “false”,
“ebs_block_device.#”: “0”,
“ebs_optimized”: “false”,
“ephemeral_block_device.#”: “0”,
“get_password_data”: “false”,
“iam_instance_profile”: “”,
“id”: “i-06bad98e885e1142d”,
“instance_state”: “running”,
“instance_type”: “t2.micro”,
“ipv6_addresses.#”: “0”,
“key_name”: “”,
“monitoring”: “false”,
“network_interface.#”: “0”,
“network_interface_id”: “eni-0beb13a621dcf2a3a”,
“password_data”: “”,
“placement_group”: “”,
“primary_network_interface_id”: “eni-0beb13a621dcf2a3a”,
“private_dns”: “ip-172-31-26-99.ec2.internal”,
“private_ip”: “172.31.26.99”,
“public_dns”: “ec2-174-129-51-81.compute-1.amazonaws.com”,
“public_ip”: “174.129.51.81”,
“root_block_device.#”: “1”,
“root_block_device.0.delete_on_termination”: “false”,
“root_block_device.0.iops”: “100”,
“root_block_device.0.volume_id”: “vol-023158925c24260c7”,
“root_block_device.0.volume_size”: “8”,
“root_block_device.0.volume_type”: “gp2”,
“security_groups.#”: “1”,
“security_groups.3814588639”: “default”,
“source_dest_check”: “true”,
“subnet_id”: “subnet-eda47ba5”,
“tags.%”: “1”,
“tags.Name”: “your-instance”,
“tenancy”: “default”,
“volume_tags.%”: “0”,
“vpc_security_group_ids.#”: “1”,
“vpc_security_group_ids.3753520163”: “sg-8691bff9”
},
“meta”: {
“e2bfb730-ecaa-11e6-8f88-34363bc7c4c0”: {
“create”: 600000000000,
“delete”: 1200000000000,
“update”: 600000000000
},
“schema_version”: “1”
},
“tainted”: false
},
“deposed”: [],
“provider”: “provider.aws”
}
},
“depends_on”: []
}
]
}
[root@node1 test-tf]# cat terraform.tfstate.backup
{
“version”: 3,
“terraform_version”: “0.11.10”,
“serial”: 2,
“lineage”: “31fce12f-ed57-76b7-9045-a3b90c165044”,
“modules”: [
{
“path”: [
“root”
],
“outputs”: {},
“resources”: {},
“depends_on”: []
}
]
}
[root@node1 test-tf]# terraform destroy
aws_instance.example: Refreshing state… (ID: i-06bad98e885e1142d)
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
– destroy
Terraform will perform the following actions:
– aws_instance.example
Plan: 0 to add, 0 to change, 1 to destroy.
Do you really want to destroy all resources?
Terraform will destroy all your managed infrastructure, as shown above.
There is no undo. Only ‘yes’ will be accepted to confirm.
Enter a value: yes
aws_instance.example: Destroying… (ID: i-06bad98e885e1142d)
aws_instance.example: Still destroying… (ID: i-06bad98e885e1142d, 10s elapsed)
aws_instance.example: Still destroying… (ID: i-06bad98e885e1142d, 20s elapsed)
aws_instance.example: Still destroying… (ID: i-06bad98e885e1142d, 30s elapsed)
aws_instance.example: Still destroying… (ID: i-06bad98e885e1142d, 40s elapsed)
aws_instance.example: Still destroying… (ID: i-06bad98e885e1142d, 50s elapsed)
aws_instance.example: Destruction complete after 1m0s
Destroy complete! Resources: 1 destroyed.
[root@node1 test-tf]# terraform plan
Refreshing Terraform state in-memory prior to plan…
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.
————————————————————————
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
+ create
Terraform will perform the following actions:
+ aws_instance.example
id: <computed>
ami: “ami-04ab2c6566a35dfe1”
arn: <computed>
associate_public_ip_address: <computed>
availability_zone: <computed>
cpu_core_count: <computed>
cpu_threads_per_core: <computed>
ebs_block_device.#: <computed>
ephemeral_block_device.#: <computed>
get_password_data: “false”
instance_state: <computed>
instance_type: “t2.micro”
ipv6_address_count: <computed>
ipv6_addresses.#: <computed>
key_name: <computed>
network_interface.#: <computed>
network_interface_id: <computed>
password_data: <computed>
placement_group: <computed>
primary_network_interface_id: <computed>
private_dns: <computed>
private_ip: <computed>
public_dns: <computed>
public_ip: <computed>
root_block_device.#: <computed>
security_groups.#: <computed>
source_dest_check: “true”
subnet_id: <computed>
tags.%: “1”
tags.Name: “your-instance”
tenancy: <computed>
volume_tags.%: <computed>
vpc_security_group_ids.#: <computed>
Plan: 1 to add, 0 to change, 0 to destroy.
————————————————————————
Note: You didn’t specify an “-out” parameter to save this plan, so Terraform
can’t guarantee that exactly these actions will be performed if
“terraform apply” is subsequently run.
