Skip to content

Instantly share code, notes, and snippets.

@kharandziuk
Last active June 8, 2020 17:03
Show Gist options
  • Select an option

  • Save kharandziuk/e992e31cc815f6b87afcd71370a440a3 to your computer and use it in GitHub Desktop.

Select an option

Save kharandziuk/e992e31cc815f6b87afcd71370a440a3 to your computer and use it in GitHub Desktop.
create an ec2 instance and ssh to it
variable "aws_region" {
type = string
}
variable "aws_access_key" {
type = string
}
variable "aws_secret_key" {
type = string
}
resource "aws_key_pair" "deployer" {
key_name = "deployer-key"
public_key = file("${path.module}/deployer-key.pub")
}
provider "aws" {
region = var.aws_region
access_key = var.aws_access_key
secret_key = var.aws_secret_key
}
data "aws_ami" "centos" {
most_recent = true
filter {
name = "virtualization-type"
values = ["hvm"]
}
filter {
name = "name"
values = ["amzn2-ami-hvm-*-x86_64-ebs"]
}
owners = ["amazon"]
}
resource "aws_security_group" "main" {
egress = [
{
cidr_blocks = [ "0.0.0.0/0", ]
description = ""
from_port = 0
ipv6_cidr_blocks = []
prefix_list_ids = []
protocol = "-1"
security_groups = []
self = false
to_port = 0
}
]
ingress = [
{
cidr_blocks = [ "0.0.0.0/0", ]
description = ""
from_port = 22
ipv6_cidr_blocks = []
prefix_list_ids = []
protocol = "tcp"
security_groups = []
self = false
to_port = 22
}
]
}
resource "aws_instance" "example" {
ami = data.aws_ami.centos.id
instance_type = "t2.micro"
key_name = aws_key_pair.deployer.key_name
tags = {
name = "example"
}
vpc_security_group_ids = [aws_security_group.main.id]
}
data "aws_instances" "test" {
instance_tags = {
name = "example"
}
instance_state_names = ["pending", "running", "shutting-down", "terminated", "stopping", "stopped"]
depends_on = [ aws_instance.example ]
}
output "dns" {
value = aws_instance.example.public_dns
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment