diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..b471316 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,28 @@ +# top-most EditorConfig file +root = true + +# Unix-style newlines with a newline ending every file +[*] +end_of_line = lf +insert_final_newline = true + +# Override for Makefile +[{Makefile, makefile, GNUmakefile}] +indent_style = tab +indent_size = 4 + +[Makefile.*] +indent_style = tab +indent_size = 4 + +[shell] +indent_style = tab +indent_size = 4 + +[*.sh] +indent_style = tab +indent_size = 4 + +[*.{tf,tfvars,tpl}] +indent_size = 2 +indent_style = space diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4e7c2d9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,10 @@ +.envrc +.build-harness +build-harness +repos/ +accounts/ +.terraform +*.tfstate +*.tfstate.backup +.idea +*.iml diff --git a/LICENSE b/LICENSE index 261eeb9..c37833f 100644 --- a/LICENSE +++ b/LICENSE @@ -186,7 +186,7 @@ same "printed page" as the copyright notice for easier identification within third-party archives. - Copyright [yyyy] [name of copyright owner] + Copyright 2018 Cloud Posse, LLC Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..5a3078f --- /dev/null +++ b/Makefile @@ -0,0 +1,30 @@ +# Import the cloudposse/build-harness +include $(shell curl -sSL -o .build-harness "https://git.io/build-harness"; echo .build-harness) +include tasks/Makefile.* + +# Run time environment passed to terraform +export TF_VAR_artifacts_dir ?= $(CURDIR)/artifacts +export TF_VAR_repos_dir ?= $(CURDIR)/repos +export TF_VAR_templates_dir ?= $(CURDIR)/templates + +# The target called when calling `make` with no arguments +export DEFAULT_HELP_TARGET = help/short + +# The command we'll use to start the container +export DOCKER_RUN = docker run --rm -it -e AWS_ACCESS_KEY_ID -e AWS_SECRET_ACCESS_KEY -e SSH_KEY=false \ + -v $(CURDIR)/artifacts:/artifacts -v $(CURDIR)/scripts:/scripts + +# The directory containing configs +export CONFIGS ?= configs + +## Clean up +clean:: + rm -rf repos accounts .terraform *.tfstate* artifacts/* + +## Format all terraform code +fmt: + find $(CONFIGS) -type f -name '*.tfvars' -exec terraform fmt {} \; + terraform fmt modules + +finalize: root/finalize children/finalize + @exit 0 diff --git a/README.md b/README.md index c7c1660..b032126 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,184 @@ # reference-architectures -Get up and running quickly with one of our reference architecture using our cold start process. + +Get up and running quickly with one of our reference architectures using our fully automated cold-start process. + +**NOTE:** This project is under active development and subject to change. Please [file issues](https://github.com/cloudposse/reference-architectures/issues/new) for all bugs encountered. + +## Table of Contents + +- [reference-architectures](#reference-architectures) + - [Table of Contents](#table-of-contents) + - [Known Limitations](#known-limitations) + - [High Level Overview](#high-level-overview) + - [Architecture](#architecture) + - [Assumptions](#assumptions) + - [Checklist](#checklist) + - [1. Provision Root Account](#1-provision-root-account) + - [2. Provision Subaccounts](#2-provision-subaccounts) + - [3. Delegate DNS](#3-delegate-dns) + - [Next Steps](#next-steps) + - [Getting Help](#getting-help) + +## Known Limitations + +- **AWS does not support programmatic deletion of accounts.** This means that if you use this project to create the account structure, terraform is not able to completely destroy it. +- **AWS by default only permits one subaccount.** This limit can be easily increased for your organization, but can take up to several days. +- **AWS will rate limit account creation.** This might mean you'll need to restart the provisioning (just re-run `make root`). + +## High Level Overview + +You can provision the basic reference architecture in 3 "easy" steps. =) + +All accounts will leverage our [`terraform-root-modules`](https://github.com/cloudposse/terraform-root-modules/) service catalog to get you started. Later, we recommend you fork this and start your own service catalog suitable for your organization. + +This process involves using `terraform` to generate the code (`Dockerfile`, `Makefile`, `terraform.tfvar`, etc) that you will use to manage your infrastructure. + +This is a "bootstrap" process that gets you running from a cold-start. You do it once and then you literally throw _this_ (e.g. `reference-architectures/`) repo away. This project generates all the pre-configured boilerplate scaffolding you need in the `repos/` directory. + +When you're done, in the `repos/` directory you'll have one Git repo for each AWS account. These repos are what you'll want to push up to GitHub. Each repo contains everything necessary to administer that account. We practice a strict "share nothing" approach, which is why each account gets it's own repo, terraform state backend, and DNS zone. This maximally reduces the blast radius of any human errors in one account affecting any other account. Also, because each account has it's own repo, it's _ideally_ suited for larger enterprise or corporate environments where various stakeholders will be responsible for running services in their account. + +See the [Next Steps](#next-steps) section for where to go after this process completes. + +## Architecture + +Our "reference architecture" is an opinionated approach to architecting accounts for AWS. + +This process provisions (7) accounts which have different designations. + +Here is what it includes. Enable the accounts you want. + +| Account | Description | +| -------- | ----------------------------------------------------------------------------------------- | +| root | The "root" (parent, billing) account creates all child accounts and is where users login. | +| prod | The "production" is account where you run your most mission critical applications | +| staging | The "staging" account is where you run all of your QA/UAT/Testing | +| dev | The "dev" sandbox account is where you let your developers have fun and break things | +| audit | The "audit" account is where all logs end up | +| corp | The "corp" account is where you run the shared platform services for the company | +| data | The "data" account is where the quants live =) | +| testing | The "testing" account is where to run automated tests of untrusted infrastructure code | +| security | The "security" account is where to run automated security scanning software | +| identity | The "identity" account is where to add users and delegate access to the other accounts | + +Each account has its own [terraform state backend](https://github.com/cloudposse/terraform-aws-tfstate-backend), along with a [dedicated DNS zone](https://www.terraform.io/docs/providers/aws/r/route53_zone.html) for service discovery. + +The root account owns the top-level DNS zone and then delegates NS authority to each child account. + +### Assumptions + +1. We are starting with a clean AWS environment and a new "root" (top-level) AWS account. This means you need the "root" credentials, since a fresh AWS account doesn't even have any AWS roles that can be assumed. +2. You have administrator access to this account. +3. You have [docker](https://docs.cloudposse.com/tools/docker/) installed on your workstation. +4. You have [terraform](https://www.terraform.io/downloads.html) installed on your workstation. + +### Checklist + +Before we get started, make sure you have the following + +- [ ] Before you can create new AWS accounts under your organization, you must [verify your email address](https://docs.aws.amazon.com/console/organizations/email-verification). +- [ ] Open a support ticket to [request the limit](https://console.aws.amazon.com/support/v1#/case/create) of AWS accounts be increased for your organization (the default is 1). +- [ ] Clone this repo on your workstation. +- [ ] Create a _temporary_ pair of [Access Keys](https://console.aws.amazon.com/iam/home#/security_credential). These should be deleted afterwards. +- [ ] Export your AWS "root" account credentials as `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` (this is _temporary_ for bootstrapping). +- [ ] An available domain we can use for DNS-base service discovery (E.g. `ourcompany.co`). This domain must not be in use elsewhere as the root account will need to be the authoritative name server (`SOA`). +- [ ] Ensure that any users who will be added during this bootstrap process have setup their [keybase](http://keybase.io) profile. You'll need this if setting them up in the `users` section of the [`config/root.tfvars`](https://github.com/cloudposse/reference-architectures/blob/master/configs/root.tfvars). For example you should be able to [verify their public key](https://keybase.io/osterman/key.asc) on `keybase.io` by running `curl https://keybase.io/$username/key.asc`. + +### 1. Provision Root Account + +The "root" account is the top-most AWS account from which all other AWS accounts are programmatically created. + +**WARNING:** Terraform cannot remove an AWS account from an organization. Terraform **cannot** close the account. The child account must be prepared to be a standalone account beforehand. To do this, issue a password reset using the child account's email address. Login and accept the prompts. Then you should be good to go. See the [AWS Organizations documentation](https://docs.aws.amazon.com/organizations/latest/userguide/orgs_manage_accounts_remove.html) for more information. + +**WARNING:** Do not chain the `make` targets together (e.g. `make root children finalize`) as it is not currently supported. + +This account is provisioned slightly different from the other subaccounts. + +Update the configuration for this account by editing the `configs/root.tfvar` file. + +Then to get started, run: + +```bash +make root +``` + +**NOTE:** We need to know each account's `AWS_ACCOUNT_ID` for Step 2. +**NOTE:** Sometimes provisioning of the `account` module fails due to rate limiting by AWS on creating subaccounts. If this happens, just run `make root/provision` to retry. If that works, just continue on to step 2, once it completes. + +
+ Here's what that roughly looks like (but entirely automated). + +1. Create a new account git repo. +2. Render templates into the repo (including `Dockerfile`). +3. Build a docker image. +4. Run the docker image and start provisioning resources including the Terraform state backend and child accounts. +5. Create the IAM groups to permit access to child accounts. +6. Write a list of child account IDs so we can use them in the next phase. + +
+ +### 2. Provision Subaccounts + +Subaccounts are created by the root account, but are ultimately provisioned using the subaccount containers. + +Update the configuration for all the child accounts by editing the `configs/root.tfvar` file. + +To get started, run: + +```bash +make children +``` + +
+ +Here's what that roughly looks like (but entirely automated). + +For each child account: + +1. Create a new account git repo. +2. Render the templates for a `child` account into the repo directory (include `Dockerfile`). Obtain the account ID from the previous phase. +3. Build a docker image. +4. Run the docker image and start provisioning the child account's Terraform state bucket, DNS zone, cloudtrail logs, etc. + +
+ +### 3. Delegate DNS + +Now that each subaccount has been provisioned, we can delegate each DNS zone to those accounts. + +To finish up, run: + +```bash +make finalize +``` + +
+Here's what that roughly looks like (but entirely automated). + +1. Re-use the docker images from phase (1) and phase (2). +2. Update DNS so that root account delegates DNS zones to the child accounts. +3. Enable cloudtrail log forwarding to audit account. + +
+ +--- + +## Next Steps + +At this point, you have everything you need to start terraforming your way to success. + +All of your account configurations are currently in `repos/` + +- [ ] Commit the changes in `repos/`. Open Pull Requests. +- [ ] Ensure that the name servers for the service discovery domain (e.g. `ourcompany.co`) have been configured with your domain registrar (e.g. GoDaddy). +- [ ] Delete your root account credentials. They are no longer needed and should not be used. Instead use the created IAM users. +- [ ] Request limits for EC2 instances to be raised in each account corresponding to the region you will be operating in. +- [ ] Set the child account's credentials. To do this, issue a password reset using the child account's email address. Login and accept the prompts. Setup MFA. +- [ ] Ensure you have MFA setup on your root account. +- [ ] Consider adding some other capabilities from our service catalog. +- [ ] Create your own [`terraform-root-modules`](https://github.com/cloudposse/terraform-root-modules) service catalog for your organization. + +**NOTE:** _This_ repo can be deleted once you're all done and pushed your changes in the `repos/` directory to GitHub. The rest of your development should happen inside your infrastructure repos. + +## Getting Help + +Did you get stuck? Find us on [slack](https://sweetops.cloudposse.com) in the `#geodesic` channel. diff --git a/configs/README.md b/configs/README.md new file mode 100644 index 0000000..975434b --- /dev/null +++ b/configs/README.md @@ -0,0 +1 @@ +Update these configurations to suit your environment diff --git a/configs/audit.tfvars b/configs/audit.tfvars new file mode 100644 index 0000000..ed39e19 --- /dev/null +++ b/configs/audit.tfvars @@ -0,0 +1,20 @@ +# This is a terraform configuration file + +stage = "audit" + +# List of templates to install +templates = [ + "Dockerfile.child", + ".gitignore", + ".dockerignore", + "Makefile", + "conf/Makefile" + ] + +# List of terraform root modules to enable +terraform_root_modules = [ + "aws/tfstate-backend", + "aws/account-dns", + "aws/chamber", + "aws/audit-cloudtrail" +] diff --git a/configs/corp.tfvars b/configs/corp.tfvars new file mode 100644 index 0000000..ae389f7 --- /dev/null +++ b/configs/corp.tfvars @@ -0,0 +1,21 @@ +# This is a terraform configuration file + +stage = "corp" + +# List of templates to install +templates = [ + "Dockerfile.child", + ".gitignore", + ".dockerignore", + "Makefile", + "conf/Makefile", + "conf/account-dns/terraform.tfvars" +] + +# List of terraform root modules to enable +terraform_root_modules = [ + "aws/tfstate-backend", + "aws/account-dns", + "aws/chamber", + "aws/cloudtrail" +] diff --git a/configs/data.tfvars b/configs/data.tfvars new file mode 100644 index 0000000..89d881d --- /dev/null +++ b/configs/data.tfvars @@ -0,0 +1,21 @@ +# This is a terraform configuration file + +stage = "data" + +# List of templates to install +templates = [ + "Dockerfile.child", + ".gitignore", + ".dockerignore", + "Makefile", + "conf/Makefile", + "conf/account-dns/terraform.tfvars" +] + +# List of terraform root modules to enable +terraform_root_modules = [ + "aws/tfstate-backend", + "aws/account-dns", + "aws/chamber", + "aws/cloudtrail" +] diff --git a/configs/dev.tfvars b/configs/dev.tfvars new file mode 100644 index 0000000..8f3e976 --- /dev/null +++ b/configs/dev.tfvars @@ -0,0 +1,21 @@ +# This is a terraform configuration file + +stage = "dev" + +# List of templates to install +templates = [ + "Dockerfile.child", + ".gitignore", + ".dockerignore", + "Makefile", + "conf/Makefile", + "conf/account-dns/terraform.tfvars" +] + +# List of terraform root modules to enable +terraform_root_modules = [ + "aws/tfstate-backend", + "aws/account-dns", + "aws/chamber", + "aws/cloudtrail" +] diff --git a/configs/prod.tfvars b/configs/prod.tfvars new file mode 100644 index 0000000..786b8be --- /dev/null +++ b/configs/prod.tfvars @@ -0,0 +1,21 @@ +# This is a terraform configuration file + +stage = "prod" + +# List of templates to install +templates = [ + "Dockerfile.child", + ".gitignore", + ".dockerignore", + "Makefile", + "conf/Makefile", + "conf/account-dns/terraform.tfvars" +] + +# List of terraform root modules to enable +terraform_root_modules = [ + "aws/tfstate-backend", + "aws/account-dns", + "aws/chamber", + "aws/cloudtrail" +] diff --git a/configs/root.tfvars b/configs/root.tfvars new file mode 100644 index 0000000..c130025 --- /dev/null +++ b/configs/root.tfvars @@ -0,0 +1,69 @@ +# This is a terraform configuration file + +# The "apex" service discovery domain for *all* infrastructure +domain = "test.co" + +# The global namespace that should be shared by all accounts +namespace = "test" + +# The default region for this account +aws_region = "us-west-2" + +# The docker registry that will be used for the images built (nothing will get pushed) +docker_registry = "cloudposse" + +# The templates to use for this account +templates = [ + "Dockerfile.root", + ".gitignore", + ".dockerignore", + "Makefile", + "conf/Makefile", + "conf/accounts/terraform.tfvars", + "conf/bootstrap/terraform.tfvars", + "conf/iam/terraform.tfvars", + "conf/root-dns/terraform.tfvars", +] + +# Account email address format (e.g. `ops+%s@example.co`). This is not easily changed later. +account_email = "ops+%s@test.co" + +# List of accounts to enable +accounts_enabled = [ + "dev", + "staging", + "prod", + "testing", + "data", + "corp", + "audit" +] + +# Administrator IAM usernames mapped to their keybase usernames for password encryption +users = { + "erik@cloudposse.com" = "osterman" +} + +# Terraform Root Modules Image (don't change this unless you know what you're doing) +# Project: https://github.com/cloudposse/terraform-root-modules +terraform_root_modules_image = "cloudposse/terraform-root-modules:0.14.3" + +# Geodesic Base Image (don't change this unless you know what you're doing) +# Project: https://github.com/cloudposse/geodesic +geodesic_base_image = "cloudposse/geodesic:0.49.0" + +# List of terraform root modules to enable +terraform_root_modules = [ + "aws/tfstate-backend", + "aws/accounts", + "aws/account-settings", + "aws/bootstrap", + "aws/root-dns", + "aws/root-iam", + "aws/iam", + "aws/users", + "aws/cloudtrail" +] + +# Message of the Day +motd_url = "https://geodesic.sh/motd" diff --git a/configs/staging.tfvars b/configs/staging.tfvars new file mode 100644 index 0000000..a6970da --- /dev/null +++ b/configs/staging.tfvars @@ -0,0 +1,21 @@ +# This is a terraform configuration file + +stage = "staging" + +# List of templates to install +templates = [ + "Dockerfile.child", + ".gitignore", + ".dockerignore", + "Makefile", + "conf/Makefile", + "conf/account-dns/terraform.tfvars" +] + +# List of terraform root modules to enable +terraform_root_modules = [ + "aws/tfstate-backend", + "aws/account-dns", + "aws/chamber", + "aws/cloudtrail" +] diff --git a/configs/testing.tfvars b/configs/testing.tfvars new file mode 100644 index 0000000..b515a82 --- /dev/null +++ b/configs/testing.tfvars @@ -0,0 +1,21 @@ +# This is a terraform configuration file + +stage = "testing" + +# List of templates to install +templates = [ + "Dockerfile.child", + ".gitignore", + ".dockerignore", + "Makefile", + "conf/Makefile", + "conf/account-dns/terraform.tfvars" +] + +# List of terraform root modules to enable +terraform_root_modules = [ + "aws/tfstate-backend", + "aws/account-dns", + "aws/chamber", + "aws/cloudtrail" +] diff --git a/modules/README.md b/modules/README.md new file mode 100644 index 0000000..7a88db9 --- /dev/null +++ b/modules/README.md @@ -0,0 +1 @@ +This directory contains modules used to create all the scaffolding. It's basically a code generator which creates directories, `Dockerfiles` and builds docker images. diff --git a/modules/account/main.tf b/modules/account/main.tf new file mode 100644 index 0000000..f6a29a8 --- /dev/null +++ b/modules/account/main.tf @@ -0,0 +1,65 @@ +data "null_data_source" "terraform_root_modules" { + count = "${length(var.terraform_root_modules)}" + + inputs = { + module_name = "${basename(element(var.terraform_root_modules, count.index))}" + copy_from = "COPY --from=terraform-root-modules /${element(var.terraform_root_modules, count.index)}/ /conf/${basename(element(var.terraform_root_modules, count.index))}/" + } +} + +locals { + domain_name = "${var.stage}.${var.domain}" + image_name = "${local.domain_name}" + repo_dir = "${var.repos_dir}/${local.image_name}" + + context = { + aws_account_id = "${var.aws_account_id}" + aws_root_account_id = "${var.aws_root_account_id}" + aws_region = "${var.aws_region}" + docker_registry = "${var.docker_registry}" + domain_name = "${local.domain_name}" + image_name = "${local.image_name}" + image_tag = "${var.image_tag}" + motd_url = "${var.motd_url}" + namespace = "${var.namespace}" + stage = "${var.stage}" + parent_domain_name = "${var.domain}" + geodesic_base_image = "${var.geodesic_base_image}" + terraform_root_modules_image = "${var.terraform_root_modules_image}" + terraform_root_modules = "${join("\n", data.null_data_source.terraform_root_modules.*.outputs.copy_from)}" + } + + vars = "${merge(var.vars, local.context)}" +} + +# Write an env file for this stage that we can use from shell scripts +resource "local_file" "artifacts" { + content = "export TERRAFORM_ROOT_MODULES=\"${join(" ", data.null_data_source.terraform_root_modules.*.outputs.module_name)}\"\n" + filename = "${var.artifacts_dir}/${var.stage}.env" +} + +module "init_dirs" { + source = "../../modules/init-dirs" + working_dir = "${local.repo_dir}" + dirs = "${var.dirs}" +} + +module "render" { + source = "../../modules/render" + output_dir = "${local.repo_dir}" + templates_dir = "${var.templates_dir}" + templates = "${var.templates}" + strip = "${var.strip}" + vars = "${local.vars}" + depends_on = ["${module.init_dirs.completed}"] +} + +module "docker_build" { + source = "../../modules/docker-build" + working_dir = "${local.repo_dir}" + short_name = "${var.stage}" + image_name = "${local.image_name}" + image_tag = "${var.image_tag}" + docker_registry = "${var.docker_registry}" + depends_on = ["${module.render.completed}", "${module.init_dirs.completed}"] +} diff --git a/modules/account/outputs.tf b/modules/account/outputs.tf new file mode 100644 index 0000000..52bdd4c --- /dev/null +++ b/modules/account/outputs.tf @@ -0,0 +1,7 @@ +output "docker_image" { + value = "${var.docker_registry}/${local.image_name}:${var.image_tag}" +} + +output "repo_dir" { + value = "${local.repo_dir}" +} diff --git a/modules/account/variables.tf b/modules/account/variables.tf new file mode 100644 index 0000000..1f427ff --- /dev/null +++ b/modules/account/variables.tf @@ -0,0 +1,51 @@ +variable "aws_account_id" {} + +variable "aws_root_account_id" {} + +variable "aws_region" {} + +variable "namespace" {} + +variable "stage" {} + +variable "domain" {} + +variable "motd_url" {} + +variable "image_tag" { + default = "latest" +} + +variable "templates" { + type = "list" +} + +variable "dirs" { + type = "list" + default = ["", "conf", "rootfs"] +} + +variable "vars" { + type = "map" + default = {} +} + +variable "strip" { + default = "" +} + +variable "artifacts_dir" {} + +variable "repos_dir" {} + +variable "templates_dir" {} + +variable "docker_registry" {} + +variable "geodesic_base_image" {} + +variable "terraform_root_modules" { + type = "list" +} + +variable "terraform_root_modules_image" {} diff --git a/modules/add-users/main.tf b/modules/add-users/main.tf new file mode 100644 index 0000000..3e887e4 --- /dev/null +++ b/modules/add-users/main.tf @@ -0,0 +1,35 @@ +locals { + unsafe_characters = "/[^A-Za-z0-9]+/" +} + +data "template_file" "data" { + count = "${length(keys(var.users))}" + + # this path is relative to repos/$image_name + template = "${file("${var.templates_dir}/conf/users/user.tf")}" + + vars = { + resource_name = "${replace(element(keys(var.users), count.index), local.unsafe_characters, "_")}" + username = "${element(keys(var.users), count.index)}" + keybase_username = "${element(values(var.users), count.index)}" + } +} + +resource "local_file" "data" { + count = "${length(keys(var.users))}" + content = "${element(data.template_file.data.*.rendered, count.index)}" + filename = "${var.output_dir}/${replace(element(keys(var.users), count.index), local.unsafe_characters, "_")}.tf" +} + +# https://github.com/terraform-providers/terraform-provider-local/issues/19 +resource "null_resource" "chmod" { + count = "${signum(length(keys(var.users)))}" + + triggers { + files = "${join(" ", local_file.data.*.filename)}" + } + + provisioner "local-exec" { + command = "chmod 644 ${null_resource.chmod.triggers.files}" + } +} diff --git a/modules/add-users/variables.tf b/modules/add-users/variables.tf new file mode 100644 index 0000000..427842e --- /dev/null +++ b/modules/add-users/variables.tf @@ -0,0 +1,9 @@ +variable "output_dir" {} + +variable "templates_dir" {} + +variable "users" { + type = "map" + description = "A map of AWS IAM usernames to create mapped to their keybase username (e.g. { 'user@example.com' => 'keybase_user_name' })" + default = {} +} diff --git a/modules/child/main.tf b/modules/child/main.tf new file mode 100644 index 0000000..df479c8 --- /dev/null +++ b/modules/child/main.tf @@ -0,0 +1,23 @@ +module "account" { + source = "../../modules/account" + dirs = "${var.dirs}" + aws_account_id = "${var.aws_account_ids[var.stage]}" + aws_root_account_id = "${var.aws_root_account_id}" + aws_region = "${var.aws_region}" + namespace = "${var.namespace}" + stage = "${var.stage}" + domain = "${var.domain}" + motd_url = "${var.motd_url}" + image_tag = "${var.image_tag}" + templates = "${var.templates}" + dirs = "${var.dirs}" + vars = "${var.vars}" + strip = "${var.strip}" + artifacts_dir = "${var.artifacts_dir}" + repos_dir = "${var.repos_dir}" + templates_dir = "${var.templates_dir}" + docker_registry = "${var.docker_registry}" + geodesic_base_image = "${var.geodesic_base_image}" + terraform_root_modules_image = "${var.terraform_root_modules_image}" + terraform_root_modules = "${var.terraform_root_modules}" +} diff --git a/modules/child/outputs.tf b/modules/child/outputs.tf new file mode 100644 index 0000000..c3cd924 --- /dev/null +++ b/modules/child/outputs.tf @@ -0,0 +1,3 @@ +output "docker_image" { + value = "${module.account.docker_image}" +} diff --git a/modules/child/variables.tf b/modules/child/variables.tf new file mode 100644 index 0000000..3606f03 --- /dev/null +++ b/modules/child/variables.tf @@ -0,0 +1,53 @@ +variable "aws_account_ids" { + type = "map" +} + +variable "aws_root_account_id" {} + +variable "aws_region" {} + +variable "namespace" {} + +variable "stage" {} + +variable "domain" {} + +variable "motd_url" {} + +variable "image_tag" { + default = "latest" +} + +variable "templates" { + type = "list" +} + +variable "dirs" { + type = "list" + default = ["", "conf", "rootfs"] +} + +variable "vars" { + type = "map" + default = {} +} + +variable "strip" { + default = "/\\.(child)$/" +} + +variable "artifacts_dir" {} + +variable "repos_dir" {} + +variable "templates_dir" {} + +variable "docker_registry" {} + +variable "geodesic_base_image" {} + +variable "terraform_root_modules" { + type = "list" +} + +variable "terraform_root_modules_image" {} diff --git a/modules/docker-build/main.tf b/modules/docker-build/main.tf new file mode 100644 index 0000000..28eae3d --- /dev/null +++ b/modules/docker-build/main.tf @@ -0,0 +1,26 @@ +resource "null_resource" "docker_build" { + provisioner "local-exec" { + command = "docker build -t ${var.image_name} -f ${var.dockerfile} ." + working_dir = "${var.working_dir}" + } +} + +resource "null_resource" "docker_tag" { + provisioner "local-exec" { + command = "docker tag ${var.image_name} ${var.docker_registry}/${var.image_name}:${var.image_tag}" + working_dir = "${var.working_dir}" + } + + depends_on = ["null_resource.docker_build"] +} + +resource "null_resource" "docker_tag_short_name" { + count = "${signum(length(var.short_name))}" + + provisioner "local-exec" { + command = "docker tag ${var.image_name} ${var.short_name}" + working_dir = "${var.working_dir}" + } + + depends_on = ["null_resource.docker_build"] +} diff --git a/modules/docker-build/variables.tf b/modules/docker-build/variables.tf new file mode 100644 index 0000000..10e96f8 --- /dev/null +++ b/modules/docker-build/variables.tf @@ -0,0 +1,21 @@ +variable "working_dir" {} + +variable "image_name" {} + +variable "short_name" {} + +variable "image_tag" {} + +variable "docker_registry" {} + +variable "dockerfile" { + default = "Dockerfile" +} + +# NOTE: this variable won't actually be used for anything and the actual `depends_on` keyword +# in terraform does not support interpolation. +variable "depends_on" { + type = "list" + description = "Define a list of variables that this module depends on in order to force serialized execution." + default = [] +} diff --git a/modules/init-dirs/main.tf b/modules/init-dirs/main.tf new file mode 100644 index 0000000..da04210 --- /dev/null +++ b/modules/init-dirs/main.tf @@ -0,0 +1,22 @@ +resource "null_resource" "mkdir" { + triggers { + dirs = "${join(" ", formatlist("${var.working_dir}/%s", sort(compact(var.dirs))))}" + } + + provisioner "local-exec" { + command = "mkdir -p ${null_resource.mkdir.triggers.dirs}" + } +} + +# Touch `.gitignore` in each directory to ensure directories persist with git +resource "null_resource" "touch" { + triggers { + files = "${join(" ", formatlist("${var.working_dir}/%s/.gitignore", sort(compact(var.dirs))))}" + } + + provisioner "local-exec" { + command = "touch ${null_resource.touch.triggers.files}" + } + + depends_on = ["null_resource.mkdir"] +} diff --git a/modules/init-dirs/outputs.tf b/modules/init-dirs/outputs.tf new file mode 100644 index 0000000..7327b27 --- /dev/null +++ b/modules/init-dirs/outputs.tf @@ -0,0 +1,3 @@ +output "completed" { + value = "true" +} diff --git a/modules/init-dirs/variables.tf b/modules/init-dirs/variables.tf new file mode 100644 index 0000000..5fbbe19 --- /dev/null +++ b/modules/init-dirs/variables.tf @@ -0,0 +1,14 @@ +variable "working_dir" {} + +variable "dirs" { + type = "list" + default = ["", "conf", "rootfs"] +} + +# NOTE: this variable won't actually be used for anything and the actual `depends_on` keyword +# in terraform does not support interpolation. +variable "depends_on" { + type = "list" + description = "Define a list of variables that this module depends on in order to force serialized execution." + default = [] +} diff --git a/modules/render/main.tf b/modules/render/main.tf new file mode 100644 index 0000000..cec77c5 --- /dev/null +++ b/modules/render/main.tf @@ -0,0 +1,24 @@ +data "template_file" "data" { + count = "${length(var.templates)}" + + # this path is relative to repos/$image_name + template = "${file("${var.templates_dir}/${element(var.templates, count.index)}")}" + vars = "${var.vars}" +} + +resource "local_file" "data" { + count = "${length(var.templates)}" + content = "${element(data.template_file.data.*.rendered, count.index)}" + filename = "${var.output_dir}/${replace(element(var.templates, count.index), var.strip, "")}" +} + +# https://github.com/terraform-providers/terraform-provider-local/issues/19 +resource "null_resource" "chmod" { + triggers { + files = "${join(" ", local_file.data.*.filename)}" + } + + provisioner "local-exec" { + command = "chmod 644 ${null_resource.chmod.triggers.files}" + } +} diff --git a/modules/render/outputs.tf b/modules/render/outputs.tf new file mode 100644 index 0000000..7327b27 --- /dev/null +++ b/modules/render/outputs.tf @@ -0,0 +1,3 @@ +output "completed" { + value = "true" +} diff --git a/modules/render/variables.tf b/modules/render/variables.tf new file mode 100644 index 0000000..6a5fb6e --- /dev/null +++ b/modules/render/variables.tf @@ -0,0 +1,23 @@ +variable "vars" { + type = "map" +} + +variable "output_dir" {} + +variable "templates_dir" {} + +variable "templates" { + type = "list" +} + +variable "strip" { + default = "" +} + +# NOTE: this variable won't actually be used for anything and the actual `depends_on` keyword +# in terraform does not support interpolation. +variable "depends_on" { + type = "list" + description = "Define a list of variables that this module depends on in order to force serialized execution." + default = [] +} diff --git a/modules/root/main.tf b/modules/root/main.tf new file mode 100644 index 0000000..ca782d5 --- /dev/null +++ b/modules/root/main.tf @@ -0,0 +1,53 @@ +locals { + context = { + # Used by `accounts` + accounts_enabled = "${jsonencode(var.accounts_enabled)}" + account_email = "${var.account_email}" + + # Used by `root-dns` + root_domain_name = "${var.stage}.${var.domain}" + } + + vars = "${merge(var.vars, local.context)}" +} + +module "account" { + source = "../../modules/account/" + + dirs = "${var.dirs}" + + # For the "root" account these should always match + aws_account_id = "${var.aws_root_account_id}" + aws_root_account_id = "${var.aws_root_account_id}" + + aws_region = "${var.aws_region}" + namespace = "${var.namespace}" + stage = "${var.stage}" + domain = "${var.domain}" + motd_url = "${var.motd_url}" + image_tag = "${var.image_tag}" + templates = "${var.templates}" + dirs = "${var.dirs}" + vars = "${local.vars}" + strip = "${var.strip}" + artifacts_dir = "${var.artifacts_dir}" + repos_dir = "${var.repos_dir}" + templates_dir = "${var.templates_dir}" + docker_registry = "${var.docker_registry}" + geodesic_base_image = "${var.geodesic_base_image}" + terraform_root_modules_image = "${var.terraform_root_modules_image}" + terraform_root_modules = "${var.terraform_root_modules}" +} + +module "add_users" { + source = "../../modules/add-users/" + users = "${var.users}" + templates_dir = "${var.templates_dir}" + output_dir = "${module.account.repo_dir}/conf/users" +} + +# Write an env file that we can use from other Makefiles +resource "local_file" "makefile_env" { + content = "ACCOUNTS_ENABLED = ${join(" ", var.accounts_enabled)}\n" + filename = "${var.artifacts_dir}/Makefile.env" +} diff --git a/modules/root/outputs.tf b/modules/root/outputs.tf new file mode 100644 index 0000000..c3cd924 --- /dev/null +++ b/modules/root/outputs.tf @@ -0,0 +1,3 @@ +output "docker_image" { + value = "${module.account.docker_image}" +} diff --git a/modules/root/variables.tf b/modules/root/variables.tf new file mode 100644 index 0000000..b49dc1e --- /dev/null +++ b/modules/root/variables.tf @@ -0,0 +1,62 @@ +variable "aws_root_account_id" {} + +variable "aws_region" {} + +variable "namespace" {} + +variable "stage" { + default = "root" +} + +variable "domain" {} + +variable "motd_url" {} + +variable "image_tag" { + default = "latest" +} + +variable "account_email" {} + +variable "accounts_enabled" { + type = "list" +} + +variable "templates" { + type = "list" +} + +variable "dirs" { + type = "list" + default = ["", "conf", "rootfs"] +} + +variable "vars" { + type = "map" + default = {} +} + +variable "users" { + type = "map" + default = {} +} + +variable "strip" { + default = "/\\.(root)$/" +} + +variable "artifacts_dir" {} + +variable "repos_dir" {} + +variable "templates_dir" {} + +variable "docker_registry" {} + +variable "geodesic_base_image" {} + +variable "terraform_root_modules" { + type = "list" +} + +variable "terraform_root_modules_image" {} diff --git a/scripts/get-root-account-id.sh b/scripts/get-root-account-id.sh new file mode 100755 index 0000000..c476493 --- /dev/null +++ b/scripts/get-root-account-id.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +export AWS_ROOT_ACCOUNT_ID=$(aws sts get-caller-identity --output text --query 'Account') + +if [[ -z "${AWS_ROOT_ACCOUNT_ID}" ]]; then + echo "Unable to obtain root account id" + exit 1 +fi + +# Export the "root AWS Account ID so we can use it with the rest of the bootstrapping process +printf 'aws_root_account_id = "%s"' $AWS_ROOT_ACCOUNT_ID > /artifacts/aws.tfvars diff --git a/scripts/lib.sh b/scripts/lib.sh new file mode 100755 index 0000000..d48dd42 --- /dev/null +++ b/scripts/lib.sh @@ -0,0 +1,129 @@ +[ "${GEODESIC_SHELL}" == "true" ] || (echo "This script is intended to be run inside the account container. "; exit 1) + +export CONF="${CONF:-/conf}" + +# Import env for this stage +function import_env() { + # Load the environment for this stage, if they exist + echo "Loading /artifacts/${STAGE}.env" + source /artifacts/${STAGE}.env + + # Export our environment to TF_VARs + eval $(tfenv sh -c "export -p") +} + +function disable_profile() { + # Don't use a role to simplify provisioning of root account + export TF_VAR_aws_assume_role_arn="" + unset AWS_DEFAULT_PROFILE + unset AWS_PROFILE +} + +# Easily assume role using the "bootstrap" user +function assume_role() { + echo "Attempting to assume role to ${AWS_DEFAULT_PROFILE}..." + + # Load the environment exported by the `bootstrap` module + source /artifacts/.envrc + + # Install the helper cli for assuming roles as part of the bootstrapping process + [ -x /usr/bin/assume-role ] || apk add assume-role@cloudposse + + # This is because the [`assume-role`](https://github.com/remind101/assume-role) cli does not respect the SDK environment variables. + export HOME="/artifacts" + export AWS_CONFIG_FILE="${HOME}/.aws/config" + + # Unset AWS credential environment variables so they don't interfere with `assume-role` + unset AWS_ACCESS_KEY_ID + unset AWS_SECRET_ACCES_KEY + + # Fetch the Role ARN from the configuration + export TF_VAR_aws_assume_role_arn=$(crudini --get ${AWS_CONFIG_FILE} "profile ${AWS_DEFAULT_PROFILE}" role_arn) + + if [ -z "${TF_VAR_aws_assume_role_arn}" ]; then + abort "TF_VAR_aws_assume_role_arn must be set" + fi + + # Obtain an assume-role session + eval $(/usr/bin/assume-role $AWS_DEFAULT_PROFILE) + if [ $? -ne 0 ]; then + echo "Failed to assume role of ${AWS_DEFAULT_PROFILE}" + exit 1 + fi +} + +# Export map of accounts +function export_accounts() { + # Export account ids (for use with provisioning children) + cd /conf/accounts + make init + ( + echo "aws_account_ids = {" + terraform output -json | jq -r 'to_entries | .[] | .key + " = \"" + .value.value + "\""' | grep account_id | sed 's/_account_id//' + echo "}" + ) | terraform fmt - > /artifacts/accounts.tfvars +} + +function abort() { + echo -e "\n\n" + echo "===============================================================================================" + echo "$1" + echo + echo "* Please report this error here:" + echo " https://github.com/cloudposse/reference-architectures/issues/new" + echo -e "\n\n" + exit 1 +} + +# Provision modules +function apply_modules() { + # Provision modules which *do not* have dependencies on other accounts (that will be a later phase) + for module in ${TERRAFORM_ROOT_MODULES}; do + if [[ -n "${SKIP_MODULES}" ]] && [[ "${module}" =~ ${SKIP_MODULES} ]]; then + echo "Skipping ${module}..." + else + echo "Processing $module..." + make -C "/conf/${module}" init plan apply + if [ $? -ne 0 ]; then + abort "The ${module} module errored. Aborting." + fi + fi + done +} + +function parse_args() { + while [[ $1 ]]; do + echo "Handling [$1]..." + case "$1" in + -a | --assume-role) + assume_role + shift + ;; + -d | --disable-profile) + disable_profile + shift + ;; + -m | --apply-modules) + apply_modules + shift + ;; + -i | --import-env) + import_env + shift + ;; + -e | --export-accounts) + export_accounts + shift + ;; + *) + echo "Error: Unknown option: $1" >&2 + exit 1 + ;; + esac + done +} + +function ctrl_c() { + echo "* Okay, aborting..." + exit 1 +} diff --git a/scripts/provision.sh b/scripts/provision.sh new file mode 100755 index 0000000..5059ede --- /dev/null +++ b/scripts/provision.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +source /scripts/lib.sh + +export TF_CLI_ARGS_apply="-auto-approve" + +# Capture ^C and exit immediately +trap ctrl_c INT + +parse_args $* diff --git a/tasks/Makefile.child b/tasks/Makefile.child new file mode 100644 index 0000000..c7515dd --- /dev/null +++ b/tasks/Makefile.child @@ -0,0 +1,64 @@ +ACCOUNTS_ENABLED ?= + +-include artifacts/Makefile.env + +# Define a macro to call terraform with appropriate state folder +terraform = TF_STATE_DIR=.terraform/$(1) terraform + +# Define a macro (`child`) for a child init & provisioner +define child = + +## Validate the child configuration exists +$(1)/validate: + @[ -f "$$(CONFIGS)/$(1).tfvars" ] || (echo "$$(CONFIGS)/$(1).tfvars does not exist"; exit 1) + +## Clean up terraform state and modules for $(1) account +$(1)/clean: + rm -rf $(1).tfstate $(1).tfstate.backup accounts/$(1) + +## Initialize the "$(1)" AWS account +$(1)/init: $(1)/validate $(1)/clean + $(call terraform,$(1)) init -from-module=modules/child accounts/$(1) + $(call terraform,$(1)) apply \ + -var-file=artifacts/aws.tfvars \ + -var-file=artifacts/accounts.tfvars \ + -var-file=$$(CONFIGS)/root.tfvars \ + -var-file=$$(CONFIGS)/$(1).tfvars \ + -state=$(1).tfstate \ + -auto-approve \ + accounts/$(1) + +## Start a shell in the "$(1)" container (depends on $(1)/init) +$(1)/shell: + $$(DOCKER_RUN) --name=$(1) -v $(HOME):/localhost $(1) -l + +## Provision resources in the "child" account +$(1)/provision: + $$(DOCKER_RUN) --name=$(1) -e SKIP_MODULES="^(cloudtrail)$$$$" $(1) -l -c "/scripts/provision.sh --import-env --assume-role --apply-modules" + +## Finalize all account configuration +$(1)/finalize: + $$(DOCKER_RUN) --name=$(1) $(1) -l -c "/scripts/provision.sh --import-env --assume-role --apply-modules" + +## Initialize all the $(1) AWS subaccounts (depends on init/root) +$(1): $(1)/init $(1)/provision + @exit 0 + +endef + +ifneq ($(ACCOUNTS_ENABLED),) +# Render the macro for each child account +$(foreach account, $(ACCOUNTS_ENABLED), $(eval $(call child,$(account)))) +endif + +## Provision all child accounts +children: children/validate $(ACCOUNTS_ENABLED) + @exit 0 + +## Validate configurations exist for all child accounts +children/validate: $(addsuffix /validate,$(ACCOUNTS_ENABLED)) + @exit 0 + +## Finalize provisioning of all child accounts +children/finalize: $(addsuffix /finalize,$(ACCOUNTS_ENABLED)) + @exit 0 diff --git a/tasks/Makefile.root b/tasks/Makefile.root new file mode 100644 index 0000000..c2f3eee --- /dev/null +++ b/tasks/Makefile.root @@ -0,0 +1,31 @@ +## Clean up terraform state and modules for root account +root/clean: + rm -rf root.tfstate root.tfstate.backup accounts/root + +## Initialize the "root" AWS account +root/init: root/clean + $(DOCKER_RUN) cloudposse/geodesic:0.46.0 -c /scripts/get-root-account-id.sh + terraform init -from-module=modules/root accounts/root + terraform apply \ + -var-file=artifacts/aws.tfvars \ + -var-file=$(CONFIGS)/root.tfvars \ + -auto-approve \ + -state=root.tfstate \ + accounts/root + terraform output -state=root.tfstate docker_image > artifacts/root-docker-image + +## Provision resources in the "root" account +root/provision: + $(DOCKER_RUN) --name=root -e SKIP_MODULES="^(root-dns|cloudtrail)$$" root -l -c "/scripts/provision.sh --import-env --disable-profile --apply-modules --export-accounts" + +## Finalize the configuration of the AWS "root" account (depends on root/init, root/provision, and children) +root/finalize: + $(DOCKER_RUN) --name=root root -l -c "/scripts/provision.sh --import-env --assume-role --apply-modules" + +## Start a shell in the "root" container (depends on root/init) +root/shell: + $(DOCKER_RUN) --name=root -v $(HOME):/localhost root -l + +## Provision "root" account +root: root/init root/provision + @exit 0 diff --git a/templates/.dockerignore b/templates/.dockerignore new file mode 100644 index 0000000..14dd8df --- /dev/null +++ b/templates/.dockerignore @@ -0,0 +1,18 @@ +.git +.gitignore +.editorconfig + +# Compiled files +**/.terraform/* +**/.terragrunt-cache/* +*.tfstate +*.tfstate.* +*.tfvars + +# Module directory +.terraform +**/.idea +**/*.iml + +**/.build-harness +**/build-harness diff --git a/templates/.editorconfig b/templates/.editorconfig new file mode 100644 index 0000000..b37aa4c --- /dev/null +++ b/templates/.editorconfig @@ -0,0 +1,24 @@ +# top-most EditorConfig file +root = true + +# Unix-style newlines with a newline ending every file +[*] +end_of_line = lf +insert_final_newline = true + +# Override for Makefile +[{Makefile, makefile, GNUmakefile}] +indent_style = tab +indent_size = 4 + +[Makefile.*] +indent_style = tab +indent_size = 4 + +[shell] +indent_style = tab +indent_size = 4 + +[*.sh] +indent_style = tab +indent_size = 4 diff --git a/templates/.gitignore b/templates/.gitignore new file mode 100644 index 0000000..55ee851 --- /dev/null +++ b/templates/.gitignore @@ -0,0 +1,13 @@ +# Compiled files +**/.terraform/* +*.tfstate +*.tfstate.* +*.tfvars + +# Module directory +.terraform +**/.idea +**/*.iml + +**/.build-harness +**/build-harness diff --git a/templates/Dockerfile.child b/templates/Dockerfile.child new file mode 100644 index 0000000..c3e9048 --- /dev/null +++ b/templates/Dockerfile.child @@ -0,0 +1,56 @@ +# +# Dockerfile generated from https://github.com/cloudposse/reference-architectures +# + +FROM ${terraform_root_modules_image} as terraform-root-modules + +FROM ${geodesic_base_image} + +ENV DOCKER_IMAGE="${docker_registry}/${image_name}" +ENV DOCKER_TAG="${image_tag}" + +# General +ENV NAMESPACE="${namespace}" +ENV STAGE="${stage}" + +# Geodesic banner +ENV BANNER="${stage}" + +# Message of the Day +ENV MOTD_URL="${motd_url}" + +# AWS Region +ENV AWS_REGION="${aws_region}" +ENV AWS_ACCOUNT_ID="${aws_account_id}" +ENV AWS_ROOT_ACCOUNT_ID="${aws_root_account_id}" + +# chamber KMS config +ENV CHAMBER_KMS_KEY_ALIAS="alias/$${NAMESPACE}-$${STAGE}-chamber" + +# Terraform State Bucket +ENV TF_BUCKET_REGION="$${AWS_REGION}" +ENV TF_BUCKET="$${NAMESPACE}-$${STAGE}-terraform-state" +ENV TF_DYNAMODB_TABLE="$${NAMESPACE}-$${STAGE}-terraform-state-lock" + +# Default AWS Profile name +ENV AWS_DEFAULT_PROFILE="$${NAMESPACE}-$${STAGE}-admin" +ENV AWS_MFA_PROFILE="$${NAMESPACE}-root-admin" + +# Copy root modules +${terraform_root_modules} + +# Place configuration in 'conf/' directory +COPY conf/ /conf/ + +# Install configuration dependencies +RUN make -C /conf install + +# Filesystem entry for tfstate +RUN s3 fstab '$${TF_BUCKET}' '/' '/secrets/tf' + +COPY rootfs/ / + +# Generate kops manifest +RUN build-kops-manifest + +WORKDIR /conf/ diff --git a/templates/Dockerfile.root b/templates/Dockerfile.root new file mode 100644 index 0000000..51ac588 --- /dev/null +++ b/templates/Dockerfile.root @@ -0,0 +1,51 @@ +# +# Dockerfile generated from https://github.com/cloudposse/reference-architectures +# + +FROM ${terraform_root_modules_image} as terraform-root-modules + +FROM ${geodesic_base_image} + +ENV DOCKER_IMAGE="${docker_registry}/${image_name}" +ENV DOCKER_TAG="${image_tag}" + +# General +ENV NAMESPACE="${namespace}" +ENV STAGE="${stage}" + +# Geodesic banner +ENV BANNER="${image_name}" + +# Message of the Day +ENV MOTD_URL="${motd_url}" + +# AWS Region +ENV AWS_REGION="${aws_region}" +ENV AWS_DEFAULT_REGION="$${AWS_REGION}" +ENV AWS_ACCOUNT_ID="${aws_account_id}" +ENV AWS_ROOT_ACCOUNT_ID="${aws_root_account_id}" + +# Terraform state bucket and DynamoDB table for state locking +ENV TF_BUCKET_REGION="$${AWS_REGION}" +ENV TF_BUCKET="$${NAMESPACE}-$${STAGE}-terraform-state" +ENV TF_DYNAMODB_TABLE="$${NAMESPACE}-$${STAGE}-terraform-state-lock" + +# Default AWS Profile name +ENV AWS_DEFAULT_PROFILE="$${NAMESPACE}-$${STAGE}-admin" + +# chamber KMS config +ENV CHAMBER_KMS_KEY_ALIAS="alias/$${NAMESPACE}-$${STAGE}-chamber" + +# Copy root modules +${terraform_root_modules} + +# Place configuration in 'conf/' directory +COPY conf/ /conf/ + +# Install configuration dependencies +RUN make -C /conf install + +# Filesystem entry for tfstate +RUN s3 fstab '$${TF_BUCKET}' '/' '/secrets/tf' + +WORKDIR /conf/ diff --git a/templates/Makefile b/templates/Makefile new file mode 100644 index 0000000..264b430 --- /dev/null +++ b/templates/Makefile @@ -0,0 +1,34 @@ +export DOCKER_ORG ?= ${docker_registry} +export DOCKER_IMAGE ?= $(DOCKER_ORG)/${image_name} +export DOCKER_TAG ?= latest +export DOCKER_IMAGE_NAME ?= $(DOCKER_IMAGE):$(DOCKER_TAG) +export DOCKER_BUILD_FLAGS = --pull +export README_DEPS ?= docs/targets.md docs/terraform.md +export INSTALL_PATH ?= /usr/local/bin +export SCRIPT ?= $(notdir $(DOCKER_IMAGE)) + +-include $(shell curl -sSL -o .build-harness "https://git.io/build-harness"; echo .build-harness) + +## Initialize build-harness, install deps, build docker container, install wrapper script and run shell +all: init deps build install run + @exit 0 + +## Install dependencies (if any) +deps: + @exit 0 + +## Build docker image +build: + @make --no-print-directory docker/build + +## Push docker image to registry +push: + docker push $(DOCKER_IMAGE) + +## Install wrapper script from geodesic container +install: + @docker run --rm $(DOCKER_IMAGE_NAME) | sudo -E bash -s $(DOCKER_TAG) + +## Start the geodesic shell by calling wrapper script +run: + $(SCRIPT) diff --git a/templates/conf/Makefile b/templates/conf/Makefile new file mode 100644 index 0000000..91bff9e --- /dev/null +++ b/templates/conf/Makefile @@ -0,0 +1,4 @@ +install: + find . -mindepth 2 -name Makefile -exec grep -l install: {} \; | \ + xargs --no-run-if-empty -n 1 dirname | \ + xargs --no-run-if-empty -n 1 -I {} bash -c "make -C {} install || exit 255" diff --git a/templates/conf/accounts/terraform.tfvars b/templates/conf/accounts/terraform.tfvars new file mode 100644 index 0000000..c8ad4c8 --- /dev/null +++ b/templates/conf/accounts/terraform.tfvars @@ -0,0 +1,5 @@ +# Email format for accounts (e.g. `ops+%s@example.co`) +account_email = "${account_email}" + +# List of accounts to enable +accounts_enabled = ${accounts_enabled} diff --git a/templates/conf/iam/terraform.tfvars b/templates/conf/iam/terraform.tfvars new file mode 100644 index 0000000..79f3e55 --- /dev/null +++ b/templates/conf/iam/terraform.tfvars @@ -0,0 +1,2 @@ +# List of accounts to enable +accounts_enabled = ${accounts_enabled} diff --git a/templates/conf/root-dns/terraform.tfvars b/templates/conf/root-dns/terraform.tfvars new file mode 100644 index 0000000..0efe132 --- /dev/null +++ b/templates/conf/root-dns/terraform.tfvars @@ -0,0 +1,8 @@ +# APEX service discovery domain for all accounts (e.g. `example.co`) +parent_domain_name = "${parent_domain_name}" + +# Service discovery domain for *this* (aka "root") account +root_domain_name = "${root_domain_name}" + +# List of accounts to enable +accounts_enabled = ${accounts_enabled} diff --git a/templates/conf/users/user.tf b/templates/conf/users/user.tf new file mode 100644 index 0000000..0e3dbfa --- /dev/null +++ b/templates/conf/users/user.tf @@ -0,0 +1,12 @@ +module "${resource_name}" { + source = "git::https://github.com/cloudposse/terraform-aws-iam-user.git?ref=tags/0.1.1" + name = "${username}" + pgp_key = "keybase:${keybase_username}" + groups = "$${local.admin_groups}" + force_destroy = "true" +} + +output "${username}" { + description = "Decrypt command" + value = "$${module.${resource_name}.keybase_password_decrypt_command}" +}