I cannot SSH into the instances created by this script:
variable "access_key" {
description = "access key"
type = string
}
variable "secret_key" {
description = "secret key"
type = string
}
variable "aws_region" {
description = "The AWS region to deploy into"
type = string
default = "us-east-1"
}
variable "ssh_user" {
description = "SSH user name to use for remote exec connections,"
type = string
default = "ubuntu"
}
provider "aws" {
access_key = var.access_key
secret_key = var.secret_key
version = "~> 2.0"
region = var.aws_region
}
resource "aws_key_pair" "deployer" {
key_name = "deployer-key"
public_key = "ssh-rsa AAAAB3Nza..."
}
resource "aws_security_group" "sec_group" {
name = "terra_sec_group"
ingress {
from_port = 8080
to_port = 8080
protocol = "tcp"
cidr_blocks =["0.0.0.0/0"]
}
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks =["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
data "aws_vpc" "default" {
default = true
}
data "aws_subnet_ids" "default" {
vpc_id = data.aws_vpc.default.id
}
resource "aws_launch_configuration" "config" {
image_id = "ami-07ebfd5b3428b6f4d"
instance_type = "t2.micro"
security_groups = [aws_security_group.sec_group.id]
key_name = "deployer-key"
user_data = <<-EOF
#!/bin/bash
echo "Hello, World" > index.html
nohup busybox httpd -f -p 8080 &
EOF
#Required with ASG
lifecycle {
create_before_destroy = true
}
}
resource "aws_autoscaling_group" "asg" {
launch_configuration = aws_launch_configuration.config.name
vpc_zone_identifier = data.aws_subnet_ids.default.ids
target_group_arns = [aws_lb_target_group.asg.arn]
health_check_type = "ELB"
min_size = 2
max_size = 8
tag {
key = "name"
value = "terraform-asg"
propagate_at_launch = true
}
}
resource "aws_lb" "example" {
name = "terraform-asg-example"
load_balancer_type = "application"
subnets = data.aws_subnet_ids.default.ids
security_groups = [aws_security_group.alb.id]
}
resource "aws_lb_listener" "http" {
load_balancer_arn = aws_lb.example.arn
port = 80
protocol = "HTTP"
default_action {
type = "fixed-response"
fixed_response {
content_type = "text/plain"
message_body = "404: page not found"
status_code = 404
}
}
}
resource "aws_security_group" "alb" {
name = "terraform-example-alb-asg"
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_lb_target_group" "asg" {
name = "terraform-asg-example"
port = 8080
protocol = "HTTP"
vpc_id = data.aws_vpc.default.id
health_check {
path = "/"
protocol = "HTTP"
matcher = "200"
interval = 15
timeout = 3
healthy_threshold = 2
unhealthy_threshold = 2
}
}
resource "aws_lb_listener_rule" "asg" {
listener_arn = aws_lb_listener.http.arn
priority = 100
condition {
field = "path-pattern"
values = ["*"]
}
action {
type = "forward"
target_group_arn = aws_lb_target_group.asg.arn
}
}
output "alb_dns_name" {
value = aws_lb.example.dns_name
description = "Domain name of load balancer"
}