Difference between revisions of "Terraform: AWS instance example with aws ami lookup and key name"

From wikieduonline
Jump to navigation Jump to search
 
(39 intermediate revisions by 5 users not shown)
Line 1: Line 1:
  
 
  [[main.tf]]
 
  [[main.tf]]
  provider "aws" {
+
 
 +
  [[provider]] "[[aws]]" {
 
   region  = "[[us-west-2]]"
 
   region  = "[[us-west-2]]"
 
   profile = "YourProfile"
 
   profile = "YourProfile"
 
  }
 
  }
 
   
 
   
   data "aws_ami" "ubuntu" {
+
   [[data]] "[[aws_ami]]" "ubuntu" {
 
   most_recent = true
 
   most_recent = true
 
   
 
   
 
   filter {
 
   filter {
 
     name  = "name"
 
     name  = "name"
     values = ["ubuntu/images/hvm-ssd/ubuntu-hirsute-21.04-amd64-server-*"]
+
     values = ["ubuntu/images/hvm-ssd/ubuntu-[[kinetic]]-22.10-amd64-server-*"]
 
   }
 
   }
 
   
 
   
Line 22: Line 23:
 
  }
 
  }
 
   
 
   
  resource "aws_instance" "myUbuntuMicroInstance" {
+
  [[Terraform resource|resource]] "[[aws_instance]]" "myUbuntuMicroInstance" {
 
   ami          = data.aws_ami.ubuntu.id
 
   ami          = data.aws_ami.ubuntu.id
   instance_type = "t3.micro"
+
   instance_type = "[[t3.micro]]"
 +
  [[associate_public_ip_address]] = "[[true]]"
 +
  [[vpc_security_group_ids]] = [aws_security_group.allow_ssh.id]
 
   [[key_name]] = "YourKeyName"  
 
   [[key_name]] = "YourKeyName"  
 
   
 
   
Line 30: Line 33:
 
     Name = "MyInstanceName"
 
     Name = "MyInstanceName"
 
   }
 
   }
 +
}
 
   
 
   
  resource "aws_key_pair" "deployer" {
+
  resource "[[aws_security_group]]" "allow_ssh" {
   key_name  = "deployer-key"
+
   name        = "allow_ssh"
   public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 [email protected]"
+
   description = "Allow ssh inbound traffic from Internet"
 +
 
 +
  ingress {
 +
    description      = "SSH from Internet"
 +
    from_port        = 22
 +
    to_port          = 22
 +
    protocol        = "tcp"
 +
    cidr_blocks      = ["0.0.0.0/0"]
 +
    ipv6_cidr_blocks = ["::/0"]
 
   }
 
   }
 +
 +
  tags = {
 +
    Name = "allow_ssh"
 +
  }
 +
}
 +
 +
resource "[[aws_key_pair]]" "YourKeyName" {
 +
  [[key_name]]  = "YourKeyName"
 +
  [[public_key]] = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 [email protected]"
 +
}
  
 +
== Errors ==
 +
* [[Error: creating Security Group]] (allow_ssh): [[VPCIdNotSpecified: No default VPC for this user]]
  
}
+
=== Errors: Key par does not exist ===
 
 
== Errors: Key par does not exist ==
 
 
  │ Error: Error launching source instance: InvalidKeyPair.NotFound: The [[key pair]] 'YourKeyName' does not exist
 
  │ Error: Error launching source instance: InvalidKeyPair.NotFound: The [[key pair]] 'YourKeyName' does not exist
 
  │      status code: 400, request id: 717f23c7-f87f-4056-a8f4-a82ad2095534
 
  │      status code: 400, request id: 717f23c7-f87f-4056-a8f4-a82ad2095534
 +
 +
 +
[[VPCIdNotSpecified]]: [[No default VPC]] for this user. [[GroupName]] is only supported for [[EC2-Classic]] and [[default VPC]].
  
 
== Related ==
 
== Related ==
 
* <code>[[aws ec2 describe-key-pairs]]</code>
 
* <code>[[aws ec2 describe-key-pairs]]</code>
* <code>[[terraform init]]</code>
+
* <code>[[terraform init]]</code>, <code>[[terraform show]]</code>
 +
* <code>[[terraform state show aws_instance.MYexample]]</code>
 
* [[Terraform resource]]: <code>[[Terraform resource: aws_key_pair|aws_key_pair]]</code>
 
* [[Terraform resource]]: <code>[[Terraform resource: aws_key_pair|aws_key_pair]]</code>
 +
* <code>[[aws ec2 run-instances]]</code>
 +
* <code>[[~/.ssh/config]]</code>
 +
* <code>[[terraform show]] | grep [[private_ip]]</code>
 +
* <code>[[aws_security_group]]</code>
 +
 +
== Activities ==
 +
* Improve this example by adding a [[security group]] with ssh access from anywhere: [[Terraform resource: aws security group]]
  
 
== See also ==
 
== See also ==

Latest revision as of 10:55, 19 October 2023

main.tf
provider "aws" {
  region  = "us-west-2"
  profile = "YourProfile"
}

 data "aws_ami" "ubuntu" {
  most_recent = true

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

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

  owners = ["099720109477"] # Canonical
}

resource "aws_instance" "myUbuntuMicroInstance" {
  ami           = data.aws_ami.ubuntu.id
  instance_type = "t3.micro"
  associate_public_ip_address = "true" 
  vpc_security_group_ids = [aws_security_group.allow_ssh.id]
  key_name = "YourKeyName" 

  tags = {
    Name = "MyInstanceName"
  }
}

resource "aws_security_group" "allow_ssh" {
  name        = "allow_ssh"
  description = "Allow ssh inbound traffic from Internet"
 
  ingress {
    description      = "SSH from Internet"
    from_port        = 22
    to_port          = 22
    protocol         = "tcp"
    cidr_blocks      = ["0.0.0.0/0"]
    ipv6_cidr_blocks = ["::/0"]
  }

  tags = {
    Name = "allow_ssh"
  }
}

resource "aws_key_pair" "YourKeyName" {
  key_name   = "YourKeyName"
  public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 [email protected]"
}

Errors[edit]

* Error: creating Security Group (allow_ssh): VPCIdNotSpecified: No default VPC for this user

Errors: Key par does not exist[edit]

│ Error: Error launching source instance: InvalidKeyPair.NotFound: The key pair 'YourKeyName' does not exist
│       status code: 400, request id: 717f23c7-f87f-4056-a8f4-a82ad2095534


VPCIdNotSpecified: No default VPC for this user. GroupName is only supported for EC2-Classic and default VPC.

Related[edit]

Activities[edit]

See also[edit]

Advertising: