can u write me a sample terraform script to create a AWS EC2 instance
- User
can u write me a sample terraform script to create a AWS EC2 instance
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] # Specify the key pair to use for the instance key_name = "my-key-pair" # Specify the network interface for the instance network_interface { device_name = "eth0" # Specify the associate public IP address associate_public_ip_address = true } } resource "aws_security_group" "example" { name = "example" description = "Example security group" # Allow inbound traffic on port 22 from anywhere ingress { from_port = 22 to_port = 22 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] } }This script creates an EC2 instance with the specified instance type, AMIs, security group, and key pair. The
aws_instanceresource creates the EC2 instance, and theaws_security_groupresource creates the security group.Please note that you will need to replace the
ami,instance_type,vpc_security_group_ids,key_name, andnetwork_interfacevalues with your own desired values. Also, you need to have the AWS credentials set up on your system to use theawsprovider.You can run this script using the following command:
terraform applyThis will create the EC2 instance and security group in your AWS account.
You can also use the
terraform destroycommand to destroy the resources created by this script.terraform destroyPlease note that this script is just an example and you should not use it in production without proper testing and validation.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.