0% found this document useful (0 votes)
37 views2 pages

Exercise #12 Terraform

The document defines Terraform configuration to create AWS infrastructure including a VPC, subnet, security group, key pair, and EC2 instance. The required AWS provider is specified along with the region. Resources defined include a VPC, subnet, security group allowing SSH access from a specific IP, key pair using an existing public key, and EC2 instance launched in the subnet with the security group and key pair. The public IP of the instance is output.

Uploaded by

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

Exercise #12 Terraform

The document defines Terraform configuration to create AWS infrastructure including a VPC, subnet, security group, key pair, and EC2 instance. The required AWS provider is specified along with the region. Resources defined include a VPC, subnet, security group allowing SSH access from a specific IP, key pair using an existing public key, and EC2 instance launched in the subnet with the security group and key pair. The public IP of the instance is output.

Uploaded by

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

terraform {

required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}

# Configure the AWS Provider


provider "aws" {
region = "us-east-1"
}

# Configure the AWS Provider


resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
}

resource "aws_subnet" "main" {


vpc_id = aws_vpc.main.id
cidr_block = "10.0.1.0/24"
availability_zone = "us-east-1"
}

resource "aws_security_group" "ssh" {


name_prefix = "ssh-"
vpc_id = aws_vpc.main.id

ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["193.137.28.212/32"]
}
}

resource "aws_key_pair" "ssh_key" {


key_name = "my-ssh-key"
public_key = file("~/.ssh/id_rsa.pub")
}

resource "aws_instance" "ec2" {


ami = "ami-0947d2ba12ee1ff75"
instance_type = "t2.nano"
subnet_id = aws_subnet.main.id
vpc_security_group_ids = [aws_security_group.ssh.id]
key_name = aws_key_pair.ssh_key.key_name
user_data = <<-EOT
#!/bin/bash
sudo apt update -y
sudo apt upgrade -y
EOT

root_block_device {
volume_size = 5
}
}

output "public_ip" {
value = aws_instance.ec2.public_ip
}

You might also like