Skip to content

Commit ecb726e

Browse files
committed
first commit
0 parents  commit ecb726e

File tree

5 files changed

+139
-0
lines changed

5 files changed

+139
-0
lines changed

.gitignore

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Local .terraform directories
2+
**/.terraform/*
3+
4+
# .tfstate files
5+
*.tfstate
6+
*.tfstate.*
7+
8+
# tf lock files
9+
.terraform.lock.hcl
10+
11+
# Crash log files
12+
crash.log
13+
14+
# Ignore any .tfvars files that are generated automatically for each Terraform run. Most
15+
# .tfvars files are managed as part of configuration and so should be included in
16+
# version control.
17+
#
18+
# example.tfvars
19+
20+
# Ignore override files as they are usually used to override resources locally and so
21+
# are not checked in
22+
override.tf
23+
override.tf.json
24+
*_override.tf
25+
*_override.tf.json
26+
27+
# Include override files you do wish to add to version control using negated pattern
28+
#
29+
# !example_override.tf
30+
31+
# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan
32+
# example: *tfplan*

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Provision an EC2 instance in AWS
2+
3+
This Terraform configuration provisions an EC2 instance in AWS using Terraform 0.13+ syntax.
4+
5+
## Details
6+
7+
By default, this configuration provisions the latest Ubuntu 18.04 AMI with type t2.micro in the us-west-1 region. The AMI Owner, AMi Search Name, region, and type can all be set as variables. You can also set the name variable to determine the value set for the Name tag.
8+
9+
Note that you need to set environment variables AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY.

main.tf

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
terraform {
2+
required_providers {
3+
aws = {
4+
source = "hashicorp/aws"
5+
version = "~> 3.46.0"
6+
}
7+
}
8+
required_version = ">= 0.13"
9+
}
10+
11+
# INITIALIZE AWS PROVIDER
12+
provider "aws" {
13+
region = "us-west-2"
14+
}
15+
16+
# CREATE AWS INSTANCE
17+
resource "aws_instance" "aws_vm" {
18+
ami = data.aws_ami.most_recent_ami.id
19+
instance_type = var.instance_type
20+
vpc_security_group_ids = [aws_security_group.ssh.id]
21+
22+
tags = merge({ "Name" = "${var.name_prefix}-instance", "Project" = var.name_prefix }, var.tags)
23+
}
24+
25+
# FIND MOST RECENT IMAGE
26+
data "aws_ami" "most_recent_ami" {
27+
filter {
28+
name = "name"
29+
values = [var.ami_search_name]
30+
}
31+
32+
most_recent = true
33+
34+
owners = [var.ami_owner]
35+
}
36+
37+
# CREATE NETWORK SECURITY GROUP ALLOWING SSH
38+
resource "aws_security_group" "ssh" {
39+
name = "${var.name_prefix}-ssh-sg"
40+
description = "${var.name_prefix} Test SG"
41+
42+
ingress {
43+
from_port = 22
44+
to_port = 22
45+
protocol = "tcp"
46+
47+
cidr_blocks = [var.ssh_cidr]
48+
}
49+
50+
egress {
51+
from_port = 0
52+
to_port = 0
53+
protocol = "-1"
54+
cidr_blocks = ["0.0.0.0/0"]
55+
}
56+
57+
tags = merge({ "Name" = "${var.name_prefix}-ssh-sg", "Project" = var.name_prefix }, var.tags)
58+
}

outputs.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# OUTPUTS
2+
3+
# DISPLAY PUBLIC DNS ADDRESS
4+
output "public_dns" {
5+
description = "Instance DNS Address"
6+
value = aws_instance.aws_vm.public_dns
7+
}

variables.tf

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# VARIABLES
2+
3+
variable "name_prefix" {
4+
description = "Name prefix to use when naming resources."
5+
default = "demo-mkt-analysis"
6+
}
7+
8+
variable "instance_type" {
9+
description = "type of EC2 instance to provision."
10+
default = "t3.micro" # default
11+
# default = "t3.xlarge" # upgrade t2.micro to t3.xlarge = cost increase violation
12+
}
13+
14+
variable "ssh_cidr" {
15+
description = "IP CIDR to allow inbound on port 22."
16+
default = "10.0.0.1/32"
17+
}
18+
19+
variable "tags" {
20+
type = map(string)
21+
default = {}
22+
}
23+
24+
variable "ami_owner" {
25+
description = "Owner of AMI to provision."
26+
default = "099720109477"
27+
}
28+
29+
variable "ami_search_name" {
30+
description = "Search name of AMI to provision."
31+
default = "ubuntu/images/hvm-ssd/ubuntu-bionic-18.04-amd64-server-*"
32+
}
33+

0 commit comments

Comments
 (0)