@cryptotaxi247 / infra / commits / b1821464

Add terraform for Silvan's demo

Eelco Dolstra committed Aug 10, 2021 at 18:25 UTC b1821464af46c5b090f87bdcde4e7a5241d497da
1 file changed +90
ngi0/silvan-demo/main.tf new
+90
@@ -0,0 +1,90 @@
1 +provider "aws" {
2 + region = "eu-west-1"
3 +}
4 +
5 +output "public_ip" {
6 + value = aws_instance.server.public_ip
7 +}
8 +
9 +resource "aws_instance" "server" {
10 + ami = "ami-048dbc738074a3083"
11 + instance_type = "t3a.xlarge"
12 + subnet_id = aws_subnet.main.id
13 + #availability_zone = var.availability_zone
14 +
15 + vpc_security_group_ids = [ aws_security_group.ssh_and_egress.id ]
16 + key_name = aws_key_pair.generated_key.key_name
17 +
18 + root_block_device {
19 + volume_size = 50 # GiB
20 + }
21 +
22 + lifecycle {
23 + ignore_changes = [ami]
24 + }
25 +}
26 +
27 +resource "aws_vpc" "vpc" {
28 + cidr_block = "10.0.0.0/16"
29 + enable_dns_support = "true"
30 + enable_dns_hostnames = "true"
31 + enable_classiclink = "false"
32 + instance_tenancy = "default"
33 +}
34 +
35 +resource "aws_internet_gateway" "igw" {
36 + vpc_id = aws_vpc.vpc.id
37 +}
38 +
39 +resource "aws_subnet" "main" {
40 + vpc_id = aws_vpc.vpc.id
41 + cidr_block = "10.0.0.0/24"
42 + map_public_ip_on_launch = "true"
43 + availability_zone = "eu-west-1a"
44 +}
45 +
46 +resource "aws_route_table" "rt" {
47 + vpc_id = aws_vpc.vpc.id
48 + route {
49 + cidr_block = "0.0.0.0/0"
50 + gateway_id = aws_internet_gateway.igw.id
51 + }
52 +}
53 +
54 +resource "aws_route_table_association" "rt_assoc" {
55 + subnet_id = aws_subnet.main.id
56 + route_table_id = aws_route_table.rt.id
57 +}
58 +
59 +resource "aws_security_group" "ssh_and_egress" {
60 + vpc_id = aws_vpc.vpc.id
61 +
62 + ingress {
63 + from_port = 22
64 + to_port = 22
65 + protocol = "tcp"
66 + cidr_blocks = [ "0.0.0.0/0" ]
67 + }
68 +
69 + egress {
70 + from_port = 0
71 + to_port = 0
72 + protocol = "-1"
73 + cidr_blocks = ["0.0.0.0/0"]
74 + }
75 +}
76 +
77 +resource "tls_private_key" "state_ssh_key" {
78 + algorithm = "RSA"
79 +}
80 +
81 +resource "local_file" "machine_ssh_key" {
82 + sensitive_content = tls_private_key.state_ssh_key.private_key_pem
83 + filename = "${path.module}/id_rsa.pem"
84 + file_permission = "0600"
85 +}
86 +
87 +resource "aws_key_pair" "generated_key" {
88 + key_name = "generated-key-${sha256(tls_private_key.state_ssh_key.public_key_openssh)}"
89 + public_key = tls_private_key.state_ssh_key.public_key_openssh
90 +}