0% found this document useful (0 votes)
3 views5 pages

Terraform AWS EC2 Instance Setup Guide

terafform code code details explaination

Uploaded by

ashis
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

Terraform AWS EC2 Instance Setup Guide

terafform code code details explaination

Uploaded by

ashis
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Details Code: This Terraform script automatically provisions an AWS EC2 instance, secures it with

an SSH key and security group, and returns the instance’s public I

resource "aws_key_pair" "my_ssh_key" {

key_name = "terra-key-auto"

public_key = file("/home/ubuntu/[Link]")

resource "aws_default_vpc" "default" {

resource "aws_security_group" "my_sg" {

name = "my_sg"

description = "This is terra auto description"

vpc_id = aws_default_vpc.[Link]

ingress {

from_port = 22

to_port = 22

protocol = "tcp"

cidr_blocks = ["[Link]/0"]

description = "for incoming world"

egress {

from_port = 0

to_port = 0

protocol = "-1"

cidr_blocks = ["[Link]/0"]

description = "For utside world"


}

resource "aws_instance" "my_instance" {

tags = {

Name = "My-Auto-Server"

ami = "ami-075686beab831bb7f"

instance_type = "[Link]"

key_name = aws_key_pair.my_ssh_key.key_name

security_groups = [aws_security_group.my_sg.name]

#resource "aws_ec2_instance_state" "my_state" {

# instance_id = aws_instance.my_instance.id

# state = "stopped"

#}

output "my_ec2_ip" {

value = aws_instance.my_instance.public_i

}
1. Creating an SSH Key Pair

resource "aws_key_pair" "my_ssh_key" { key_name = "terra-key-auto" public_key =


file("/home/ubuntu/[Link]") }

• This defines an AWS key pair named "terra-key-auto".

• It uses an existing public key from the specified file (/home/ubuntu/[Link]).

• This key pair is used for secure SSH access to the instance.

2. Using the Default VPC

• This ensures Terraform will use the default AWS VPC.

• No additional configuration is provided, meaning it adopts AWS default settings.

3. Creating a Security Group


• This defines a security group named "my_sg" inside the default VPC.

• Security groups control inbound and outbound traffic.

Ingress (Inbound Rule)

• Allows SSH (port 22) traffic from anywhere ([Link]/0).


• This rule makes the instance accessible from any IP.

Egress (Outbound Rule)

• Allows all outbound traffic (protocol -1 means all types).

• Ensures the instance can communicate externally.

4. Launching an EC2 Instance


• Creates an EC2 instance named "My-Auto-Server".

• Uses the specified Amazon Machine Image (AMI) (ami-075686beab831bb7f).

• Uses the "[Link]" instance type.

• Assigns the previously defined security group and SSH key pair.

5. (Optional) Stopping the Instance

# resource "aws_ec2_instance_state" "my_state" { # instance_id = aws_instance.my_instance.id #


state = "stopped" # }

• This block is commented out and doesn't execute.

• If uncommented, it ensures the instance is stopped instead of running.

6. Outputting the Instance's Public IP

• Displays the public IP of the EC2 instance after Terraform execution.

• Useful for accessing the instance remotely.

You might also like