-
- Download Terraform binary package from:
https://www.terraform.io/downloads.html - Install Ansible
apt-get install ansible
- To verify an AWS profile and ensure Terraform has correct provider credentials,
install the AWS CLI and runaws configure
.The AWS CLI will then verify and save your AWS Access Key ID and Secret Access Key.
Those credentials are found on this page. - Copy the program into your bin folder:
-
sudo cp terraform /usr/local/bin
- Create subfolder an file for configuration code
mkdir terraform-aws-instance touch provider-aws.tf
The format of the configuration files are able to be in two formats: Terraform format (.tf) and JSON (.tf.json). The Terraform format is more human-readable, supports comments, and is the generally recommended format for most Terraform file
- Start terraform init:
# terraform init OUTPUT: Initializing the backend... Initializing provider plugins... - Checking for available provider plugins... - Downloading plugin for provider "aws" (hashicorp/aws) 2.54.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 = "~> 2.54" 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.
Terraform downloads the
aws
provider and installs it in a hidden subdirectory of the current working directory. - check your configuration with both commands:
terraform validate terraform fmt
- In the same directory as the
provider.tf
file you created, runterraform apply
. You should see output similar to below, though we’ve truncated some of the output to save space. -
# 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 will be created + resource "aws_instance" "example" { + ami = "ami-2757f631" + arn = (known after apply) + associate_public_ip_address = (known after apply) + availability_zone = (known after apply) + cpu_core_count = (known after apply) + cpu_threads_per_core = (known after apply) + get_password_data = false + host_id = (known after apply) + id = (known after apply) + instance_state = (known after apply) + instance_type = "t2.nano" + ipv6_address_count = (known after apply) + ipv6_addresses = (known after apply) + key_name = (known after apply) + network_interface_id = (known after apply) + password_data = (known after apply) + placement_group = (known after apply) + primary_network_interface_id = (known after apply) + private_dns = (known after apply) + private_ip = (known after apply) + public_dns = (known after apply) + public_ip = (known after apply) + security_groups = (known after apply) + source_dest_check = true + subnet_id = (known after apply) + tenancy = (known after apply) + volume_tags = (known after apply) + vpc_security_group_ids = (known after apply) + ebs_block_device { + delete_on_termination = (known after apply) + device_name = (known after apply) + encrypted = (known after apply) + iops = (known after apply) + kms_key_id = (known after apply) + snapshot_id = (known after apply) + volume_id = (known after apply) + volume_size = (known after apply) + volume_type = (known after apply) } + ephemeral_block_device { + device_name = (known after apply) + no_device = (known after apply) + virtual_name = (known after apply) } + network_interface { + delete_on_termination = (known after apply) + device_index = (known after apply) + network_interface_id = (known after apply) } + root_block_device { + delete_on_termination = (known after apply) + encrypted = (known after apply) + iops = (known after apply) + kms_key_id = (known after apply) + volume_id = (known after apply) + volume_size = (known after apply) + volume_type = (known after apply) } } 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... aws_instance.example: Still creating... [10s elapsed] aws_instance.example: Still creating... [20s elapsed] aws_instance.example: Creation complete after 27s [id=i-018ba2f7d5d35bc73] Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
- Now Terraform is general usable for projects!
- Check out the following github project with all needed project files:
https://github.com/odonzyk/terra-aws-ansible-wp-inst - After it is downloaded start the terraform process
terraform plan -out challenge01
terraform apply „challenge01“OUTPUT shows something like that:
aws_instance.challenge01: Creating... aws_eip.ip: Destruction complete after 1s aws_instance.challenge01: Still creating... [10s elapsed] aws_instance.challenge01: Still creating... [20s elapsed] aws_instance.challenge01: Creation complete after 27s [id=i-0f25c5661d027fba8] aws_eip.ip: Creating... aws_eip.ip: Provisioning with 'local-exec'... aws_eip.ip (local-exec): Executing: ["/bin/sh" "-c" "sleep 60; ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook -i '100.24.0.244', -u 'ubuntu' --private-key '/root/.ssh/aws' ./master.yml"] aws_eip.ip: Still creating... [10s elapsed] aws_eip.ip: Still creating... [20s elapsed] aws_eip.ip: Still creating... [30s elapsed] aws_eip.ip: Still creating... [40s elapsed] aws_eip.ip: Still creating... [50s elapsed] aws_eip.ip: Still creating... [1m0s elapsed] aws_eip.ip (local-exec): PLAY [all] ********************************************************************* aws_eip.ip (local-exec): TASK [Install python for Ansible] ********************************************** aws_eip.ip: Still creating... [1m10s elapsed] aws_eip.ip: Still creating... [1m20s elapsed] aws_eip.ip (local-exec): changed: [100.24.0.244] aws_eip.ip (local-exec): TASK [Update apt repos] ******************************************************** aws_eip.ip (local-exec): changed: [100.24.0.244] aws_eip.ip (local-exec): TASK [Installing LEMP] ********************************************************* aws_eip.ip: Still creating... [1m30s elapsed] aws_eip.ip: Still creating... [1m40s elapsed] aws_eip.ip: Still creating... [1m50s elapsed] aws_eip.ip: Still creating... [2m0s elapsed] aws_eip.ip: Still creating... [2m10s elapsed] aws_eip.ip: Still creating... [2m20s elapsed] aws_eip.ip (local-exec): changed: [100.24.0.244] aws_eip.ip (local-exec): TASK [Copy Nginx config to remote] ********************************************* aws_eip.ip: Still creating... [2m30s elapsed] aws_eip.ip (local-exec): changed: [100.24.0.244] aws_eip.ip (local-exec): TASK [Copy SQL dump script to remote] ****************************************** aws_eip.ip: Still creating... [2m40s elapsed] aws_eip.ip (local-exec): changed: [100.24.0.244] aws_eip.ip (local-exec): TASK [Copy install script to remote] ******************************************* aws_eip.ip (local-exec): changed: [100.24.0.244] aws_eip.ip (local-exec): TASK [Execute install script] ************************************************** aws_eip.ip: Still creating... [2m50s elapsed] aws_eip.ip: Still creating... [3m0s elapsed] aws_eip.ip (local-exec): changed: [100.24.0.244] aws_eip.ip (local-exec): PLAY RECAP ********************************************************************* aws_eip.ip (local-exec): 100.24.0.244 : ok=7 changed=7 unreachable=0 failed=0 aws_eip.ip: Creation complete after 3m5s [id=eipalloc-0ad3cccd4b55f681d] Apply complete! Resources: 2 added, 0 changed, 1 destroyed. The state of your infrastructure has been saved to the path below. This state is required to modify and destroy your infrastructure, so keep it safe. To inspect the complete state use the `terraform show` command. State path: terraform.tfstate Outputs: public_ip = 100.24.0.244
- Now try to connect with
ssh -i ~/.ssh/(your-private-key) ubuntu@(YOURE public-key from the output above)!
- Don’t forget to destroy all of them, if you don’t need it on aws anymore!
Delete it with:terraform destroy [Enter]
- Download Terraform binary package from: