Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 2 additions & 51 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,52 +1,3 @@
ENV ?= staging

apply: apply-remote-state apply-secrets apply-env apply-k8s-utils

## remove state file only if exit code 0 from terraform apply
apply-remote-state:
pushd terraform/bootstrap/remote-state && \
terraform init && \
terraform apply -var "environment=$(ENV)" && \
rm ./terraform.tfstate

apply-secrets:
pushd terraform/bootstrap/secrets && \
terraform init && \
terraform apply && \
rm ./terraform.tfstate

apply-env:
pushd terraform/environments/$(ENV); \
terraform init && \
terraform apply

apply-k8s-utils: update-k8s-conf
pushd kubernetes/terraform/environments/$(ENV) && \
terraform init && \
terraform apply

update-k8s-conf:
aws eks --region <% index .Params `region` %> update-kubeconfig --name <% .Name %>-$(ENV)-<% index .Params `region` %>

teardown: teardown-k8s-utils teardown-env teardown-secrets teardown-remote-state

teardown-remote-state:
export AWS_PAGER='' && \
aws s3 rb s3://<% .Name %>-$(ENV)-terraform-state --force && \
aws dynamodb delete-table --table-name <% .Name %>-$(ENV)-terraform-state-locks

teardown-secrets:
export AWS_PAGER='' && \
aws secretsmanager list-secrets --query "SecretList[?Tags[?Key=='project' && Value=='<% .Name %>']].[Name] | [0][0]" | xargs aws secretsmanager delete-secret --secret-id && \
aws iam delete-access-key --user-name <% .Name %>-ci-user --access-key-id $(shell aws iam list-access-keys --user-name <% .Name %>-ci-user --query "AccessKeyMetadata[0].AccessKeyId" | sed 's/"//g') && \
aws iam delete-user --user-name <% .Name %>-ci-user

teardown-env:
pushd terraform/environments/$(ENV) && \
terraform destroy

teardown-k8s-utils:
pushd kubernetes/terraform/environments/$(ENV) && \
terraform destroy

.PHONY: apply apply-remote-state apply-secrets apply-env apply-k8s-utils teardown-k8s-utils teardown-env teardown-secrets teardown-remote-state
run:
@echo Done
65 changes: 4 additions & 61 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# AWS EKS Stack

This is a [Commit0](https://github.com/commitdev/commit0) module which sets up a
This is a [zero](https://github.com/commitdev/zero) module which sets up a
hosting environment on AWS running Kubernetes. It will generate terraform output
which describes the environment mapped in this [architecture
diagram](./docs/architecture-overview.svg).
diagram](./templates/docs/architecture-overview.svg).

**Prerequisites**
- Terraform installed
Expand All @@ -16,67 +16,10 @@ diagram](./docs/architecture-overview.svg).

## Getting Started

