How to Create an AWS EC2 Instance in Terraform

0 min read 128 words

The following Terraform code snippet creates an EC2 instance for you.

provider "aws" {
  region = "us-west-2"
}

resource "aws_instance" "example" {
  ami           = "ami-0c94855ba95c71c99"  # Amazon Linux 2 AMI
  instance_type = "t2.micro"
  key_name      = "example-keypair"

  tags = {
    Name = "example-instance"
  }
}

In this example, we’re using the aws_instance resource type to create an EC2 instance in the us-west-2 region. We’re using the ami parameter to specify the Amazon Linux 2 AMI ID, and the instance_type parameter to specify a t2.micro instance type. We’re also specifying a key pair to use for SSH access with the key_name parameter.

Finally, we’re adding a tags block to specify a Name tag for the instance. You can customize this block with any additional tags that you’d like to add.

Andrew
Andrew

Andrew is a visionary software engineer and DevOps expert with a proven track record of delivering cutting-edge solutions that drive innovation at Ataiva.com. As a leader on numerous high-profile projects, Andrew brings his exceptional technical expertise and collaborative leadership skills to the table, fostering a culture of agility and excellence within the team. With a passion for architecting scalable systems, automating workflows, and empowering teams, Andrew is a sought-after authority in the field of software development and DevOps.

Tags

Recent Posts