Difference between revisions of "Terraform resource: aws instance"

From wikieduonline
Jump to navigation Jump to search
 
(48 intermediate revisions by 3 users not shown)
Line 1: Line 1:
https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/instance
+
<code>[[aws_instance]]</code> ([https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/instance ref])
  
== Basic example ==
+
== Arguments ==
 +
* <code>[[subnet_id]]</code>
 +
* <code>[[instance_type]]</code>
 +
* <code>[[ami]]</code>
 +
* <code>[[key_name]]</code>
 +
* <code>[[vpc_security_group_ids]]</code>
 +
* <code>[[metadata_options]]</code>: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/instance#metadata-options
 +
** <code>[[http_tokens]] = required</code>
 +
 
 +
== [[Terraform examples|Examples]] ==
 +
* [[AWS instance example with aws_ami lookup and key_name]]:  <code>[[aws_instance]]</code>, <code>[[data.]][[aws_ami]]</code>
 +
* [[AWS instance example with aws_ami lookup]]:  <code>[[aws instance]]</code>, <code>[[aws_ami]]</code>
 +
* [[AWS instance example: main.tf]]: <code>[[aws instance]]</code>
 +
* [[AWS instance example creating VPC]]: <code>[[aws_vpc]]</code>
 +
 
 +
== Examples ==
  
 
  [[resource]] "[[aws_instance]]" "MYexample" {
 
  [[resource]] "[[aws_instance]]" "MYexample" {
 
   [[ami]]          = "[[ami-08d70e59c07c61a3a]]"
 
   [[ami]]          = "[[ami-08d70e59c07c61a3a]]"
   [[instance_type]] = "[[t2.micro]]"  
+
  [[instance_type]] = "[[t3.micro]]"
 +
 +
  [[tags]] = {
 +
    [[Name]] = "your_name_here"
 +
  }
 +
}
 +
 +
See also: [[AWS instance example with aws_ami lookup]]
 +
 
 +
[[resource]] "[[aws_instance]]" "MYexample" {
 +
  [[ami]]          = "[[ami-08d70e59c07c61a3a]]"
 +
   [[instance_type]] = "[[t2.micro]]"
 +
  [[associate_public_ip_address]] = "yes"
 +
 
 
   
 
   
 
   tags = {
 
   tags = {
Line 11: Line 39:
 
   }
 
   }
 
  }
 
  }
 
See also: [[AWS instance example with aws_ami lookup]]
 
  
  
Line 20: Line 46:
 
  }
 
  }
  
== [[Terraform examples|Examples]] ==
+
 
* [[AWS instance example with aws_ami lookup and key_name]]: <code>[[aws_instance]]</code>, <code>[[aws_ami]]</code>
+
resource "aws_instance" "myUbuntuMicroInstance" {
* [[AWS instance example with aws_ami lookup]]: <code>[[aws instance]]</code>, <code>[[aws_ami]]</code>
+
  ami          = [[data.]]aws_ami.ubuntu.id
* [[AWS instance example: main.tf]]: <code>[[aws instance]]</code>
+
  .../...
* [[AWS instance example creating VPC]]
+
 
 +
 
 +
resource "aws_instance" "my-instance" {
 +
  count        = var.instance_count
 +
  ami          = [[lookup]](var.ami,var.aws_region)
 +
  instance_type = var.instance_type
 +
  key_name      = aws_key_pair.terraform-demo.key_name
 +
  user_data    = file("install_apache.sh")
 +
 +
  tags = {
 +
    Name  = "Terraform-${count.index + 1}"
 +
    Batch = "5AM"
 +
  }
 +
}
 +
 
 +
== Outputs ==
 +
 
 +
  output "instance_ip_addr" {
 +
  value = [[aws_instance.]]your_server[[.private_ip]]
 +
  }
 +
 
 +
== Errors ==
 +
* [[Error: creating EC2 Instance: UnauthorizedOperation: You are not authorized to perform this operation]]
  
 
== aws_instance arguments ==  
 
== aws_instance arguments ==  
* <code>[[ami]]</code>
+
* <code>[[Amazon AMI|ami]]</code>
 
* <code>[[instance_type]]</code>
 
* <code>[[instance_type]]</code>
 
* <code>[[key_name]]</code> <ref>https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/instance#key_name</ref>
 
* <code>[[key_name]]</code> <ref>https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/instance#key_name</ref>
 
* <code>[[aws_key_pair]]</code>
 
* <code>[[aws_key_pair]]</code>
* <code>[[availability_zone]]</code>
+
* <code>[[availability_zone]]</code> (optional)
 
* <code>[[monitoring]]</code>
 
* <code>[[monitoring]]</code>
 
* <code>[[associate_public_ip_address]]</code>
 
* <code>[[associate_public_ip_address]]</code>
* <code>[[tenancy]]</code>
+
* <code>[[subnet_id]], [[public_ip]]</code>
* <code>[[subnet_id]]</code>
+
* <code>[[aws_security_group]]: [[vpc_security_group_ids]]</code>: (Optional, [[list]]) https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/instance#vpc_security_group_ids
* <code>[[public_ip]]</code>
 
* <code>[[vpc_security_group_ids]]</code>: (Optional, [[list]]) https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/instance#vpc_security_group_ids
 
 
* <code>[[root_block_device]]</code>
 
* <code>[[root_block_device]]</code>
 
* <code>[[iops]]</code>
 
* <code>[[iops]]</code>
* <code>[[iam_instance_profile]]</code>
+
* <code>[[iam_instance_profile]]</code> (Optional)
 
* <code>[[user_data]]</code>
 
* <code>[[user_data]]</code>
 
* <code>[[credit_specification]]</code>
 
* <code>[[credit_specification]]</code>
 
* <code>[[lifecycle]]</code>
 
* <code>[[lifecycle]]</code>
* <code>[[vpc_security_group_ids]]</code>
+
* <code>[[tenancy]]</code>
 +
* <code>[[instance_state]]</code>
  
 
== Related terms ==
 
== Related terms ==
Line 50: Line 97:
 
* <code>[[aws ec2 run-instances]]</code>
 
* <code>[[aws ec2 run-instances]]</code>
 
* <code>[[aws ec2 describe-key-pairs]]</code>
 
* <code>[[aws ec2 describe-key-pairs]]</code>
* [[Terraform examples]]
 
 
* <code>[[source (Terraform modules)]]</code>
 
* <code>[[source (Terraform modules)]]</code>
 
* [[Terraform: Data sources]]
 
* [[Terraform: Data sources]]
 
* <code>[[Terraform: VPCIdNotSpecified]]</code>
 
* <code>[[Terraform: VPCIdNotSpecified]]</code>
* [[Terraform module: ec2-instance]]
+
* [[Terraform module]]: <code>[[Terraform module: ec2-instance|ec2-instance]]</code>
* <code>[[aws_volume_attachment]]</code>
+
* [[Terraform resource]]: <code>[[Terraform resource: aws volume attachment|aws_volume_attachment]]</code>
* [[Terraform resource: aws volume attachment]]
+
* [[volume_size]]
 
* <code>[[ebs_block_device]]</code> resource argument
 
* <code>[[ebs_block_device]]</code> resource argument
 +
* <code>[[aws_eip]]</code>
 +
* <code>[[aws instance.your_instance_name will be updated in-place]]</code>
 +
* <code>[[count]]</code>
 +
* <code>[[VcpuLimitExceeded]]</code>
 +
* [[Datadog: EC2 instances should enforce IMDSv2]]
 +
* <code>[[aws_ec2_instance_metadata_defaults]]</code>
  
 
== See also ==
 
== See also ==
 
* {{aws_instance}}
 
* {{aws_instance}}
* {{tf ec2}}
 
* {{terraform aws resources}}
 
  
 
[[Category:Terraform]]
 
[[Category:Terraform]]

Latest revision as of 11:31, 25 October 2024

aws_instance (ref)

Arguments[edit]

Examples[edit]

Examples[edit]

resource "aws_instance" "MYexample" {
 ami           = "ami-08d70e59c07c61a3a"
 instance_type = "t3.micro" 

 tags = {
   Name = "your_name_here"
 }
}

See also: AWS instance example with aws_ami lookup
resource "aws_instance" "MYexample" {
 ami           = "ami-08d70e59c07c61a3a"
 instance_type = "t2.micro"
 associate_public_ip_address = "yes" 
  

 tags = {
   Name = "your_name_here"
 }
}


Example for terraform import:

resource "aws_instance" "restore-backups-machine" {
  # (resource arguments)
}


resource "aws_instance" "myUbuntuMicroInstance" {
  ami           = data.aws_ami.ubuntu.id
 .../...


resource "aws_instance" "my-instance" {
  count         = var.instance_count
  ami           = lookup(var.ami,var.aws_region)
  instance_type = var.instance_type
  key_name      = aws_key_pair.terraform-demo.key_name
  user_data     = file("install_apache.sh") 

  tags = {
    Name  = "Terraform-${count.index + 1}"
    Batch = "5AM"
  }
}

Outputs[edit]

output "instance_ip_addr" {
  value = aws_instance.your_server.private_ip
}

Errors[edit]

aws_instance arguments[edit]

Related terms[edit]

See also[edit]

  • https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/instance#key_name
  • Advertising: