diff --git a/.gitignore b/.gitignore index 164c76d..7e0b66d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .env .terraform* *.tfstate* +*.zip \ No newline at end of file diff --git a/elasting_running_worpress/elastic.tf b/elasting_running_worpress/elastic.tf new file mode 100644 index 0000000..94612eb --- /dev/null +++ b/elasting_running_worpress/elastic.tf @@ -0,0 +1,20 @@ +data "aws_instance" "alive" { + + filter { + name = "image-id" + values = [ "ami-63ec5b1e"] + } + + filter { + name = "instance-state-name" + values = [ "running"] + } + +} + + +resource "aws_eip" "bar" { + vpc = true + + instance = data.aws_instance.alive.id +} \ No newline at end of file diff --git a/elasting_running_worpress/main.tf b/elasting_running_worpress/main.tf new file mode 100644 index 0000000..0aa7c69 --- /dev/null +++ b/elasting_running_worpress/main.tf @@ -0,0 +1,17 @@ +terraform { + required_providers { + aws = { + source = "hashicorp/aws" + version = "3.65.0" + } + } + + required_version = ">= 0.14.9" +} + +provider "aws" {} # provided via env file + +variable "aws_region" { + # provided via env file + description = "Aws region to use" +} \ No newline at end of file diff --git a/readme.md b/readme.md index 3331a08..49cb6a1 100644 --- a/readme.md +++ b/readme.md @@ -43,6 +43,16 @@ make PROJECT=vm_import destoy # Available projects + +## vpc_ha + +Creates a VPC with a public and a private subnet for each abailability zone. +Creates an autoscaling group to maintain a single worpress insance available across the 3 availability zones +Creates an Elastic Ip and uses a Livecycle hook with a python lambta to keep it attached to the running instance upon scaling events. + +## elasting_running_worpress/ +Retrieve current running wodpress insance and "manually" attach elastic IP to it + ## lambda_api_gw Creates an HelloWord lambra function and exposes it via Api Gateway diff --git a/vpc_ha/autoscale_instance.tf b/vpc_ha/autoscale_instance.tf index 9c18822..4f22e57 100644 --- a/vpc_ha/autoscale_instance.tf +++ b/vpc_ha/autoscale_instance.tf @@ -33,7 +33,7 @@ resource "aws_launch_configuration" "launch_wordpress" { resource "aws_autoscaling_group" "autoscale_single" { name = "single_ha_wordpress" launch_configuration = aws_launch_configuration.launch_wordpress.name - vpc_zone_identifier = [for subnet in aws_subnet.private : "${subnet.id}"] + vpc_zone_identifier = [for subnet in aws_subnet.public : "${subnet.id}"] min_size = 1 max_size = 1 desired_capacity = 1 @@ -43,9 +43,45 @@ resource "aws_autoscaling_group" "autoscale_single" { } - - ### SSH Key ### data "aws_key_pair" "deploy" { key_name = "deploykey" +} + + +#### lifecycle management ### + +resource "aws_autoscaling_lifecycle_hook" "insance_start_hook" { + name = "ec2_instance_start" + autoscaling_group_name = aws_autoscaling_group.autoscale_single.name + default_result = "CONTINUE" + heartbeat_timeout = 30 + lifecycle_transition = "autoscaling:EC2_INSTANCE_LAUNCHING" + + notification_metadata = <