All Projects → robert-stuttaford → Terraform Example

robert-stuttaford / Terraform Example

Terraform, Ansible, sticky tape and magic

Labels

Projects that are alternatives of or similar to Terraform Example

Terraform Eks
Terraform for AWS EKS
Stars: ✭ 82 (-16.33%)
Mutual labels:  hcl
Terraform Aws Rabbitmq
Terraform configuration for creating RabbitMQ cluster on AWS.
Stars: ✭ 86 (-12.24%)
Mutual labels:  hcl
Hashidays Nyc
Code accompanying my talk at HashiDays New York, 2017
Stars: ✭ 91 (-7.14%)
Mutual labels:  hcl
Terraform Ansible
Working on a webinar series on Terraform and Ansible on AWS
Stars: ✭ 84 (-14.29%)
Mutual labels:  hcl
Terraform Aws Elb
Terraform module which creates ELB resources on AWS
Stars: ✭ 85 (-13.27%)
Mutual labels:  hcl
Terraform Azurerm Aks
Terraform Module for deploying an AKS cluster
Stars: ✭ 87 (-11.22%)
Mutual labels:  hcl
Proxy Benchmarks
Benchmarks for several proxies
Stars: ✭ 79 (-19.39%)
Mutual labels:  hcl
Vaultron
🤖 Vault clusters Terraformed onto Docker for great fun and learning!
Stars: ✭ 96 (-2.04%)
Mutual labels:  hcl
Kubernetes Aws Vpc Kops Terraform
Code for Deploy Kubernetes in an Existing AWS VPC with Kops and Terraform blog post
Stars: ✭ 85 (-13.27%)
Mutual labels:  hcl
Pci Gke Blueprint
PCI on GKE Blueprint: PCI Deployable Architecture on Google Cloud and GKE
Stars: ✭ 91 (-7.14%)
Mutual labels:  hcl
Terraform Aws Ecs Codepipeline
Terraform Module for CI/CD with AWS Code Pipeline and Code Build for ECS https://cloudposse.com/
Stars: ✭ 85 (-13.27%)
Mutual labels:  hcl
Terraform Aws S3 Website
Terraform Module for Creating S3 backed Websites and Route53 DNS
Stars: ✭ 85 (-13.27%)
Mutual labels:  hcl
Clarity
A declaritive test framework for Terraform
Stars: ✭ 88 (-10.2%)
Mutual labels:  hcl
Terraform
Stars: ✭ 84 (-14.29%)
Mutual labels:  hcl
Terraform 0.12 Examples
Some Terraform 0.12 Examples
Stars: ✭ 92 (-6.12%)
Mutual labels:  hcl
Mikado
🤖💨Mikado helps managing your AWS infrastructure for WordPress sites by defining an out-of-box, highly available, easy-to-deploy setup
Stars: ✭ 80 (-18.37%)
Mutual labels:  hcl
Kubernetes Bgp
Kubernetes on Equinix Metal using Calico and MetalLB
Stars: ✭ 87 (-11.22%)
Mutual labels:  hcl
Cobalt
Infrastructure turn-key solution for app service workloads
Stars: ✭ 97 (-1.02%)
Mutual labels:  hcl
Terraform Ecs Jenkins
Provisions Jenkins on AWS ECS using Terraform
Stars: ✭ 93 (-5.1%)
Mutual labels:  hcl
Nomadfiles
A collection of Nomad job files for deploying applications to a cluster
Stars: ✭ 89 (-9.18%)
Mutual labels:  hcl

Terraform Example - http://bit.ly/2eRXQ2q

This code was extracted from Cognician's 3rd-gen AWS infrastructure on Oct 1 2016.

Cognician's codebase is still very much a work in progress :-)

The overall design decisions are:

  • Approachable infrastructure code for the whole tech team. I'm glaring at you, CloudFormation.
  • Use Terraform for provisioning infrastructure.
  • Use Packer to build a single general-purpose base AMI.
  • Use Ansible playbooks for instance configuration.
  • Keep separate environments in separate AWS accounts, with a controlling 'master' account for some goodies. Please follow this excellent article on that: http://cobus.io/aws/2016/09/03/AWS_Multi_Account.html.

