Skip to content

Commit f92cf36

Browse files
committed
should define your key file
0 parents commit f92cf36

File tree

7 files changed

+161
-0
lines changed

7 files changed

+161
-0
lines changed

.gitignore

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Local .terraform directories
2+
**/.terraform/*
3+
4+
# Terraform lockfile
5+
.terraform.lock.hcl
6+
7+
# .tfstate files
8+
*.tfstate
9+
*.tfstate.*
10+
11+
# Crash log files
12+
crash.log
13+
14+
# Exclude all .tfvars files, which are likely to contain sensitive data, such as
15+
# password, private keys, and other secrets. These should not be part of version
16+
# control as they are data points which are potentially sensitive and subject
17+
# to change depending on the environment.
18+
*.tfvars

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Terraform Module to provision an AWS EC2 instance with the latest amazon linux 2 ami and installed docker in it.
2+
3+
Not intended for production use. It is an example module.
4+
5+
It is just for showing how to create a publish module in Terraform Registry.
6+
7+
Usage:
8+
9+
```hcl
10+
11+
provider "aws" {
12+
region = "us-east-1"
13+
}
14+
15+
module "docker_instance" {
16+
source = "turangozukara/docker-instance-module/aws"
17+
key_name = "yourkeyname"
18+
}
19+
```

main.tf

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
data "aws_ami" "amazon-linux-2023" {
2+
owners = ["amazon"]
3+
most_recent = true
4+
5+
filter {
6+
name = "root-device-type"
7+
values = ["ebs"]
8+
}
9+
10+
filter {
11+
name = "virtualization-type"
12+
values = ["hvm"]
13+
}
14+
15+
filter {
16+
name = "architecture"
17+
values = ["x86_64"]
18+
}
19+
20+
filter {
21+
name = "owner-alias"
22+
values = ["amazon"]
23+
}
24+
25+
filter {
26+
name = "name"
27+
values = ["al2023-ami-2023*"]
28+
}
29+
}
30+
31+
resource "aws_instance" "tfmyec2" {
32+
ami = data.aws_ami.amazon-linux-2023.id
33+
instance_type = var.instance_type
34+
count = var.num_of_instance
35+
key_name = var.key_name
36+
vpc_security_group_ids = [aws_security_group.tf-sec-gr.id]
37+
user_data = templatefile("${abspath(path.module)}/userdata.sh", {myserver = var.server-name})
38+
tags = {
39+
Name = var.tag
40+
}
41+
}
42+
43+
resource "aws_security_group" "tf-sec-gr" {
44+
name = "${var.tag}-terraform-sec-grp"
45+
tags = {
46+
Name = var.tag
47+
}
48+
49+
dynamic "ingress" {
50+
for_each = var.docker-instance-ports
51+
iterator = port
52+
content {
53+
from_port = port.value
54+
to_port = port.value
55+
protocol = "tcp"
56+
cidr_blocks = ["0.0.0.0/0"]
57+
}
58+
}
59+
60+
egress {
61+
from_port =0
62+
protocol = "-1"
63+
to_port =0
64+
cidr_blocks = ["0.0.0.0/0"]
65+
}
66+
}

outputs.tf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
output "instance_public_ip" {
2+
value = aws_instance.tfmyec2.*.public_ip
3+
}
4+
5+
output "sec_gr_id" {
6+
value = aws_security_group.tf-sec-gr.id
7+
}
8+
9+
output "instance_id" {
10+
value = aws_instance.tfmyec2.*.id
11+
}

userdata.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/bash
2+
hostnamectl set-hostname ${myserver}
3+
dnf update -y
4+
dnf install docker -y
5+
systemctl start docker
6+
systemctl enable docker
7+
usermod -a -G docker ec2-user
8+
# install docker-compose
9+
curl -SL https://github.com/docker/compose/releases/download/v2.17.3/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose
10+
chmod +x /usr/local/bin/docker-compose

variables.tf

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
variable "instance_type" {
2+
type = string
3+
default = "t2.micro"
4+
}
5+
6+
variable "key_name" {
7+
type = string
8+
}
9+
10+
variable "num_of_instance" {
11+
type = number
12+
default = 1
13+
}
14+
15+
variable "tag" {
16+
type = string
17+
default = "Docker-Instance"
18+
}
19+
20+
variable "server-name" {
21+
type = string
22+
default = "docker-instance"
23+
}
24+
25+
variable "docker-instance-ports" {
26+
type = list(number)
27+
description = "docker-instance-sec-gr-inbound-rules"
28+
default = [22, 80, 8080]
29+
}

versions.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
terraform {
2+
required_providers {
3+
aws = {
4+
source = "hashicorp/aws"
5+
version = "~> 5.0"
6+
}
7+
}
8+
}

0 commit comments

Comments
 (0)