Skip to content
The Chat Archive
llama-2-70b-chat· Meta Arena Human Preference 55K

can u write me a sample terraform script to create a AWS EC2 instance

  1. User

    can u write me a sample terraform script to create a AWS EC2 instance

  2. Assistant

    Sure! Here is a sample Terraform script that creates an AWS EC2 instance:

    provider "aws" {
      region = "us-west-2"
    }
    
    resource "aws_instance" "example" {
      ami           = "ami-12345678"
      instance_type = "t2.micro"
      vpc_security_group_ids = [aws_security_group.example.id]
      key_name               = "my_key"
    
      # Specify the VPC subnets and assign a public IP
      vpc_subnet_id = aws_subnet.example.id
      public_ip = "99.99.99.99"
    }
    
    resource "aws_security_group" "example" {
      name        = "example"
      description = "Example security group"
      vpc_id      = aws_vpc.example.id
    
      # Allow inbound traffic on port 22 from anywhere
      ingress {
        from_port   = 22
        to_port     = 22
        protocol    = "tcp"
        cidr_blocks = ["0.0.0.0/0"]
      }
    }
    
    resource "aws_vpc" "example" {
      cidr_block = "10.0.0.0/16"
    }
    
    resource "aws_subnet" "example" {
      cidr_block = "10.0.1.0/24"
      vpc_id     = aws_vpc.example.id
    }
    

    This script creates an AWS EC2 instance using the aws_instance resource. It also creates a security group and a VPC subnet, and associates the security group with the subnet.

    Here's a brief explanation of each resource:

    • aws_instance: This resource creates an EC2 instance. It takes several properties, such as the AMI ID, instance type, security group ID, and subnet ID.
    • aws_security_group: This resource creates a security group. It takes a name, description, VPC ID, and a list of ingress rules.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.