mirror of
https://github.com/farcasclaudiu/terraform-course.git
synced 2026-06-28 21:01:57 +03:00
elasticbeanstalk demo
This commit is contained in:
@@ -24,6 +24,7 @@ demo-13 | IAM
|
||||
demo-14 | IAM Roles with S3 bucket
|
||||
demo-15 | Autoscaling
|
||||
demo-16 | Autoscaling with ELB (Elastic Load Balancer)
|
||||
demo-17 | Elastic Beanstalk PHP 7 stack with RDS
|
||||
packer-demo | Build AMIs with Packer
|
||||
jenkins-packer-demo | Demo with jenkins and Packer
|
||||
docker-demo-1 | Using ECR - The EC2 Container Registry
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
resource "aws_elastic_beanstalk_application" "app" {
|
||||
name = "app"
|
||||
description = "app"
|
||||
}
|
||||
|
||||
resource "aws_elastic_beanstalk_environment" "app-prod" {
|
||||
name = "app-prod"
|
||||
application = "${aws_elastic_beanstalk_application.app.name}"
|
||||
solution_stack_name = "64bit Amazon Linux 2016.09 v2.2.0 running PHP 7.0"
|
||||
setting {
|
||||
namespace = "aws:ec2:vpc"
|
||||
name = "VPCId"
|
||||
value = "${aws_vpc.main.id}"
|
||||
}
|
||||
setting {
|
||||
namespace = "aws:ec2:vpc"
|
||||
name = "Subnets"
|
||||
value = "${aws_subnet.main-private-1.id},${aws_subnet.main-private-2.id}"
|
||||
}
|
||||
setting {
|
||||
namespace = "aws:ec2:vpc"
|
||||
name = "AssociatePublicIpAddress"
|
||||
value = "false"
|
||||
}
|
||||
setting {
|
||||
namespace = "aws:autoscaling:launchconfiguration"
|
||||
name = "IamInstanceProfile"
|
||||
value = "app-ec2-role"
|
||||
}
|
||||
setting {
|
||||
namespace = "aws:autoscaling:launchconfiguration"
|
||||
name = "SecurityGroups"
|
||||
value = "${aws_security_group.app-prod.id}"
|
||||
}
|
||||
setting {
|
||||
namespace = "aws:autoscaling:launchconfiguration"
|
||||
name = "EC2KeyName"
|
||||
value = "${aws_key_pair.mykeypair.id}"
|
||||
}
|
||||
setting {
|
||||
namespace = "aws:autoscaling:launchconfiguration"
|
||||
name = "InstanceType"
|
||||
value = "t2.micro"
|
||||
}
|
||||
setting {
|
||||
namespace = "aws:elasticbeanstalk:environment"
|
||||
name = "ServiceRole"
|
||||
value = "aws-elasticbeanstalk-service-role"
|
||||
}
|
||||
setting {
|
||||
namespace = "aws:ec2:vpc"
|
||||
name = "ELBScheme"
|
||||
value = "public"
|
||||
}
|
||||
setting {
|
||||
namespace = "aws:ec2:vpc"
|
||||
name = "ELBSubnets"
|
||||
value = "${aws_subnet.main-public-1.id},${aws_subnet.main-public-2.id}"
|
||||
}
|
||||
setting {
|
||||
namespace = "aws:elb:loadbalancer"
|
||||
name = "CrossZone"
|
||||
value = "true"
|
||||
}
|
||||
setting {
|
||||
namespace = "aws:elasticbeanstalk:command"
|
||||
name = "BatchSize"
|
||||
value = "30"
|
||||
}
|
||||
setting {
|
||||
namespace = "aws:elasticbeanstalk:command"
|
||||
name = "BatchSizeType"
|
||||
value = "Percentage"
|
||||
}
|
||||
setting {
|
||||
namespace = "aws:autoscaling:asg"
|
||||
name = "Availability Zones"
|
||||
value = "Any 2"
|
||||
}
|
||||
setting {
|
||||
namespace = "aws:autoscaling:asg"
|
||||
name = "MinSize"
|
||||
value = "1"
|
||||
}
|
||||
setting {
|
||||
namespace = "aws:autoscaling:updatepolicy:rollingupdate"
|
||||
name = "RollingUpdateType"
|
||||
value = "Health"
|
||||
}
|
||||
setting {
|
||||
namespace = "aws:elasticbeanstalk:application:environment"
|
||||
name = "RDS_USERNAME"
|
||||
value = "${aws_db_instance.mariadb.username}"
|
||||
}
|
||||
setting {
|
||||
namespace = "aws:elasticbeanstalk:application:environment"
|
||||
name = "RDS_PASSWORD"
|
||||
value = "${aws_db_instance.mariadb.password}"
|
||||
}
|
||||
setting {
|
||||
namespace = "aws:elasticbeanstalk:application:environment"
|
||||
name = "RDS_DATABASE"
|
||||
value = "mydb"
|
||||
value = "${aws_db_instance.mariadb.name}"
|
||||
}
|
||||
setting {
|
||||
namespace = "aws:elasticbeanstalk:application:environment"
|
||||
name = "RDS_HOSTNAME"
|
||||
value = "${aws_db_instance.mariadb.endpoint}"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
resource "aws_key_pair" "mykeypair" {
|
||||
key_name = "mykeypair"
|
||||
public_key = "${file("${var.PATH_TO_PUBLIC_KEY}")}"
|
||||
lifecycle {
|
||||
ignore_changes = ["public_key"]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
provider "aws" {
|
||||
region = "${var.AWS_REGION}"
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
resource "aws_db_subnet_group" "mariadb-subnet" {
|
||||
name = "mariadb-subnet"
|
||||
description = "RDS subnet group"
|
||||
subnet_ids = ["${aws_subnet.main-private-1.id}","${aws_subnet.main-private-2.id}"]
|
||||
}
|
||||
|
||||
resource "aws_db_parameter_group" "mariadb-parameters" {
|
||||
name = "mariadb-parameters"
|
||||
family = "mariadb10.1"
|
||||
description = "MariaDB parameter group"
|
||||
|
||||
parameter {
|
||||
name = "max_allowed_packet"
|
||||
value = "16777216"
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
resource "aws_db_instance" "mariadb" {
|
||||
allocated_storage = 100 # 100 GB of storage, gives us more IOPS than a lower number
|
||||
engine = "mariadb"
|
||||
engine_version = "10.1.14"
|
||||
instance_class = "db.t2.small" # use micro if you want to use the free tier
|
||||
identifier = "mariadb"
|
||||
name = "mariadb"
|
||||
username = "root" # username
|
||||
password = "${var.RDS_PASSWORD}" # password
|
||||
db_subnet_group_name = "${aws_db_subnet_group.mariadb-subnet.name}"
|
||||
parameter_group_name = "mariadb-parameters"
|
||||
multi_az = "false" # set to true to have high availability: 2 instances synchronized with each other
|
||||
vpc_security_group_ids = ["${aws_security_group.allow-mariadb.id}"]
|
||||
storage_type = "gp2"
|
||||
backup_retention_period = 30 # how long you’re going to keep your backups
|
||||
availability_zone = "${aws_subnet.main-private-1.availability_zone}" # prefered AZ
|
||||
tags {
|
||||
Name = "mariadb-instance"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
resource "aws_security_group" "app-prod" {
|
||||
vpc_id = "${aws_vpc.main.id}"
|
||||
name = "application - production"
|
||||
description = "security group for my app"
|
||||
egress {
|
||||
from_port = 0
|
||||
to_port = 0
|
||||
protocol = "-1"
|
||||
cidr_blocks = ["0.0.0.0/0"]
|
||||
}
|
||||
|
||||
ingress {
|
||||
from_port = 22
|
||||
to_port = 22
|
||||
protocol = "tcp"
|
||||
cidr_blocks = ["0.0.0.0/0"]
|
||||
}
|
||||
|
||||
|
||||
tags {
|
||||
Name = "myinstance"
|
||||
}
|
||||
}
|
||||
resource "aws_security_group" "allow-mariadb" {
|
||||
vpc_id = "${aws_vpc.main.id}"
|
||||
name = "allow-mariadb"
|
||||
description = "allow-mariadb"
|
||||
ingress {
|
||||
from_port = 3306
|
||||
to_port = 3306
|
||||
protocol = "tcp"
|
||||
security_groups = ["${aws_security_group.app-prod.id}"] # allowing access from our example instance
|
||||
}
|
||||
egress {
|
||||
from_port = 0
|
||||
to_port = 0
|
||||
protocol = "-1"
|
||||
cidr_blocks = ["0.0.0.0/0"]
|
||||
self = true
|
||||
}
|
||||
tags {
|
||||
Name = "allow-mariadb"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
variable "AWS_REGION" {
|
||||
default = "eu-west-1"
|
||||
}
|
||||
variable "PATH_TO_PRIVATE_KEY" {
|
||||
default = "mykey"
|
||||
}
|
||||
variable "PATH_TO_PUBLIC_KEY" {
|
||||
default = "mykey.pub"
|
||||
}
|
||||
variable "RDS_PASSWORD" {}
|
||||
+110
@@ -0,0 +1,110 @@
|
||||
# Internet VPC
|
||||
resource "aws_vpc" "main" {
|
||||
cidr_block = "10.0.0.0/16"
|
||||
instance_tenancy = "default"
|
||||
enable_dns_support = "true"
|
||||
enable_dns_hostnames = "true"
|
||||
enable_classiclink = "false"
|
||||
tags {
|
||||
Name = "main"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
# Subnets
|
||||
resource "aws_subnet" "main-public-1" {
|
||||
vpc_id = "${aws_vpc.main.id}"
|
||||
cidr_block = "10.0.1.0/24"
|
||||
map_public_ip_on_launch = "true"
|
||||
availability_zone = "eu-west-1a"
|
||||
|
||||
tags {
|
||||
Name = "main-public-1"
|
||||
}
|
||||
}
|
||||
resource "aws_subnet" "main-public-2" {
|
||||
vpc_id = "${aws_vpc.main.id}"
|
||||
cidr_block = "10.0.2.0/24"
|
||||
map_public_ip_on_launch = "true"
|
||||
availability_zone = "eu-west-1b"
|
||||
|
||||
tags {
|
||||
Name = "main-public-2"
|
||||
}
|
||||
}
|
||||
resource "aws_subnet" "main-public-3" {
|
||||
vpc_id = "${aws_vpc.main.id}"
|
||||
cidr_block = "10.0.3.0/24"
|
||||
map_public_ip_on_launch = "true"
|
||||
availability_zone = "eu-west-1c"
|
||||
|
||||
tags {
|
||||
Name = "main-public-3"
|
||||
}
|
||||
}
|
||||
resource "aws_subnet" "main-private-1" {
|
||||
vpc_id = "${aws_vpc.main.id}"
|
||||
cidr_block = "10.0.4.0/24"
|
||||
map_public_ip_on_launch = "false"
|
||||
availability_zone = "eu-west-1a"
|
||||
|
||||
tags {
|
||||
Name = "main-private-1"
|
||||
}
|
||||
}
|
||||
resource "aws_subnet" "main-private-2" {
|
||||
vpc_id = "${aws_vpc.main.id}"
|
||||
cidr_block = "10.0.5.0/24"
|
||||
map_public_ip_on_launch = "false"
|
||||
availability_zone = "eu-west-1b"
|
||||
|
||||
tags {
|
||||
Name = "main-private-2"
|
||||
}
|
||||
}
|
||||
resource "aws_subnet" "main-private-3" {
|
||||
vpc_id = "${aws_vpc.main.id}"
|
||||
cidr_block = "10.0.6.0/24"
|
||||
map_public_ip_on_launch = "false"
|
||||
availability_zone = "eu-west-1c"
|
||||
|
||||
tags {
|
||||
Name = "main-private-3"
|
||||
}
|
||||
}
|
||||
|
||||
# Internet GW
|
||||
resource "aws_internet_gateway" "main-gw" {
|
||||
vpc_id = "${aws_vpc.main.id}"
|
||||
|
||||
tags {
|
||||
Name = "main"
|
||||
}
|
||||
}
|
||||
|
||||
# route tables
|
||||
resource "aws_route_table" "main-public" {
|
||||
vpc_id = "${aws_vpc.main.id}"
|
||||
route {
|
||||
cidr_block = "0.0.0.0/0"
|
||||
gateway_id = "${aws_internet_gateway.main-gw.id}"
|
||||
}
|
||||
|
||||
tags {
|
||||
Name = "main-public-1"
|
||||
}
|
||||
}
|
||||
|
||||
# route associations public
|
||||
resource "aws_route_table_association" "main-public-1-a" {
|
||||
subnet_id = "${aws_subnet.main-public-1.id}"
|
||||
route_table_id = "${aws_route_table.main-public.id}"
|
||||
}
|
||||
resource "aws_route_table_association" "main-public-2-a" {
|
||||
subnet_id = "${aws_subnet.main-public-2.id}"
|
||||
route_table_id = "${aws_route_table.main-public.id}"
|
||||
}
|
||||
resource "aws_route_table_association" "main-public-3-a" {
|
||||
subnet_id = "${aws_subnet.main-public-3.id}"
|
||||
route_table_id = "${aws_route_table.main-public.id}"
|
||||
}
|
||||
Reference in New Issue
Block a user