Slide 1

Slide 1 text

Copyright © 2020 HashiCorp Understanding the AWS Provider for Terraform

Slide 2

Slide 2 text

Kerim Satirli (He/Him) Developer Advocate at HashiCorp

Slide 3

Slide 3 text

@ksatirli on GitHub and Twitter Developer Advocate at HashiCorp

Slide 4

Slide 4 text

Infrastructure as Code ▪ executable documentation ▪ enables collaboration ▪ safe and predictable

Slide 5

Slide 5 text

Agenda Introducing Terraform basic concepts Managing AWS with Terraform provisioning resources Scaling Terraform expanding your knowledge

Slide 6

Slide 6 text

Introducing Terraform

Slide 7

Slide 7 text

Terraform 125+ Official Providers AWS, GCP, Docker, etc. 175+ Community Providers 1Password, Stripe, Unifi, etc.

Slide 8

Slide 8 text

HashiCorp Configuration Language CODE EDITOR service { key = "value" }

Slide 9

Slide 9 text

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 }

Slide 10

Slide 10 text

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 }

Slide 11

Slide 11 text

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" } }

Slide 12

Slide 12 text

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.

Slide 13

Slide 13 text

TERMINAL > tree .terraform/ └── plugins └── darwin_amd64 ├── lock.json └── terraform-provider-docker_v2.7.0_x4

Slide 14

Slide 14 text

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" }

Slide 15

Slide 15 text

Command: terraform fmt TERMINAL > terraform fmt main.tf

Slide 16

Slide 16 text

Command: terraform validate TERMINAL > terraform fmt main.tf > terraform validate Success! The configuration is valid.

Slide 17

Slide 17 text

Command: terraform help TERMINAL > terraform help Usage: terraform [-version] [-help] [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

Slide 18

Slide 18 text

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.

Slide 19

Slide 19 text

Command: terraform apply TERMINAL > terraform apply "docker.tfplan"

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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" }

Slide 23

Slide 23 text

Command: terraform plan TERMINAL > terraform plan -out="docker.tfplan" var.image_version version of Docker Image to pull Enter a value: latest

Slide 24

Slide 24 text

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.

Slide 25

Slide 25 text

Variable Definition Files CODE EDITOR image_version = "latest"

Slide 26

Slide 26 text

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"

Slide 27

Slide 27 text

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 }

Slide 28

Slide 28 text

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.

Slide 29

Slide 29 text

Managing AWS with Terraform

Slide 30

Slide 30 text

AWS Provider for Terraform hashi.co/tf-aws-provider-changelog

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

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 }

Slide 33

Slide 33 text

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" }

Slide 34

Slide 34 text

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" }

Slide 35

Slide 35 text

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" }

Slide 36

Slide 36 text

EC2 Instance CODE EDITOR resource "aws_instance" "hug_demo" { ami = var.ami_id instance_type = var.instance_type availability_zone = var.availability_zone }

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

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.

Slide 39

Slide 39 text

EC2 Instance ap-south-1.console.aws.amazon.com/ec2

Slide 40

Slide 40 text

EBS Volume CODE EDITOR resource "aws_instance" "hug_demo" { ... } resource "aws_ebs_volume" "hug_demo" { ... } resource "aws_volume_attachment" "hug_demo" { ... }

Slide 41

Slide 41 text

EBS Volume CODE EDITOR output "volume_device_name" { value = aws_volume_attachment.hug_demo.device_name }

Slide 42

Slide 42 text

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

Slide 43

Slide 43 text

Command: terraform output TERMINAL > terraform output volume_device_name = /dev/sdh

Slide 44

Slide 44 text

Command: terraform output TERMINAL > terraform output volume_device_name /dev/sdh

Slide 45

Slide 45 text

Command: terraform destroy TERMINAL > terraform plan -destroy -out="aws.tfplan"

Slide 46

Slide 46 text

Command: terraform destroy TERMINAL > terraform apply "aws.tfplan"

Slide 47

Slide 47 text

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.

Slide 48

Slide 48 text

Terraform lifecycle ▪ terraform init ▪ terraform fmt ▪ terraform validate ▪ terraform plan -out="terraform.tfplan" ▪ terraform apply "terraform.tfplan" ▪ terraform plan -destroy

Slide 49

Slide 49 text

Scaling Terraform

Slide 50

Slide 50 text

Importing existing resources CODE EDITOR resource "aws_s3_bucket" "hug_demo" { bucket = "hug-demo" }

Slide 51

Slide 51 text

Command: terraform import TERMINAL > terraform import \ aws_s3_bucket.hug_demo "hug-demo"

Slide 52

Slide 52 text

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!

Slide 53

Slide 53 text

Review ▪ Providers ▪ Lifecycle ▪ State

Slide 54

Slide 54 text

Materials ▪ slides: hashi.co/tf-basics-for-aws ▪ code: hashi.co/tf-basics-for-aws-code ▪ guides: hashi.co/tf-learn-aws ▪ forums: hashi.co/tf-forum-aws

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

Thank You [email protected]