feat: basic docker instance

This commit is contained in:
david committed 2024-01-18 15:54:20 +01:00
1 parent 0c69ca0f98
commit a3a2ea832b
5 files changed
+157 -4

No files matched your search

+13 -4
View File
@@ -1,11 +1,20 @@
# How to use this template # Module docker-instance
## Desciption This Terraform module start an AWS instance in default VPC.
Instance is build from latest Amazon Linux 2023 minimal AMI, with
EC2 Instance Connect and Docker installed at first boot.
This is a template for a minimal terraform module under GNU General Public License version 3. ## How to connect with SSH
```bash
aws ec2-instance-connect send-ssh-public-key \
--instance-id ${INSTANCE_ID} \
--availability-zone ${AVAILABILITY_ZONE} \
--instance-os-user ec2-user \
--ssh-public-key file://${PATH_TO_PUBLIC_KEY}
## Todo ssh -o "IdentitiesOnly=yes" -i ${PATH_TO_PUBLIC_KEY} ec2-user@${INSTANCE_PUBLIC_IP}
```
* workflows / actions (.tf format and validate) * workflows / actions (.tf format and validate)
* git hooks * git hooks
+99
View File
@@ -0,0 +1,99 @@
terraform {
required_version = ">=1.0"
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 4.0"
}
}
}
data "aws_ami" "latest" {
owners = ["amazon"]
most_recent = true
name_regex = var.ami_regexp
filter {
name = "architecture"
values = ["x86_64"]
}
}
resource "aws_security_group" "remote_control" {
name = "remote_control"
vpc_id = var.vpc_id
}
resource "aws_security_group_rule" "ssh" {
description = "SSH"
type = "ingress"
protocol = "tcp"
from_port = 22
to_port = 22
cidr_blocks = ["0.0.0.0/0"]
security_group_id = aws_security_group.remote_control.id
}
resource "aws_security_group_rule" "out" {
description = "allow any"
type = "egress"
protocol = "-1"
from_port = 0
to_port = 0
cidr_blocks = ["0.0.0.0/0"]
security_group_id = aws_security_group.remote_control.id
}
data "aws_iam_policy_document" "ec2_assume" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["ec2.amazonaws.com"]
}
}
}
data "aws_iam_policy_document" "docker" {
statement {
actions = ["ecr:*"]
resources = ["*"]
}
}
resource "aws_iam_role" "docker" {
name_prefix = "docker"
assume_role_policy = data.aws_iam_policy_document.ec2_assume.json
}
resource "aws_iam_role_policy" "docker" {
name = "docker"
role = aws_iam_role.docker.name
policy = data.aws_iam_policy_document.docker.json
}
resource "aws_iam_instance_profile" "docker" {
name_prefix = "docker"
role = aws_iam_role.docker.name
}
resource "aws_instance" "docker" {
ami = data.aws_ami.latest.id
instance_type = var.instance_type
key_name = var.key_name
iam_instance_profile = aws_iam_instance_profile.docker.name
vpc_security_group_ids = [aws_security_group.remote_control.id]
user_data_base64 = filebase64("${path.module}/templates/boot-docker.sh")
root_block_device {
# volume_size = 20
# volume_type = "gp3"
encrypted = true
}
metadata_options {
http_tokens = "required"
}
}
+9
View File
@@ -0,0 +1,9 @@
output "instance" {
description = "Instance attributes."
value = {
availability_zone = aws_instance.docker.availability_zone
id = aws_instance.docker.id
public_dns = aws_instance.docker.public_dns
public_ip = aws_instance.docker.public_ip
}
}
+14
View File
@@ -0,0 +1,14 @@
#!/bin/bash
# Install ec2-instance-connect
[ -f /opt/aws/bin/eic_run_authorized_keys ] || dnf install -y ec2-instance-connect
# Install Docker
dnf install -y docker
systemctl enable docker
systemctl start docker
usermod -a -G docker ec2-user
# Install docker-compose
curl -SL https://github.com/docker/compose/releases/download/v2.16.0/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
+22
View File
@@ -0,0 +1,22 @@
variable "vpc_id" {
description = "Target VPC id."
type = string
}
variable "instance_type" {
description = "Instance type to use for the Docker instance."
type = string
default = "t3.medium"
}
variable "ami_regexp" {
description = "value"
type = string
default = "^al2023-ami-minimal-.*"
}
variable "key_name" {
description = "value"
type = string
default = ""
}