From a3a2ea832bb36609a090b5006dd85dc3916db1c6 Mon Sep 17 00:00:00 2001 From: David FILLON Date: Thu, 18 Jan 2024 15:54:20 +0100 Subject: [PATCH] feat: basic docker instance --- README.md | 17 +++++-- main.tf | 99 ++++++++++++++++++++++++++++++++++++++++ outputs.tf | 9 ++++ templates/boot-docker.sh | 14 ++++++ variables.tf | 22 +++++++++ 5 files changed, 157 insertions(+), 4 deletions(-) create mode 100644 templates/boot-docker.sh diff --git a/README.md b/README.md index a694fbe..7d856a3 100644 --- a/README.md +++ b/README.md @@ -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) * git hooks diff --git a/main.tf b/main.tf index e69de29..8ef2702 100644 --- a/main.tf +++ b/main.tf @@ -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" + } +} diff --git a/outputs.tf b/outputs.tf index e69de29..5fff8c9 100644 --- a/outputs.tf +++ b/outputs.tf @@ -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 + } +} diff --git a/templates/boot-docker.sh b/templates/boot-docker.sh new file mode 100644 index 0000000..a6022f1 --- /dev/null +++ b/templates/boot-docker.sh @@ -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 diff --git a/variables.tf b/variables.tf index e69de29..a544040 100644 --- a/variables.tf +++ b/variables.tf @@ -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 = "" +}