Skip to content

Instantly share code, notes, and snippets.

@adduc
Last active November 12, 2019 08:19
Show Gist options
  • Save adduc/c7d751742d7527fad233c28823ee25e5 to your computer and use it in GitHub Desktop.
Save adduc/c7d751742d7527fad233c28823ee25e5 to your computer and use it in GitHub Desktop.
locals {
ami_ubuntu_1804 = "ami-06397100adf427136"
ssh_public_key = "ssh-rsa ABCD...asdfasdf"
}
provider "aws" {
profile = "default"
region = "us-west-1"
}
##
# VPC Configuration
##
resource "aws_vpc" "vpc" {
cidr_block = "10.0.0.0/16"
}
resource "aws_subnet" "subnet" {
vpc_id = "${aws_vpc.vpc.id}"
cidr_block = "${cidrsubnet(aws_vpc.vpc.cidr_block, 10, 0)}"
}
resource "aws_internet_gateway" "gateway" {
vpc_id = "${aws_vpc.vpc.id}"
}
resource "aws_route" "internet_access" {
route_table_id = "${aws_vpc.vpc.main_route_table_id}"
destination_cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.gateway.id}"
}
##
# Security Group
##
resource "aws_security_group" "ssh-access" {
name_prefix = "ssh-access"
vpc_id = "${aws_vpc.vpc.id}"
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_security_group" "allow-all-outbound" {
name_prefix = "allow-all-outbound"
vpc_id = "${aws_vpc.vpc.id}"
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
##
# EC2 Configuration
##
resource "aws_key_pair" "key_pair" {
key_name_prefix = "key_pair"
public_key = "${local.ssh_public_key}"
}
resource "aws_instance" "example" {
ami = "${local.ami_ubuntu_1804}"
associate_public_ip_address = true
key_name = "${aws_key_pair.key_pair.key_name}"
instance_type = "t2.micro"
subnet_id = "${aws_subnet.subnet.id}"
vpc_security_group_ids = [
"${aws_security_group.ssh-access.id}",
"${aws_security_group.allow-all-outbound.id}",
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment