100 lines
2.2 KiB
Terraform
100 lines
2.2 KiB
Terraform
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"
|
|
}
|
|
}
|