Nic Wortel
@nicwortel
After this talk, you will...
Providers are plugins that Terraform uses to interact with infrastructure providers
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.16"
}
}
}
variable "aws_access_key" {
type = string
sensitive = true
}
variable "aws_secret_key" {
type = string
sensitive = true
}
provider "aws" {
region = "eu-central-1"
access_key = var.aws_access_key
secret_key = var.aws_secret_key
}
# terraform.tfvars
aws_access_key = "xxxxx"
aws_secret_key = "xxxxxxx"
resource "aws_instance" "vm" {
ami = "ami-06dd92ecc74fdfb36"
instance_type = "t2.micro"
tags = {
Name = "DemoInstance"
}
}
resource "aws_instance" "vm" {
ami = "ami-06dd92ecc74fdfb36"
instance_type = "t2.micro"
tags = {
Name = "DemoInstance"
}
}
$ terraform import aws_instance.vm i-07d8d81553bbf38be
resource "aws_instance" "vm" {
- ami = "ami-0aa6457dc2d115893" # 20.04 LTS
+ ami = "ami-06461d2b867abebf0" # 22.04 LTS
instance_type = "t2.micro"
tags = {
Name = "DemoInstance"
}
}
$ terraform plan
Terraform will perform the following actions:
# aws_instance.vm must be replaced
-/+ resource "aws_instance" "vm" {
~ ami = "ami-0aa6457dc2d115893" -> "ami-06461d2b867abebf0" # forces replacement
~ id = "i-07d8d81553bbf38be" -> (known after apply)
~ public_ip = (known after apply)
}
resource "aws_instance" "vm" {
ami = "ami-06461d2b867abebf0"
instance_type = "t2.micro"
tags = {
Name = "DemoInstance"
}
+ lifecycle {
+ create_before_destroy = true
+ }
}
data "aws_ami" "ubuntu" {
most_recent = true
owners = ["amazon"]
filter {
name = "name"
values = [
"ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-*"
]
}
}
resource "aws_instance" "vm" {
- ami = "ami-06461d2b867abebf0"
+ ami = data.aws_ami.ubuntu.id
instance_type = "t2.micro"
tags = {
Name = "DemoInstance"
}
}
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.16"
}
+ cloudflare = {
+ source = "cloudflare/cloudflare"
+ version = "~> 3.0"
+ }
}
}
+variable "cloudflare_api_token" {
+ type = string
+ sensitive = true
+}
provider "aws" {
region = "eu-central-1"
access_key = var.aws_access_key
secret_key = var.aws_secret_key
}
+provider "cloudflare" {
+ api_token = var.cloudflare_api_token
+}
# terraform.tfvars
aws_access_key = "xxxx"
aws_secret_key = "xxxx"
+cloudflare_api_token = "xxxx"
resource "aws_instance" "vm" {
# ...
}
data "cloudflare_zone" "nicwortel" {
name = "nicwortel.nl"
}
resource "cloudflare_record" "demo" {
zone_id = data.cloudflare_zone.nicwortel.id
name = "demo"
type = "A"
value = aws_instance.vm.public_ip
proxied = true
}
data "aws_ami" "ubuntu" {
most_recent = true
owners = ["amazon"]
filter {
name = "name"
values = [
- "ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-*"
+ "ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"
]
}
}