Skip to content

Instantly share code, notes, and snippets.

@jftuga
Last active July 26, 2021 22:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jftuga/936500b3b378955b50420d7ffd37029a to your computer and use it in GitHub Desktop.
Save jftuga/936500b3b378955b50420d7ffd37029a to your computer and use it in GitHub Desktop.
Terraform and EC2 Spot Instances
#!/bin/bash
T=terraform
${T} init
${T} fmt
${T} validate
# to deploy:
${T} plan
${T} apply -auto-approve
${T} show
# to deploy with different values:
${T} apply -var "inst_type=t3a.small" -var "spot_price=0.004"
# to get public ip:
${T} show --json | jq -r ".values.root_module.resources | .[] .values.public_ip"
# alternatively, use 'output' directives within the .tg file
${T} output --json
# terminate instance
${T} apply -auto-approve -destroy
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 3.27"
}
}
required_version = ">= 0.14.9"
}
provider "aws" {
profile = "ec2-admin"
region = "ap-south-1"
default_tags {
tags = {
Name = "AmazonLinux2"
}
}
}
resource "aws_spot_instance_request" "amazon_linux" {
key_name = "AP-South-1"
associate_public_ip_address = true
wait_for_fulfillment = true
ami = "ami-011c99152163a87ae"
spot_price = "0.002"
spot_type = "one-time"
instance_type = "t3a.micro"
}
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 3.27"
}
}
required_version = ">= 0.14.9"
}
variable "aws_region" {
description = "AWS region"
type = string
default = "ap-south-1"
}
variable "ssh_key_name" {
description = "ssh pem key file"
type = string
default = "AP-South-1"
}
variable ami {
description = "Amazon Machine Image identifier, unqiue to each AZ"
type = string
default = "ami-011c99152163a87ae"
}
variable inst_type {
description = "EC2 instance type"
type = string
default = "t3a.micro"
}
variable spot_price {
description = "max spot price"
type = string
default = "0.002"
}
provider "aws" {
profile = "ec2-admin"
region = var.aws_region
default_tags {
tags = {
Name = "AmazonLinux2"
}
}
}
resource "aws_spot_instance_request" "amazon_linux" {
key_name = var.ssh_key_name
associate_public_ip_address = true
wait_for_fulfillment = true
ami = var.ami
spot_price = var.spot_price
spot_type = "one-time"
instance_type = var.inst_type
}
output "instance_public_ip" {
description = "Public IP address of the EC2 instance"
value = aws_spot_instance_request.amazon_linux.public_ip
}
output "instance_id" {
description = "ID of the EC2 instance"
value = aws_spot_instance_request.amazon_linux.id
}
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 3.27"
}
}
required_version = ">= 0.14.9"
}
variable "aws_region" {
description = "AWS region"
type = string
default = "ap-south-1"
}
variable "ssh_key_name" {
description = "ssh pem key file"
type = string
default = "AP-South-1"
}
variable ami {
description = "Amazon Machine Image identifier, unqiue to each AZ"
type = string
default = "ami-011c99152163a87ae"
}
variable inst_type {
description = "EC2 instance type"
type = string
default = "t3a.micro"
}
variable spot_price {
description = "max spot price"
type = string
default = "0.002"
}
provider "aws" {
profile = "ec2-admin"
region = var.aws_region
default_tags {
tags = {
Name = "AmazonLinux2"
}
}
}
resource "aws_spot_instance_request" "amazon_linux" {
key_name = var.ssh_key_name
associate_public_ip_address = true
wait_for_fulfillment = true
ami = var.ami
spot_price = var.spot_price
spot_type = "one-time"
instance_type = var.inst_type
}
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 3.27"
}
}
required_version = ">= 0.14.9"
}
provider "aws" {
profile = "ec2-admin"
region = "us-east-1"
default_tags {
tags = {
Name = "AmazonLinux2"
}
}
}
resource "aws_spot_instance_request" "amazon_linux" {
key_name = "US-East-1.pem"
associate_public_ip_address = true
wait_for_fulfillment = true
ami = "ami-0dc2d3e4c0f9ebd18"
spot_price = "0.002"
spot_type = "one-time"
instance_type = "t3a.nano"
availability_zone = "us-east-1b"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment