Skip to content

Instantly share code, notes, and snippets.

@solarce
Last active February 4, 2022 04:04
Show Gist options
  • Save solarce/ec87ac9ce10ccb92b0b5 to your computer and use it in GitHub Desktop.
Save solarce/ec87ac9ce10ccb92b0b5 to your computer and use it in GitHub Desktop.
terraform.io example template for ec2 instance with tags
# The various ${var.foo} come from variables.tf
# Specify the provider and access details
provider "aws" {
region = "${var.aws_region}"
access_key = "${var.aws_access_key}"
secret_key = "${var.aws_secret_key}"
}
resource "aws_instance" "web" {
# The connection block tells our provisioner how to
# communicate with the resource (instance)
connection {
# The default username for our AMI
user = "ubuntu"
# The path to your keyfile
key_file = "${var.key_path}"
}
# subnet ID for our VPC
subnet_id = "${var.subnet_id}"
# the instance type we want, comes from rundeck
instance_type = "${var.instance_type}"
# Lookup the correct AMI based on the region
# we specified
ami = "${lookup(var.aws_amis, var.aws_region)}"
# The name of our SSH keypair you've created and downloaded
# from the AWS console.
#
# https://console.aws.amazon.com/ec2/v2/home?region=us-west-2#KeyPairs:
#
key_name = "${var.key_name}"
# We set the name as a tag
tags {
"Name" = "${var.instance_name"
}
}
variable "key_name" {
description = "Name of the SSH keypair to use in AWS."
}
variable "key_path" {
description = "Path to the private portion of the SSH key specified."
}
variable "aws_region" {
description = "AWS region to launch servers."
}
variable "aws_access_key" {
decscription = "AWS Access Key"
}
variable "aws_secret_key" {
description = "AWS Secret Key"
}
variable "subnet_id" {
description = "Subnet ID to use in VPC"
}
variable "instance_type" {
description = "Instance type"
}
variable "instance_name" {
description = "Instance Name"
}
# Ubuntu Precise 12.04 LTS (x64)
variable "aws_amis" {
default = {
"eu-west-1": "ami-b1cf19c6",
"us-east-1": "ami-de7ab6b6",
"us-west-1": "ami-3f75767a",
"us-west-2": "ami-21f78e11"
}
}
@kamaljeetrathi
Copy link

how to give dynamic name to the aws-ec2 instance in this tf file.

@ybubnov
Copy link

ybubnov commented Aug 9, 2017

There is a missing closing curly bracket in the ec2.tf on 41 line.

Copy link

ghost commented Aug 10, 2017

variable "aws_access_key" {
decscription = "AWS Access Key"
}

Typo mistake "description = AWS Access Key"

@aman105
Copy link

aman105 commented Aug 20, 2018

tags {
"Name" = "${var.instance_name}"
}

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