Skip to content

Instantly share code, notes, and snippets.

@cedriczirtacic
Last active February 2, 2018 21:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cedriczirtacic/cf6e7696d6c183a82efd114ad53631ae to your computer and use it in GitHub Desktop.
Save cedriczirtacic/cf6e7696d6c183a82efd114ad53631ae to your computer and use it in GitHub Desktop.
auxis interview test (terraform code, tomcat recipe, chefspec for recipe)
# terraform code
provider "aws" {
region = "us-west-2"
}
#######
### NETWORKING INFRAESTRUCTURE
#######
# vpc
resource "aws_vpc" "auxis-VPC" {
cidr_block = "10.0.0.0/16"
}
# subnet
resource "aws_subnet" "auxis-Subnet" {
vpc_id = "${aws_vpc.auxis-VPC.id}"
cidr_block = "10.0.0.0/24"
map_public_ip_on_launch = true
}
# internet gw
resource "aws_internet_gateway" "auxis-IGW" {
vpc_id = "${aws_vpc.auxis-VPC.id}"
}
# route table
resource "aws_route" "auxis-Route" {
route_table_id = "${aws_vpc.auxis-VPC.main_route_table_id}"
destination_cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.auxis-IGW.id}"
}
# associate route table to subnet 10.0.0.0/24
resource "aws_route_table_association" "route_assoc" {
subnet_id = "${aws_subnet.auxis-Subnet.id}"
route_table_id = "${aws_route.auxis-Route.route_table_id}"
}
# security group
resource "aws_security_group" "auxis-SecGroup" {
name = "main_security_group"
vpc_id = "${aws_vpc.auxis-VPC.id}"
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1" # all protocols
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_security_group" "auxis-SecGroup-Tomcat" {
name = "tomcat_security_group"
vpc_id = "${aws_vpc.auxis-VPC.id}"
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 = ["10.0.0.0/16"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1" # all protocols
cidr_blocks = ["0.0.0.0/0"]
}
}
#######
### INSTANCES
#######
# ec2 instance
resource "aws_instance" "auxis-Tomcat" {
ami = "ami-f2d3638a"
instance_type = "t2.micro"
key_name = "auxis_keypair"
subnet_id = "${aws_subnet.auxis-Subnet.id}"
vpc_security_group_ids = ["${aws_security_group.auxis-SecGroup-Tomcat.id}"]
provisioner "chef" {
server_url = "https://api.chef.io/organizations/auxis_test"
node_name = "auxis_test"
user_key = "${file("./chef-repo/.chef/cicatriz.pem")}"
user_name = "cicatriz"
recreate_client = true
run_list = ["tomcat::default"]
connection {
type = "ssh"
agent = true
private_key = "${file("../auxis_keypair")}"
user = "ec2-user"
}
}
# provisioner "remote-exec" {
# inline = [
# "mkdir /tmp/test",
# ]
# }
}
# load balancer instance
resource "aws_elb" "auxis-ELB" {
name = "auxis-ELB"
instances = ["${aws_instance.auxis-Tomcat.id}"]
security_groups = ["${aws_security_group.auxis-SecGroup.id}"]
subnets = ["${aws_subnet.auxis-Subnet.id}"]
listener {
instance_port = 8080
instance_protocol = "http"
lb_port = 80
lb_protocol = "http"
}
}
output "Instace IPv4 address" {
value = "${aws_instance.auxis-Tomcat.public_ip}"
}
output "ELB DNS address" {
value = "${aws_elb.auxis-ELB.dns_name}"
}
output "Sample URL" {
value = "http://${aws_elb.auxis-ELB.dns_name}/sample/"
}
# This is a Chef recipe file. It can be used to specify resources which will
# apply configuration to a server.
log "Deploying Apache Tomcat on #{node["starter_name"]}!" do
level :info
end
yum_package 'tomcat7' do
action :install
end
cookbook_file "/usr/share/tomcat7/webapps/sample.war" do
source "sample.war"
owner "root"
group "root"
mode "644"
action :create
end
service 'tomcat7' do
action :start
#only_if { ::File.exist?('/etc/tomcat7/tomcat7.conf') }
end
# used for sepcs testing
require 'chefspec'
describe 'tomcat::default' do
let(:chef_run) { ChefSpec::SoloRunner.converge(described_recipe) }
it 'installs tomcat7' do
expect(chef_run).to install_yum_package('tomcat7')
end
context 'file' do
it 'renders the file' do
expect(chef_run).to render_file('/usr/share/tomcat7/webapps/sample.war')
end
end
# context 'service' do
it 'starts a service with an explicit action' do
expect(chef_run).to start_service('tomcat7')
end
# end
end

auxis test

Intro

This is my solution for the auxis test provided at the technical interview.

AWS

For our Terraform instance to connect to the AWS instances, we will need to generate a key pair:

$ ssh-keygen -t rsa -f auxis_keypair
...
$ ls auxis_keypair*
auxis_keypair  auxis_keypair.pub

auxis_keypair is our PEM file and the other is the public key that we will be using for our connection thru the SSH Agent. Then you need to import this using the AWS console in order to be used:

  1. Go to Services and under Compute click on EC2. The EC2 Dashboard will welcome you with information about your current instances, security groups, key pairs, etc.
  2. Under Resources you will see the Key Pairs link.
  3. Click on Import Key Pair and then upload the public key file previously created.
  4. Set the name as auxis_keypair and click on Import.

You'll need to set up the ssh_agent:

$ eval `ssh-agent`

Note: The keys should be in the root directory of the repo, one directory before the Terraform and Chef code (../terraform/)

Chef

Cookbooks are already included in the commit. You can test the specs:

$ cd chef-repo/cookbooks/tomcat/
$ chef exec rspec

And foodcritic:

$ cd chef-repo/
$ foodcritic -B cookbooks/

Terraform

After installing Terraform (in my case I used pacman: sudo pacman -S -q community/terraform) we change the working directory to terraform/ and init it:

$ cd terraform/ && terraform init

Tip: you can check the plan via terraform plan or reverse the plan with terraform destroy -force.

If everything is OK then you have to export the AWS Access Key and Secret Access Key in order to run the terraform plan:

$ export AWS_ACCESS_KEY_ID="..."
$ export AWS_SECRET_ACCESS_KEY="..."
$ terraform apply -auto-approve

And it will start deploying the environment.

Note: Due to DNS catching or replication, check the load balancers' DNS name from other clients.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment