Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Understanding the AWS Provider for Terraform

Understanding the AWS Provider for Terraform

In this talk, I look at the basics concepts of HashiCorp Terraform and explain them, using the Docker Provider. Then, I show how to use Terraform to manage AWS resources efficiently and reliably.

This version of the presentation was given at a virtual event for the Bangalore HashiCorp User Group in May 2020.

---

Companion Code: github.com/ksatirli/understanding-the-aws-provider-for-terraform

Kerim Satirli

May 02, 2020
Tweet

More Decks by Kerim Satirli

Other Decks in Technology

Transcript

  1. HashiCorp Configuration Language CODE EDITOR service "http" "web_proxy" { listen_addr

    = "127.0.0.1:8080" process "server" { command = ["proxy-app", "server"] } } variable "port" { description = "Port for web_proxy" default = 8080 }
  2. HashiCorp Configuration Language CODE EDITOR service "http" "web_proxy" { listen_addr

    = "127.0.0.1:${var.port}" process "server" { command = ["proxy-app", "server"] } } variable "port" { description = "Port for web_proxy" default = 8080 }
  3. Provider set-up CODE EDITOR provider "docker" { version = "~>

    2.7" host = "tcp://localhost:2376" registry_auth { address = "registry.hub.docker.com" config_file = "/Users/me/.docker/config.json" } }
  4. TERMINAL > terraform init Initializing the backend... Initializing provider plugins...

    - Checking for available provider plugins... - Downloading plugin for provider "docker" (terraform-providers/docker) 2.7.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. 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.
  5. Docker Container CODE EDITOR resource "docker_image" "hello_world" { name =

    "hello-world:${var.image_version}" } variable "image_version" { type = string description = "version of Docker Image to pull" default = "latest" }
  6. Command: terraform validate TERMINAL > terraform fmt main.tf > terraform

    validate Success! The configuration is valid.
  7. Command: terraform help TERMINAL > terraform help 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. Common commands: apply Builds or changes infrastructure destroy Destroy Terraform-managed infrastructure fmt Rewrites config files to canonical format output Read an output from a state file
  8. Command: terraform plan TERMINAL > terraform plan -out="docker.tfplan" Terraform will

    perform the following actions: # docker_image.hello_world will be created + resource "docker_image" "hello_world" { + id = (known after apply) + latest = (known after apply) + name = "hello-world:latest" } Plan: 1 to add, 0 to change, 0 to destroy.
  9. Command: terraform apply TERMINAL docker_image.hello_world: Creating... docker_image.hello_world: Still creating... docker_image.hello_world:

    Creation complete after 5s Apply complete! Resources: 1 added, 0 changed, 0 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. State path: terraform.tfstate
  10. Terraform State ▪ maps real-world resources to your configuration ▪

    keeps track of (resource) metadata ▪ improves performance for large infrastructures ▪ stored locally (by default), can be stored remotely
  11. Docker Container CODE EDITOR resource "docker_image" "hello_world" { name =

    "hello-world:${var.image_version}" } variable "image_version" { type = string description = "version of Docker Image to pull" default = "latest" }
  12. Command: terraform plan TERMINAL 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. 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.
  13. Variable Definition Files ▪ contain key-value definitions of variables ▪

    automatically loaded if named: ▪ "terraform.tfvars" or "terraform.tfvars.json" ▪ ".auto.tfvars" or ".auto.tfvars.json"
  14. Docker Container CODE EDITOR resource "docker_image" "hello_world" { name =

    "hello-world:${var.image_version}" } resource "docker_container" "hello_world" { name = "hello-world" image = docker_image.hello_world.name }
  15. Command: terraform apply TERMINAL docker_container.hello_world: Creating... docker_container.hello_world: Creation complete after

    0s [id=3d5...966] Apply complete! Resources: 1 added, 0 changed, 0 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.
  16. AWS Provider CODE EDITOR provider "aws" { version = "~>

    2.60" region = "ap-south-1" access_key = "AKIAIOSFODNN7EXAMPLE" secret_access_key = "wJalrXUtnFEMI/K7MDEN" }
  17. AWS Provider CODE EDITOR provider "aws" { version = "~>

    2.60" region = "ap-south-1" access_key = var.aws_access_key secret_access_key = var.aws_secret_access_key }
  18. AWS Provider CODE EDITOR provider "aws" { version = "~>

    2.60" region = "ap-south-1" shared_credentials_file = "/Users/me/.aws/creds" secret_access_key = "hug-demo" }
  19. AWS Provider TERMINAL > export AWS_ACCESS_KEY_ID="AKIAIOSFODNN7EXAMPLE" > export AWS_SECRET_ACCESS_KEY="wJalrXUtnFEMI/K7MDEN" CODE

    EDITOR provider "aws" { version = "~> 2.60" region = "ap-south-1" profile. = "hug-demo" }
  20. EC2 Instance CODE EDITOR variable "ami_id" { type = string

    description = "AMI ID to use" default = "ami-0470e33cd681b2476" } variable "instance_type" { type = string description = "Instance type to use" default = "t2.micro" }
  21. EC2 Instance CODE EDITOR resource "aws_instance" "hug_demo" { ami =

    var.ami_id instance_type = var.instance_type availability_zone = var.availability_zone }
  22. EC2 Instance TERMINAL > terraform plan -out="aws.tfplan" Terraform will perform

    the following actions: # aws_instance.hug_demo will be created + resource "aws_instance" "hug_demo" Plan: 1 to add, 0 to change, 0 to destroy. This plan was saved to: aws.tfplan
  23. EC2 Instance TERMINAL > terraform apply "aws.tfplan" aws_instance.hug_demo: Creating... aws_instance.hug_demo:

    Still creating... [10s elapsed] aws_instance.hug_demo: Still creating... [20s elapsed] aws_instance.hug_demo: Creation complete after 22s Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
  24. EBS Volume CODE EDITOR resource "aws_instance" "hug_demo" { ... }

    resource "aws_ebs_volume" "hug_demo" { ... } resource "aws_volume_attachment" "hug_demo" { ... }
  25. Command: terraform apply TERMINAL > terraform apply "aws.tfplan" aws_ebs_volume.hug_demo: Refreshing

    state... aws_instance.hug_demo: Refreshing state... aws_volume_attachment.hug_demo: Refreshing state... Apply complete! Resources: 0 added, 0 changed, 0 destroyed. Outputs: volume_device_name = /dev/sdh
  26. Command: terraform destroy TERMINAL > terraform apply "aws.tfplan" aws_ebs_volume.hug_demo: Destroying...

    aws_instance.hug_demo: Destroying... aws_volume_attachment.hug_demo: Destroying… Apply complete! Resources: 0 added, 0 changed, 3 destroyed.
  27. Terraform lifecycle ▪ terraform init ▪ terraform fmt ▪ terraform

    validate ▪ terraform plan -out="terraform.tfplan" ▪ terraform apply "terraform.tfplan" ▪ terraform plan -destroy
  28. Command: terraform import TERMINAL > terraform import \ aws_s3_bucket.hug_demo "hug-demo"

    aws_s3_bucket.hug_demo: Importing from ID "hug-demo"... aws_s3_bucket.hug_demo: Import prepared! Prepared aws_s3_bucket for import aws_s3_bucket.hug_demo: Refreshing state... [id=hug- demo] Import successful!