Skip to content

Commit

Permalink
Update launch_user_data.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielLin9406 committed Nov 25, 2020
1 parent 1dbd28a commit 0722762
Show file tree
Hide file tree
Showing 11 changed files with 121 additions and 18 deletions.
Binary file modified .DS_Store
Binary file not shown.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ dump.rdb
plan
terraform.log
terraform.tfstate
terraform.tfstate.backup
terraform.tfstate.backup
*.pem
5 changes: 5 additions & 0 deletions scripts/create-ssh-key-pair.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@


aws ec2 create-key-pair --key-name ecs-key-stage --query 'KeyMaterial' --output text > ecs-key-stage.pem
chmod 400 ecs-key-stage.pem
public_key=`ssh-keygen -y -f ecs-key-stage.pem`
8 changes: 7 additions & 1 deletion terraform/ecs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ module "ecs" {
vpc_cidr = var.vpc_cidr
project_name = var.project_name
cluster = var.cluster
max_size = var.max_size
min_size = var.min_size
desired_capacity = var.desired_capacity
instance_type = var.instance_type
ecs_aws_ami = var.ecs_aws_ami
environment = var.environment
Expand All @@ -18,7 +21,7 @@ module "ecs" {

resource "aws_key_pair" "ecs" {
key_name = "ecs-key-${var.environment}"
public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCtMljjj0Ccxux5Mssqraa/iHHxheW+m0Rh17fbd8t365y9EwBn00DN/0PjdU2CK6bjxwy8BNGXWoUXiSDDtGqRupH6e9J012yE5kxhpXnnkIcLGjkAiflDBVV4sXS4b3a2LSXL5Dyb93N2GdnJ03FJM4qDJ8lfDQxb38eYHytZkmxW14xLoyW5Hbyr3SXhdHC2/ecdp5nLNRwRWiW6g9OA6jTQ3LgeOZoM6dK4ltJUQOakKjiHsE+jvmO0hJYQN7+5gYOw0HHsM+zmATvSipAWzoWBWcmBxAbcdW0R0KvCwjylCyRVbRMRbSZ/c4idZbFLZXRb7ZJkqNJuy99+ld41 ecs@aws.fake"
public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC9H/2sFSFdfL8oOh1Aba0rrcZFVuUzE+Y9HYj41ghWQCWf6tx+F6EcFl8f+0ZkPDGxUW7i8cVKWbGA9oSFGLN0GDJwIODN8SBZxw/iRDtINa4W6svCyf4yBVLJ+EuLQSqQ4FBAN4jk2s8h1CTWUvIYaFib486I82PqOoo81JbK8Dm7NxtcSz1MDt81T0OUMXZSqiLvtR1r3HxGjk1zSaeAHXlr73wNumEcZVg3u6xGLvwhK3SWPAU9wDE1eaXFSgqXcGM+9w/wGXV+bxiBmWYx8k5+RjHTgd3oRI3hwbYpfShjectT2HZ2b7VHikLd8pkxrDfkpvf/fUyWN6y0Rssh"
}

variable "aws_region" {
Expand All @@ -30,6 +33,9 @@ variable "aws_region" {
# default =
# }

variable "desired_capacity" {}
variable "max_size" {}
variable "min_size" {}
variable "cluster" {
description = "The name of the ECS cluster"
}
Expand Down
3 changes: 3 additions & 0 deletions terraform/ecs.tfvars
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ availability_zones = ["us-east-1a", "us-east-1b"]
aws_region = "us-east-1"
instance_type = "t2.micro"
ecs_aws_ami = "ami-275ffe31"
max_size = 1
min_size = 1
desired_capacity = 1
51 changes: 47 additions & 4 deletions terraform/ecs_module/ecs_instances/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ resource "aws_ecs_service" "price-dashboard-service" {
name = "price-dashboard-service"
cluster = aws_ecs_cluster.cluster.id
launch_type = "EC2"
desired_count = 1
task_definition = aws_ecs_task_definition.cluster.arn
network_configuration {
subnets = var.private_subnet_ids
Expand Down Expand Up @@ -61,13 +62,13 @@ resource "aws_launch_configuration" "launch" {
image_id = var.ecs_aws_ami
instance_type = var.instance_type
security_groups = [aws_security_group.instance.id]
user_data = templatefile("${path.module}/templates/launch_user_data.sh", {
ecs_config = var.ecs_config
ecs_logging = var.ecs_logging
user_data_base64 = base64encode(templatefile("${path.module}/templates/launch_user_data.sh", {
# ecs_config = var.ecs_config
# ecs_logging = var.ecs_logging
cluster_name = var.cluster
env_name = var.environment
custom_userdata = var.custom_userdata
})
}))
iam_instance_profile = aws_iam_instance_profile.ec2-readonly-profile.id
key_name = var.key_name

Expand All @@ -79,3 +80,45 @@ resource "aws_launch_configuration" "launch" {
}
}

resource "aws_autoscaling_group" "asg" {
name = "${var.environment}_${var.cluster}_${var.instance_group}"
max_size = var.max_size
min_size = var.min_size
desired_capacity = var.desired_capacity
force_delete = true
launch_configuration = aws_launch_configuration.launch.id
vpc_zone_identifier = var.private_subnet_ids
load_balancers = var.load_balancers

tag {
key = "Name"
value = "${var.environment}_ecs_${var.cluster}_${var.instance_group}"
propagate_at_launch = "true"
}

tag {
key = "Environment"
value = var.environment
propagate_at_launch = "true"
}

tag {
key = "Cluster"
value = var.cluster
propagate_at_launch = "true"
}

tag {
key = "InstanceGroup"
value = var.instance_group
propagate_at_launch = "true"
}

# EC2 instances require internet connectivity to boot. Thus EC2 instances must not start before NAT is available.
# For info why see description in the network module.
tag {
key = "DependsId"
value = var.depends_id
propagate_at_launch = "false"
}
}
14 changes: 7 additions & 7 deletions terraform/ecs_module/ecs_instances/templates/launch_user_data.sh
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
#!/bin/bash

# Timezone
ln -fs /usr/share/zoneinfo/Europe/Amsterdam /etc/localtime
# # Timezone
# ln -fs /usr/share/zoneinfo/Europe/Amsterdam /etc/localtime

# ECS config
${ecs_config}
{
echo "ECS_CLUSTER=${cluster_name}"
echo 'ECS_AVAILABLE_LOGGING_DRIVERS=${ecs_logging}'
} >> /etc/ecs/ecs.config
echo ECS_CLUSTER=${cluster_name} >> /etc/ecs/ecs.config

start ecs

# yum install -y aws-cli jq

#Get ECS instance info, althoug not used in this user_data it self this allows you to use
#az(availability zone) and region
until $(curl --output /dev/null --silent --head --fail http://localhost:51678/v1/metadata); do
Expand All @@ -22,6 +20,8 @@ instance_arn=$(curl -s http://localhost:51678/v1/metadata | jq -r '. | .Containe
az=$(curl -s http://instance-data/latest/meta-data/placement/availability-zone)
region=$${az:0:$${#az} - 1}

aws ecs start-task --cluster ${cluster_name} --task-definition cadvisor:1 --container-instances $instance_arn --region $region

#Custom userdata script code
${custom_userdata}

Expand Down
22 changes: 22 additions & 0 deletions terraform/ecs_module/ecs_instances/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ variable "custom_userdata" {
description = "Inject extra command in the instance template to be run on boot"
}

variable "load_balancers" {
type = list
default = []
description = "The load balancers to couple to the instances. Only used when NOT using ALB"
}

variable "key_name" {
description = "SSH key name to be used"
}
Expand All @@ -61,3 +67,19 @@ variable "ecs_logging" {
default = "[\"json-file\",\"awslogs\"]"
description = "Adding logging option to ECS that the Docker containers can use. It is possible to add fluentd as well"
}

variable "max_size" {
description = "Maximum size of the nodes in the cluster"
}

variable "min_size" {
description = "Minimum size of the nodes in the cluster"
}

variable "desired_capacity" {
description = "The desired capacity of the cluster"
}

variable "depends_id" {
description = "Workaround to wait for the NAT gateway to finish before starting the instances"
}
7 changes: 6 additions & 1 deletion terraform/ecs_module/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,17 @@ module "ecs_instances" {
environment = var.environment
project_name = var.project_name
cluster = var.cluster
desired_capacity = var.desired_capacity
instance_group = var.instance_group
load_balancers = var.load_balancers
instance_type = var.instance_type
max_size = var.max_size
min_size = var.min_size
custom_userdata = var.custom_userdata
ecs_aws_ami = var.ecs_aws_ami
redis_endpoint_address = module.ecs_cache.redis_endpoint_address
key_name = var.key_name
redis_endpoint_address = module.ecs_cache.redis_endpoint_address
depends_id = module.network.depends_id
vpc_id = module.network.vpc_id
private_subnet_ids = module.network.private_subnet_ids
alb_target_group_arn = module.ecs_alb.alb_target_group_arn
Expand Down
17 changes: 17 additions & 0 deletions terraform/ecs_module/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ variable "instance_group" {
description = "The name of the instances that you consider as a group"
}

variable "load_balancers" {
type = list
default = []
description = "The load balancers to couple to the instances"
}

variable "instance_type" {
description = "AWS instance type to use"
}
Expand All @@ -52,3 +58,14 @@ variable "key_name" {
description = "SSH key name to be used"
}

variable "max_size" {
description = "Maximum size of the nodes in the cluster"
}

variable "min_size" {
description = "Minimum size of the nodes in the cluster"
}

variable "desired_capacity" {
description = "The desired capacity of the cluster"
}
9 changes: 5 additions & 4 deletions terraform/shared/subnet/main.tf
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
resource "aws_subnet" "subnet" {
vpc_id = var.vpc_id
cidr_block = element(var.cidr_blocks, count.index)
availability_zone = element(var.availability_zones, count.index)
count = length(var.cidr_blocks)
vpc_id = var.vpc_id
cidr_block = element(var.cidr_blocks, count.index)
availability_zone = element(var.availability_zones, count.index)
count = length(var.cidr_blocks)
map_public_ip_on_launch = true

tags = {
Name = "${var.subnet_name}_${element(var.availability_zones, count.index)}"
Expand Down

0 comments on commit 0722762

Please sign in to comment.