Given that it is extracted, it's a mix of the things Cognician needs. In no particular order, those are:

  • A single VPC, with the usual 3 security group setup - internal, web, and bastion.
  • A bastion a.k.a. jump host.
  • A Datomic transactor pair.
  • Elasticache:
  • AWS CodeDeploy.
  • Datadog integration.
  • Several web apps.
  • Several non-web apps.
  • A Zookeeper cluster.
  • Several S3 buckets.

Why am I sharing this?

I wanted to give back. Several folks in the community really helped me get up to speed, either through their writing or through answering many questions. Check them out.

Also, I strongly believe in Terraform and I can clearly see the leverage it produces, and want to make it easy for others to see it and adopt it.

Finally, I'm really hoping some folks are going to tell me how wrong I am doing things, so that I can learn :-)

Disclaimer

I'm sharing this 'as is'. I make no guarantees of maintenance of this code. Use at your own risk.

Just saying!


Workstation Setup

1. AWS

Sets up AWS credentials for aws cli and the rest of the tools.

Install AWS CLI.

brew install awscli

This will ensure that the environments you work with are in your ~/.aws/ files.

Note that the default profile is empty; this is intentional. We'll declare a profile in our terraform files.

~/.aws/config:

[default]
region = us-west-2

[profile cgn-master]
region = us-west-2

[profile cgn-staging]
role_arn = arn:aws:iam::SUB-ACCOUNT-ID:role/SUB-ACCOUNT-ROLE
source_profile = cgn-master
region = us-west-2

~/.aws/credentials:

[default]

[cgn-master]
aws_access_key_id = ...
aws_secret_access_key = ...

Test that AWS is set up by calling aws ec2 describe-instances with a --profile cgn-??? arg, which prints info about the user you're authenticated as.

2. Terraform

Terraform manages AWS infrastructure - IAM users, S3 buckets, EC2 scaling groups, etc.

Install Terraform:

brew install terraform

For each _[environment], go into each one and run make remote:

cd _staging
make remote

This will allow you to download the current Terraform state for that env from S3.

Verify that it's working with make plan:

make plan

You should see Terraform do some work and then declare that there are no differences between what you have and what's running.

3. Packer

Packer builds AMIs (Amazon Machine Images) for our EC2 instances to use.

Install Packer:

brew install packer

4. Ansible

Ansible configures our instances for specific tasks e.g. Zookeeper or one of our apps.

Install Ansible:

brew install python
pip install ansible

Install Ansible Dynamic Inventory for EC2:

mkdir -p /etc/ansible
cp playbooks/inventory/ec2.py /etc/ansible/hosts
cp playbooks/inventory/ec2.ini /etc/ansible/ec2.ini

4. SSH proxying

For staging, put this into ~/.ssh/config:

Host *
  UseRoaming no
  ControlPath ~/.ssh/cm-%[email protected]%h:%p
  ControlMaster auto
  ControlPersist 10m
  ForwardAgent yes
  Port 22

Host b.cgn.fyi
  HostName b.cgn.fyi
  User ubuntu
  IdentityFile ~/.ssh/your-ec2-ssh-key-for-that-env

Host 10.1.*
  ProxyCommand ssh -W %h:%p [email protected]
  User ubuntu
  IdentityFile ~/.ssh/your-ec2-ssh-key-for-that-env

Pack AMIs

AMIs are packed per environment. We may centralise them in the future.

cd _staging
make pack

This will pack _staging/amis/cgn-base.json, and eventually produce a new ami-xxxxxxxx value for you to place in _staging/_staging.tfvars -> *_ami values.


Using Terraform

Terraform uses a two-phase approach:

cd _staging
make remote # you only need to do this once
make plan

This will assess what's live and compare it to your state, and come up with a plan to apply (which it will store in ./proposed.plan.

Assuming the output matches your intentions, apply the plan:

make apply

Once you are returned to the prompt, your changes are live — although some EC2 provisioning may still be in progress.

Generate Ansible vars from Terraform outputs

cd _staging
./gen-ansible-vars.py

This will populate playbooks/group_vars/all.yml with values produced by Terraform output.

List private ips for all running instances for tag

When using the SSH config described above, you can use this to get a list of IPs for an app to SSH into.

cd _staging
make instance-ips

CI build steps for this repo

  1. Ensure terraform is installed.
  2. Ensure terraform.tfstate is present: cd _staging && make remote.
  3. Update all CircleCI apps via API to have the correct AWS creds (from terraform output) with python update-circleci-env.py.
  4. bash upload-ansible-playbooks.sh.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].