Skip to content

Instantly share code, notes, and snippets.

@elvisgiv
Last active April 2, 2020 11:23
Show Gist options
  • Save elvisgiv/3ec1944f680a02625f5f03f215262536 to your computer and use it in GitHub Desktop.
Save elvisgiv/3ec1944f680a02625f5f03f215262536 to your computer and use it in GitHub Desktop.

scripts for launch instance

# main.tf

variable "aws_access_key" {}
variable "aws_secret_key" {}
variable "instance_type" {}

provider "aws" {
  access_key = var.aws_access_key
  secret_key = var.aws_secret_key
  profile    = "default"
  region     = "us-east-2"
}

data "aws_ami" "ubuntu" {
  most_recent      = true

  filter {
    name   = "name"
    values = ["ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-*"]
  }

  filter {
      name   = "virtualization-type"
      values = ["hvm"]
  }

  owners = ["099720109477"] # Canonical
}

resource "aws_instance" "elvis_test" {
  ami           = "${data.aws_ami.ubuntu.id}"
  instance_type = var.instance_type
  // name of instance
  tags = {
    Name = "test_name"
  }

}

// add elasticIP to instance
resource "aws_eip" "ip" {
  vpc      = true
  instance = aws_instance.elvis_test.id
}

output "image_id" {
    value = "${data.aws_ami.ubuntu.id}"
}
# terraform.tfvars

aws_access_key = "YOUR_AWS_ACCESS_KEY"
aws_secret_key = "YOUR_AWS_SECRET_KEY"
instance_type = "type_of_aws_instance" # t2.micro etc
# versions.tf

terraform {
  required_version = ">= 0.12"
}

commands

terraform init
terraform apply
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment