diff --git a/docker-demo-3/cloudinit.tf b/docker-demo-3/cloudinit.tf new file mode 100644 index 0000000..2de0ab7 --- /dev/null +++ b/docker-demo-3/cloudinit.tf @@ -0,0 +1,20 @@ +provider "cloudinit" {} + +data "template_file" "jenkins-init" { + template = "${file("scripts/jenkins-init.sh")}" + vars { + DEVICE = "${var.INSTANCE_DEVICE_NAME}" + JENKINS_VERSION = "${var.JENKINS_VERSION}" + } +} +data "template_cloudinit_config" "cloudinit-jenkins" { + + gzip = false + base64_encode = false + + part { + content_type = "text/x-shellscript" + content = "${data.template_file.jenkins-init.rendered}" + } + +} diff --git a/docker-demo-3/ecr.tf b/docker-demo-3/ecr.tf new file mode 100644 index 0000000..c136228 --- /dev/null +++ b/docker-demo-3/ecr.tf @@ -0,0 +1,3 @@ +resource "aws_ecr_repository" "myapp" { + name = "myapp" +} diff --git a/docker-demo-3/ecs.tf b/docker-demo-3/ecs.tf new file mode 100644 index 0000000..4dc7f7e --- /dev/null +++ b/docker-demo-3/ecs.tf @@ -0,0 +1,28 @@ +# cluster +resource "aws_ecs_cluster" "example-cluster" { + name = "example-cluster" +} +resource "aws_launch_configuration" "ecs-example-launchconfig" { + name_prefix = "ecs-launchconfig" + image_id = "${lookup(var.ECS_AMIS, var.AWS_REGION)}" + instance_type = "${var.ECS_INSTANCE_TYPE}" + key_name = "${aws_key_pair.mykeypair.key_name}" + iam_instance_profile = "${aws_iam_instance_profile.ecs-ec2-role.id}" + security_groups = ["${aws_security_group.ecs-securitygroup.id}"] + user_data = "#!/bin/bash\necho 'ECS_CLUSTER=example-cluster' > /etc/ecs/ecs.config\nstart ecs" + lifecycle { create_before_destroy = true } +} +resource "aws_autoscaling_group" "ecs-example-autoscaling" { + name = "ecs-example-autoscaling" + vpc_zone_identifier = ["${aws_subnet.main-public-1.id}", "${aws_subnet.main-public-2.id}"] + launch_configuration = "${aws_launch_configuration.ecs-example-launchconfig.name}" + min_size = 1 + max_size = 1 + tag { + key = "Name" + value = "ecs-ec2-container" + propagate_at_launch = true + } +} + + diff --git a/docker-demo-3/iam.tf b/docker-demo-3/iam.tf new file mode 100644 index 0000000..ca5616f --- /dev/null +++ b/docker-demo-3/iam.tf @@ -0,0 +1,113 @@ +# ecs ec2 role +resource "aws_iam_role" "ecs-ec2-role" { + name = "ecs-ec2-role" + assume_role_policy = <> /etc/fstab +mount /var/lib/jenkins + +# install jenkins and docker +wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add - +echo "deb http://pkg.jenkins.io/debian-stable binary/" >> /etc/apt/sources.list +apt-get update +apt-get install -y jenkins=${JENKINS_VERSION} unzip docker.io + +# enable docker and add perms +usermod -G docker jenkins +systemctl enable docker +service docker start +service jenkins restart + +# install pip +wget -q https://bootstrap.pypa.io/get-pip.py +python get-pip.py +python3 get-pip.py +rm -f get-pip.py +# install awscli +pip install awscli + +# install terraform +cd /usr/local/bin +wget -q https://releases.hashicorp.com/terraform/0.7.7/terraform_0.7.7_linux_amd64.zip +unzip terraform_0.7.7_linux_amd64.zip +# clean up +apt-get clean +rm terraform_0.7.7_linux_amd64.zip diff --git a/docker-demo-3/securitygroup.tf b/docker-demo-3/securitygroup.tf new file mode 100644 index 0000000..60f749b --- /dev/null +++ b/docker-demo-3/securitygroup.tf @@ -0,0 +1,76 @@ +resource "aws_security_group" "ecs-securitygroup" { + vpc_id = "${aws_vpc.main.id}" + name = "ecs" + description = "security group for ecs" + egress { + from_port = 0 + to_port = 0 + protocol = "-1" + cidr_blocks = ["0.0.0.0/0"] + } + + ingress { + from_port = 3000 + to_port = 3000 + protocol = "tcp" + security_groups = ["${aws_security_group.myapp-elb-securitygroup.id}"] + } + ingress { + from_port = 22 + to_port = 22 + protocol = "tcp" + cidr_blocks = ["0.0.0.0/0"] + } + tags { + Name = "ecs" + } +} +resource "aws_security_group" "myapp-elb-securitygroup" { + vpc_id = "${aws_vpc.main.id}" + name = "myapp-elb" + description = "security group for ecs" + egress { + from_port = 0 + to_port = 0 + protocol = "-1" + cidr_blocks = ["0.0.0.0/0"] + } + + ingress { + from_port = 80 + to_port = 80 + protocol = "tcp" + cidr_blocks = ["0.0.0.0/0"] + } + tags { + Name = "myapp-elb" + } +} +# jenkins +resource "aws_security_group" "jenkins-securitygroup" { + vpc_id = "${aws_vpc.main.id}" + name = "jenkins-securitygroup" + description = "security group that allows ssh and all egress traffic" + 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"] + } + ingress { + from_port = 8080 + to_port = 8080 + protocol = "tcp" + cidr_blocks = ["0.0.0.0/0"] + } +tags { + Name = "jenkins-securitygroup" + } +} diff --git a/docker-demo-3/templates/app.json.tpl b/docker-demo-3/templates/app.json.tpl new file mode 100644 index 0000000..59afc10 --- /dev/null +++ b/docker-demo-3/templates/app.json.tpl @@ -0,0 +1,18 @@ +[ + { + "essential": true, + "memory": 256, + "name": "myapp", + "cpu": 256, + "image": "${REPOSITORY_URL}:${APP_VERSION}", + "workingDirectory": "/app", + "command": ["npm", "start"], + "portMappings": [ + { + "containerPort": 3000, + "hostPort": 3000 + } + ] + } +] + diff --git a/docker-demo-3/vars.tf b/docker-demo-3/vars.tf new file mode 100644 index 0000000..466daba --- /dev/null +++ b/docker-demo-3/vars.tf @@ -0,0 +1,37 @@ +variable "AWS_REGION" { + default = "eu-west-1" +} +variable "PATH_TO_PRIVATE_KEY" { + default = "mykey" +} +variable "PATH_TO_PUBLIC_KEY" { + default = "mykey.pub" +} +variable "ECS_INSTANCE_TYPE" { + default = "t2.micro" +} +variable "ECS_AMIS" { + type = "map" + default = { + us-east-1 = "ami-1924770e" + us-west-2 = "ami-56ed4936" + eu-west-1 = "ami-c8337dbb" + } +} +# Full List: http://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-optimized_AMI.html + +variable "AMIS" { + type = "map" + default = { + us-east-1 = "ami-13be557e" + us-west-2 = "ami-06b94666" + eu-west-1 = "ami-844e0bf7" + } +} +variable "INSTANCE_DEVICE_NAME" { + default = "/dev/xvdh" +} +variable "JENKINS_VERSION" { + default = "2.19.1" +} + diff --git a/docker-demo-3/vpc.tf b/docker-demo-3/vpc.tf new file mode 100644 index 0000000..1325feb --- /dev/null +++ b/docker-demo-3/vpc.tf @@ -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}" +}