-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Terraform Provision EC2 single node, AMI with Packer, and deploy sing…
- Loading branch information
Showing
9 changed files
with
341 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
terraform { | ||
required_providers { | ||
# https://registry.terraform.io/providers/hashicorp/aws/latest | ||
aws = { | ||
source = "hashicorp/aws" | ||
version = "5.45.0" | ||
} | ||
# https://registry.terraform.io/providers/hashicorp/tls/latest | ||
# 4. Creating a Key Pair with Local and TLS Providers | ||
tls = { | ||
source = "hashicorp/tls" | ||
version = "4.0.5" | ||
} | ||
# https://registry.terraform.io/providers/hashicorp/local/latest | ||
local = { | ||
source = "hashicorp/local" | ||
version = "2.5.1" | ||
|
||
} | ||
} | ||
} | ||
|
||
data "aws_vpc" "main" { | ||
filter { | ||
name = "isDefault" | ||
values = ["true"] | ||
} | ||
} | ||
|
||
data "aws_subnets" "main_subnets" { | ||
filter { | ||
name = "vpc-id" | ||
values = [data.aws_vpc.main.id] | ||
} | ||
} | ||
|
||
resource "tls_private_key" "rsa" { | ||
algorithm = "RSA" | ||
rsa_bits = 4096 | ||
} | ||
|
||
resource "aws_key_pair" "deployer_key" { | ||
key_name = "swarm-key" | ||
public_key = tls_private_key.rsa.public_key_openssh | ||
} | ||
|
||
resource "local_sensitive_file" "private_key" { | ||
filename = var.private_key_path | ||
content = tls_private_key.rsa.private_key_pem | ||
file_permission = "0400" | ||
} | ||
|
||
data "aws_ami" "amazon_linux_docker" { | ||
most_recent = true | ||
|
||
filter { | ||
name = "name" | ||
values = ["amazon-linux-docker*"] | ||
} | ||
# find with aws cli: aws sts get-caller-identity | ||
# owners = ["AWS_ACC_ID_UNSET"] | ||
owners = ["447130666878"] | ||
} | ||
|
||
resource "aws_security_group" "swarm_sg" { | ||
description = "launch-wizard-2 created 2024-04-17T22:18:12.426Z" | ||
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 | ||
}, | ||
{ | ||
cidr_blocks = [ | ||
"0.0.0.0/0", | ||
] | ||
description = "" | ||
from_port = 4000 | ||
ipv6_cidr_blocks = [] | ||
prefix_list_ids = [] | ||
protocol = "tcp" | ||
security_groups = [] | ||
self = false | ||
to_port = 4000 | ||
}, | ||
] | ||
tags = {} | ||
tags_all = {} | ||
# vpc_id = "vpc-0021bbe35d223bc80" | ||
vpc_id = data.aws_vpc.main.id | ||
} | ||
|
||
resource "aws_instance" "belly_swarm" { | ||
# ami = "ami-0d421d84814b7d51c" | ||
ami = data.aws_ami.amazon_linux_docker.id | ||
availability_zone = "eu-west-1b" | ||
key_name = aws_key_pair.deployer_key.key_name | ||
subnet_id = data.aws_subnets.main_subnets.ids[1] | ||
user_data = <<-EOF | ||
#!/bin/bash | ||
docker swarm init | ||
EOF | ||
instance_type = "t2.micro" | ||
tags = { | ||
"Name" = "docker-swarm-manager" | ||
} | ||
vpc_security_group_ids = [ | ||
aws_security_group.swarm_sg.id, | ||
] | ||
} | ||
|
||
output "ssh_command" { | ||
value = "ssh -i ${var.private_key_path} ec2-user@${aws_instance.belly_swarm.public_ip}" | ||
description = "The SSH command to connect to the instance." | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
variable "private_key_path" { | ||
description = "The path to the private key file." | ||
type = string | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# set up production | ||
module "swarm" { | ||
source = "../../../modules/cloud/aws/compute/swarm" | ||
private_key_path = "${path.module}/private_key.pem" | ||
} | ||
|
||
output "swarm_ssh_command" { | ||
value = module.swarm.ssh_command | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
-----BEGIN RSA PRIVATE KEY----- | ||
MIIJKQIBAAKCAgEAz9INbdZICvmfNjo8kYy77HFTsNT+Cz3A0+5j7A1rLjLoKRVw | ||
7Ws4qsuiuN7pcnArkX0Aozz4VvsYMTL7XNNPjGOoAKxOBGBhcM5RRVOmI/+9uapL | ||
y+lDqaS8DUNYl+SZ0HGQaKrkc1+qCrKW6J1lkE6cYfJhasIzj0bgYZe4ZWjxOAHF | ||
0gnU33uhU+3iP25TbV9db0mV7i1ESylezEFffGL0UkDVp9bNvqF3coroLdjXldSR | ||
Lcbtls9sD/t54Wnr5Sd1J8SuZ/FL0j2k994J+y+mkqtHwNT/x93ACaPh+w2no8AS | ||
h7tWsn6lqNcuHgCy3ursoX16K4V/Sew5D0bS7HS2Khm7xWgMk/eG3xjCy9sqe2J+ | ||
QwHmvEh/6O/Ph4T9POkjose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GQKCAQAPTCp65gWGF1ljtIYZztkAUcI24DJj2cnH2C9My0ffGGGBoOVK/VSl | ||
4bH4Z3DZYHd2PnPEF+gbBzF65jXXbQTE6GrDu/QOrB/AvSp3leF8Cl35623huYjw | ||
Tg6IrKTH8B6/he9QoAkm2LoeFhG+EfjNBLYlNNy4o6flew4Wkt8wSngBsNwmR+el | ||
GPzsjThtHilZeBa4IpqhDuMSJqNsI8LoUIE+BiAmsFI77mIwPLmFv+iorVgwoKhL | ||
47EYIJwIdLaDLRaOG9c8JUhb5hnJrndjs2TkyXlPaU8rB7abbmfyDHDXl0/aWxrB | ||
42qh8BM/Sqf0QBVQHj6rYpyzkOh98W2dAoIBAQCdy7xVz+NTrfRxuxWGdUktIp78 | ||
17nZXzwfjU06PPhfY6qfwyFN9zbM6OpLdWMYdQ+oX7qEXQLA1AQg0iJ87RDYyk3t | ||
R/VFSKyO3pi8RSDnxVn8gkhwkPyx4pAzM/kkswS+TI0JcgFXtdIS7ynw/knskUKS | ||
jKCleyr/ptnYqjjVeB3OAns6GANVMP3sIrxB6TQzM6aiyNSCExjREz9eG9J48x+y | ||
D6NXTbiYlazxkHlNZqDqTF8iWZt9kFB4RP+dZ3cjrk6aTdcOAPNa6pabDfmFIzne | ||
UbYZYMIfdr3rvdZpI9FdMIsxBD6eJFBX1YLsGA+twV3lPdtNS7eaF5JIUg0f | ||
-----END RSA PRIVATE KEY----- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
packer { | ||
required_plugins { | ||
amazon = { | ||
version = ">= 1.3.2" | ||
source = "github.com/hashicorp/amazon" | ||
} | ||
} | ||
} | ||
|
||
source "amazon-ebs" "base" { | ||
ami_regions = var.ami_regions | ||
// source_ami = "YOUR_AMI_ID" | ||
// source_ami = "ami-0d421d84814b7d51c" | ||
source_ami_filter { | ||
filters = { | ||
name = "al2023-ami-2023*" | ||
architecture = "x86_64" | ||
} | ||
most_recent = true | ||
owners = ["amazon"] | ||
} | ||
instance_type = "t2.micro" | ||
ssh_username = "ec2-user" | ||
ami_name = "amazon-linux-docker_{{timestamp}}" | ||
} | ||
|
||
build { | ||
sources = ["source.amazon-ebs.base"] | ||
provisioner "shell" { | ||
// inline = [ | ||
// "sudo dnf update -y", | ||
// "sudo dnf install -y docker", | ||
// "sudo systemctl start docker", | ||
// "sudo systemctl enable docker", | ||
// "sudo usermod -a -G docker ec2-user", "sudo dnf install -y nmap" | ||
// ] | ||
script = "setup.sh" | ||
# run script after cloud-init finishes to avoid race conditions | ||
execute_command = "cloud-init status --wait && sudo -E sh '{{ .Path }}'" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#!/bin/bash | ||
set -ex | ||
sudo dnf update -y | ||
sudo dnf install -y docker | ||
sudo systemctl start docker | ||
sudo systemctl enable docker | ||
sudo usermod -a -G docker ec2-user | ||
sudo dnf install -y nmap |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
variable "ami_regions" { | ||
type = list(string) | ||
description = "A list of regions where the AMI will be copied to." | ||
} |