This is meant to be used with the commit0 `stack` tool and not directly. See
the [stack](https://github.com/commitdev/stack) repository for more
This is meant to be used with the `zero` tool and not directly. See
the [zero](https://github.com/commitdev/zero) repository for more
information. The tool will parse through configuration and fill in any
template variables which need to be encoded into the terraform configuration.
Once that is done you'll have a directory containing the contents of this
repository minus the `.git` directory.

To generate the templates you will need to provide some values to fill in.

First get the AMI for your region:
```shell
$ REGION=us-east-1
$ aws ssm get-parameters \
--names /aws/service/eks/optimized-ami/1.15/amazon-linux-2/recommended/image_id \
--region $REGION \
--query "Parameters[0].Value"
```

Then get the aws account id:
```shell
aws sts get-caller-identity --query "Account"
```

Then create a `zero.yml` file and fill in the appropriate values:

```yaml
name: my-project

params:
region: us-east-1
accountId: <from above>
kubeWorkerAMI: ami-<from above>
productionHostRoot: domain.com
productionFrontendHost: domain.com
productionBackendHost: api.domain.com
stagingHostRoot: domain-staging.com
stagingFrontendHost: domain-staging.com
stagingBackendHost: api.domain-staging.com

```

And run `stack`:
```shell
$ stack -config config.yml commit0-aws-eks-stack/ my-project-infrastructure/
```

### Apply Configuration
To init and apply the terraform configs, simply run the `make` and specify the
environment. The default environment is `staging`
```shell
$ make ENV=<environment>
```

### Set up an application
Configure your k8s context

```shell
$ aws eks update-kubeconfig --name <cluster-name> --region us-east-1
```

Then talk to Bill.

### How much does this stack cost?
The expected total monthly cost: $ 0.202 USD / hr or ~$150USD / month. The most
Expand Down
6 changes: 0 additions & 6 deletions commit0.module.yml

This file was deleted.

2 changes: 1 addition & 1 deletion .gitignore → templates/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,4 @@ $RECYCLE.BIN/
# Windows shortcuts
*.lnk

# End of https://www.gitignore.io/api/linux,macos,windows,terraform
# End of https://www.gitignore.io/api/linux,macos,windows,terraform
52 changes: 52 additions & 0 deletions templates/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
ENV ?= staging

apply: apply-remote-state apply-secrets apply-env apply-k8s-utils

## remove state file only if exit code 0 from terraform apply
apply-remote-state:
pushd terraform/bootstrap/remote-state && \
terraform init && \
terraform apply -var "environment=$(ENV)" && \
rm ./terraform.tfstate

apply-secrets:
pushd terraform/bootstrap/secrets && \
terraform init && \
terraform apply && \
rm ./terraform.tfstate

apply-env:
pushd terraform/environments/$(ENV); \
terraform init && \
terraform apply

apply-k8s-utils: update-k8s-conf
pushd kubernetes/terraform/environments/$(ENV) && \
terraform init && \
terraform apply

update-k8s-conf:
aws eks --region <% index .Params `region` %> update-kubeconfig --name <% .Name %>-$(ENV)-<% index .Params `region` %>

teardown: teardown-k8s-utils teardown-env teardown-secrets teardown-remote-state

teardown-remote-state:
export AWS_PAGER='' && \
aws s3 rb s3://<% .Name %>-$(ENV)-terraform-state --force && \
aws dynamodb delete-table --table-name <% .Name %>-$(ENV)-terraform-state-locks

teardown-secrets:
export AWS_PAGER='' && \
aws secretsmanager list-secrets --query "SecretList[?Tags[?Key=='project' && Value=='<% .Name %>']].[Name] | [0][0]" | xargs aws secretsmanager delete-secret --secret-id && \
aws iam delete-access-key --user-name <% .Name %>-ci-user --access-key-id $(shell aws iam list-access-keys --user-name <% .Name %>-ci-user --query "AccessKeyMetadata[0].AccessKeyId" | sed 's/"//g') && \
aws iam delete-user --user-name <% .Name %>-ci-user

teardown-env:
pushd terraform/environments/$(ENV) && \
terraform destroy

teardown-k8s-utils:
pushd kubernetes/terraform/environments/$(ENV) && \
terraform destroy

.PHONY: apply apply-remote-state apply-secrets apply-env apply-k8s-utils teardown-k8s-utils teardown-env teardown-secrets teardown-remote-state
38 changes: 38 additions & 0 deletions templates/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Infrastructure

**Prerequisites**
- Terraform installed
- Kubectl installed
- Wget installed
- A valid AWS account
- [Set up the AWS CLI](https://docs.aws.amazon.com/polly/latest/dg/setup-aws-cli.html)
- [A domain registered with Route53](https://docs.aws.amazon.com/Route53/latest/DeveloperGuide/domain-register.html)
- Note: if you want to use different domain per environment (staging/poduction), you need to have multiple hosted zones. The available zones in Route53 can be found by running this command. `aws route53 list-hosted-zones`

### Using the Kubernetes Cluster

Before using the cluster the first time you'll need to set up your local `kubectl` context:
```shell
make update-k8s-conf
```

Then you should be able to run commands normally:
```shell
kubectl get pods -A
```


### Apply Configuration
To init and apply the terraform configs, simply run the `make` and specify the
environment. The default environment is `staging`
```shell
$ make ENV=<environment>
```


### Set up an application
Configure your k8s context

```shell
$ aws eks update-kubeconfig --name <cluster-name> --region us-east-1
```
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
43 changes: 43 additions & 0 deletions zero-module.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: aws-eks-stack
description: 'zero module for a basic AWS kubernetes stack on EKS'
author: 'Commit'

template:
strictMode: true
delimiters:
- '<%'
- '%>'
inputDir: 'templates'
outputDir: 'infrastructure'

requiredCredentials:
- aws
- github

parameters:
- field: region
label: Select AWS Region
options:
- "us-west-1"
- "us-west-2"
- "us-east-1"
- "us-east-2"
- field: productionHostRoot
label: Production Root Host Name (e.g. mydomain.com) - this must be the root of the chosen domain, not a subdomain.
- field: productionFrontendHost
label: Production Frontend Host Name (e.g. app.mydomain.com)
- field: productionBackendHost
label: Production Backend Host Name (e.g. api.mydomain.com)
- field: stagingHostRoot
label: Staging Root Host Name (e.g. mydomain-staging.com) - this must be the root of the chosen domain, not a subdomain.
- field: stagingFrontendHost
label: Staging Frontend Host Name (e.g. app.mydomain-staging.com)
- field: stagingBackendHost
label: Staging Backend Host Name (e.g. api.mydomain-staging.com)
- field: accountId
label: AWS Account ID
execute: aws sts get-caller-identity --query "Account" | tr -d '"'
- field: eksWorkerAMI
label: EKS Worker EC2 AMI ID
execute: aws ssm get-parameters --names /aws/service/eks/optimized-ami/1.15/amazon-linux-2/recommended/image_id --region $region --query "Parameters[0].Value" | tr -d '"'