๓ฐ—›
๐Ÿงช
๐Ÿ”ฌ
โš—
{
"AWSTemplateFormatVersion" : "2010-09-09",
"Description" : "AWS CloudFormation Sample Template EC2InstanceWithSecurityGroupSample: Crea
"Parameters" : {
"KeyName": {
"Description" : "Name of an existing EC2 KeyPair to enable SSH access to the instance",
"Type": "AWS::EC2::KeyPair::KeyName",
"ConstraintDescription" : "must be the name of an existing EC2 KeyPair."
},
}
}
resource aws_instance "web" {
ami = data.aws_ami.ubuntu.id
instance_type = "t2.micro"
tags = {
Name = "HelloWorld"
}
}
resource aws_instance "catapp" {
ami = data.aws_ami.ubuntu.id
instance_type = var.instance_type
tags = {
Name = "${var.prefix}-meow"
}
terraform plan
terraform plan
terraform plan
terraform plan
terraform plan
"name": "{ "Fn::Join" : [ "-", [ PilotServerName, vm ] ] }",
name = "${var.PilotServerName}-vm"
# Basic Terraform Commands
terraform version
terraform help
terraform init
terraform plan
terraform apply
terraform destroy
$ terraform help
Usage: terraform [-version] [-help] <command> [args]
...
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
graph Create a visual graph of Terraform resources
terraform subcommand help
resource aws_vpc "main" {
cidr_block = "10.0.0.0/16"
instance_tenancy = "dedicated"
}
# This is a line comment.
/* This is a block comment.
Block comments can span multiple lines.
The comment ends with this symbol: */
*.tf *.tfvars
$ terraform init
Initializing provider plugins...
- Checking for available provider plugins...
- Downloading plugin for provider "aws" (hashicorp/aws) 2.35.0...
...
provider.aws: version = "~> 2.35"
Terraform has been successfully initialized!
$ terraform plan
An execution plan has been generated and is shown below.
Terraform will perform the following actions:
# aws_vpc.main will be created
+ resource "aws_vpc" "main" {
+ arn = (known after apply)
+ cidr_block = "10.0.0.0/16"
...
+ instance_tenancy = "dedicated"
}
terraform plan
variable "prefix" {
description = "This prefix will be included in the name of most resourc
}
variable "instance_tenancy" {
description = "A tenancy option for instances launched into the VPC."
default = "dedicated"
}
1. Command line flag - run as a
command line switch
2. Configuration file - set in
your terraform.tfvars file
3. Environment variable - part of
your shell environment
4. Default Config - default value
in variables.tf
5. User manual entry - if not
specified, prompt the user
for entry
๓ฐ˜Ÿ
๓ฐ˜Ÿ
๐Ÿ›‘
๐Ÿ“
terraform init
terraform plan
resource type "name" {
parameter = "foo"
parameter2 = "bar"
list = ["one", "two", "three"]
}
aws_instance
provider "aws" {
version = "=2.35.0"
}
- = (or no operator): exact version equality
- !=: version not equal
- \>, >=, <, <=: version comparison
- ~>: pessimistic constraint, constraining both the oldest and newest
version allowed. ~> 0.9 is equivalent to >= 0.9, < 1.0, and ~> 0.8.4
is equivalent to >= 0.8.4, < 0.9
$ terraform apply
An execution plan has been generated and is shown below.
Terraform will perform the following actions:
# aws_vpc.main will be created
+ resource "aws_vpc" "main" {
+ cidr_block = "10.0.0.0/16"
+ instance_tenancy = "dedicated"
...
+ tags = {
+ "Name" = "main"
}
}
Plan: 1 to add, 0 to change, 0 to destroy.
terraform apply
$ terraform destroy
An execution plan has been generated and is shown below.
Terraform will perform the following actions:
# aws_vpc.main will be destroyed
- resource "aws_vpc" "main" {
- cidr_block = "10.0.0.0/16" -> null
- instance_tenancy = "dedicated" -> null
...
- tags = {
- "Name" = "main"
} -> null
}
Plan: 0 to add, 0 to change, 1 to destroy.
terraform destroy
terraform fmt
data "aws_ami" "ubuntu" {
most_recent = true
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-trusty-14.04-amd64-server-*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
owners = ["099720109477"] # Canonical
}
resource aws_key_pair "my-keypair" {
key_name = "my-keypair"
public_key = file(var.public_key)
}
resource "aws_instance" "web" {
ami = data.aws_ami.ubuntu.id
instance_type = "t2.micro"
key_name = aws_key_pair.my-keypair.name
.tf
main.tf
variables.tf
outputs.tf
# This is the main.tf file.
resource aws_vpc "main" {
cidr_block = var.cidr_block
instance_tenancy = var.instance_tenancy
}
resource aws_subnet "main" {
vpc_id = aws_vpc.main.id
cidr_block = var.cidr_block
}
}
...
variable "cidr_block" {
description = "The address space that is used within the VPC. Changing this forces a new res
}
variable "instance_tenancy" {
description = "A tenancy option for instances launched into the VPC. Acceptable values are '
default = "dedicated"
}
output "catapp_url" {
value = "http://${aws_route53_record.hashicat.fqdn}"
}
output "private_key" {
value = "${tls_private_key.hashicat.private_key_pem}"
}
๓ฐ˜Ÿ
terraform apply
๐Ÿ›‘
๐Ÿ“
provisioner "file" {
source = "files/"
destination = "/home/${var.admin_username}/"
connection {
type = "ssh"
user = var.username
private_key = file(var.ssh_key)
host = ${self.ip}
}
}
provisioner "remote-exec" {
inline = [
"sudo chown -R ${var.admin_username}:${var.admin_username} /var/www/html",
"chmod +x *.sh",
"PLACEHOLDER=${var.placeholder} WIDTH=${var.width} HEIGHT=${var.height} PREFIX=${var.prefi
]
...
}
๓ฐ˜Ÿ
๐Ÿ›‘
๐Ÿ“
{
"terraform_version": "0.12.7",
"serial": 14,
"lineage": "452b4191-89f6-db17-a3b1-4470dcb00607",
"outputs": {
"catapp_url": {
"value": "http://go-hashicat-5c0265179ccda553.workshop.aws.hashidemos.io",
"type": "string"
},
}
}
terraform refresh
+ create
- destroy
-/+ replace
~ update in-place
๓ฐ˜Ÿ