|
1 | | -# infrastructure |
2 | | -Terraform infrastructure as code |
| 1 | +# AWS EKS Stack |
3 | 2 |
|
4 | | -## Dependencies |
5 | | -The only things that will need to be set up before deploying for the first time are an AWS account, and a domain name with a Route53 zone created for it. |
6 | | -You'll also need a user created and the credentials available in your shell. |
| 3 | +This is a [Commit0](https://github.com/commitdev/commit0) module which sets up a |
| 4 | +hosting environment on AWS running Kubernetes. It will generate terraform output |
| 5 | +which describes the environment mapped in this [architecture |
| 6 | +diagram](./docs/architecture-overview.svg). |
7 | 7 |
|
8 | | -[AWS Docs: Set up the AWS CLI](https://docs.aws.amazon.com/polly/latest/dg/setup-aws-cli.html) |
9 | | -[AWS Docs: Register a domain with Route53](https://docs.aws.amazon.com/Route53/latest/DeveloperGuide/domain-register.html) |
| 8 | +**Prerequisites** |
| 9 | + - Terraform installed |
| 10 | + - Kubectl installed |
| 11 | + - A valid AWS account |
| 12 | + - [Set up the AWS CLI](https://docs.aws.amazon.com/polly/latest/dg/setup-aws-cli.html) |
| 13 | + - [A domain registered with Route53](https://docs.aws.amazon.com/Route53/latest/DeveloperGuide/domain-register.html) |
10 | 14 |
|
11 | | -## Instructions |
12 | | -To init and apply the terraform configs, simply run the makefile and specify the environment. The default environment is `staging` |
13 | | -``` |
14 | | -make ENV=<environment> |
15 | | -``` |
16 | | - |
17 | | -## Components |
18 | | -[See Diagram](./docs/architecture-overview.svg) |
19 | | - |
20 | | -## AWS Stack |
21 | | -All the modules that are being applied can be found in [terraform/modules/environment/main.tf](terraform/modules/environment/main.tf) |
22 | | -- [ ] VPC - [Virtual Private Cloud](https://aws.amazon.com/vpc/pricing/) |
23 | | -- [ ] EKS - [Elastic Kubernetes Service](https://aws.amazon.com/eks/pricing/) |
24 | | -- [ ] EC2 - [Elastic Comput Cloud orchestrated by EKS](https://aws.amazon.com/eks/pricing/) |
25 | | -- [ ] S3 - [Simple Storage Service](https://aws.amazon.com/s3/pricing/) |
26 | | -- [ ] Cloudfront - [Cloudfront Pricing](https://aws.amazon.com/cloudfront/pricing/) |
27 | | -- [ ] ECR - [Elastic Container Registry](https://aws.amazon.com/ecr/pricing/) |
28 | | - |
29 | | -## Costs |
30 | | -The most expensive component will be the EKS cluster as well as the instances that it spins up. The rest of the modules have very low cost |
31 | | -- Costs will vary depending on the region selected but based on `us-west-2` the following items will contribute to the most of the cost of the infrastructure |
32 | | -- EKS Cluster: $0.1 USD / hr |
33 | | -- NAT Gateway: $0.045 USD / hr |
34 | | -- RDS (db.t3.small): $0.034 USD / hr |
35 | | -- EC2 (t2.small): $0.023 USD / hr |
36 | | -- Expected total monthly cost: $ 0.202 USD / hr or ~$150USD / month |
| 15 | +## Getting Started |
37 | 16 |
|
38 | | -EC2 instance sizing can be configured in [terraform/environments/staging/main.tf](terraform/environments/staging/main.tf) |
| 17 | +This is meant to be used with commit0 and not directly. See |
| 18 | +the [commit0](https://github.com/commitdev/commit0) repository for more |
| 19 | +information. The commit0 tool will parse through configuration and fill in any |
| 20 | +template variables which need to be encoded into the terraform configuration. |
| 21 | +Once that is done you'll have a directory containing the contents of this |
| 22 | +repository minus the `.git` directory. |
39 | 23 |
|
| 24 | +### Edits Required |
40 | 25 |
|
41 | | -## AWS Setting the Kubernetes context |
42 | | -``` |
43 | | -aws eks update-kubeconfig --name <cluster-name> --region us-west-2 |
44 | | -``` |
45 | | - |
46 | | -## Workin with Kubernetes |
47 | | -Running Bash |
48 | | -``` |
49 | | -kubectl run -it --image ubuntu bash |
50 | | -``` |
51 | | - |
52 | | -Getting secrets |
53 | | -``` |
54 | | -kubectl get secret --namespace default <secret-key> -o jsonpath="{.data.password}" | base64 --decode; echo |
55 | | -``` |
56 | | -Port forward |
57 | | -``` |
58 | | -kubectl port-forward --namespace default $(kubectl get pods --namespace default -l app.kubernetes.io/instance=keycloak -o jsonpath="{.items[0].metadata.name}") 8080 |
59 | | -``` |
60 | | - |
61 | | -## AWS ECR Container Image Hosting |
62 | | - |
63 | | -### 1. Creating an ECR repository |
64 | | -``` |
65 | | -aws ecr describe-repositories --region us-west-2 |
66 | | -aws ecr create-repository --repository-name <ecr-repo-name> --region <aws-region> |
67 | | -aws ecr delete-repository --repository-name <ecr-repo-name> --region <aws-region> |
68 | | -``` |
69 | | -Describing the ECR repositories will also give you a list of the fully resolved repository URI. |
| 26 | +This repository requires post-template configuration edits to the AMI depending |
| 27 | +on the region you chose. To find the appropriate AMI for your region you can use |
| 28 | +the following snippet: |
70 | 29 |
|
71 | | -If you need your AWS account ID, you can use: |
72 | | -``` |
73 | | -aws sts get-caller-identity --query Account --output text |
| 30 | +```shell |
| 31 | +$ REGION=us-east-2 |
| 32 | +$ aws ssm get-parameters \ |
| 33 | + --names /aws/service/eks/optimized-ami/1.15/amazon-linux-2/recommended/image_id \ |
| 34 | + --region $REGION \ |
| 35 | + --query "Parameters[0].Value" | cat |
74 | 36 | ``` |
75 | 37 |
|
76 | | -### 2. Authenticate your Docker with AWS ECR |
77 | | -[AWS DOCS: Registry Authentication](https://docs.aws.amazon.com/AmazonECR/latest/userguide/Registries.html#registry_auth) |
78 | | -``` |
79 | | -aws ecr get-login --region <region> --no-include-email |
| 38 | +### Apply Configuration |
| 39 | +To init and apply the terraform configs, simply run the `make` and specify the |
| 40 | +environment. The default environment is `staging` |
| 41 | +```shell |
| 42 | +$ make ENV=<environment> |
80 | 43 | ``` |
81 | | -This will output a docker command for you to login with the password. Eg. `docker login -u AWS -p password https://<aws_account_id>.dkr.ecr.<region>.amazonaws.com` |
82 | 44 |
|
83 | | -### 3. Push your Docker image to the repository |
84 | | -[AWS Docs: Docker Push ECR Image](https://docs.aws.amazon.com/AmazonECR/latest/userguide/docker-push-ecr-image.html) |
| 45 | +### Set up an application |
| 46 | +Configure your k8s context |
85 | 47 |
|
86 | | -Make sure you have a docker image withe the appropriately named tag that corresponds to an ECR repo. |
87 | | -``` |
88 | | -docker build --tag <aws_account_id>.dkr.ecr.<region>.amazonaws.com/<ecr-repo-name> . |
89 | | -``` |
90 | | -or for an existing image |
91 | | -``` |
92 | | -docker tag <dockerImage> <aws_account_id>.dkr.ecr.<region>.amazonaws.com/<ecr-repo-name> |
93 | | -``` |
94 | | -then just docker push |
95 | | -``` |
96 | | -docker push <aws_account_id>.dkr.ecr.<region>.amazonaws.com/<ecr-repo-name> |
| 48 | +```shell |
| 49 | +$ aws eks update-kubeconfig --name <cluster-name> --region us-west-2 |
97 | 50 | ``` |
98 | 51 |
|
99 | | -### Getting Secrets from AWS Secrets Manager |
100 | | -The terraform by default generates random password during for the RDS instance and stores it in AWS secrets manager |
101 | | -[Using AWS Secretsmanager](https://github.com/aws-samples/aws-workshop-for-kubernetes/tree/master/04-path-security-and-networking/401-configmaps-and-secrets#secrets-using-aws-secrets-manager) |
102 | | -``` |
103 | | -aws secretsmanager list-secrets |
104 | | -aws secretsmanager get-secret-value --secret-id <SECRETNAME> --region <REGION> |
105 | | -``` |
| 52 | +Then talk to Bill. |
106 | 53 |
|
107 | | -## Setting up RDS |
| 54 | +### How much does this stack cost? |
| 55 | +The expected total monthly cost: $ 0.202 USD / hr or ~$150USD / month. The most |
| 56 | +expensive component will be the EKS cluster as well as the instances that it |
| 57 | +spins up. Costs will vary depending on the region selected but based on |
| 58 | +`us-west-2` the following items will contribute to the most of the cost of the |
| 59 | +infrastructure: |
| 60 | + - EKS Cluster: $0.1 USD / hr |
| 61 | + - NAT Gateway: $0.045 USD / hr |
| 62 | + - RDS (db.t3.small): $0.034 USD / hr |
| 63 | + - EC2 (t2.small): $0.023 USD / hr |
108 | 64 |
|
109 | | -In a Kubernetes cluster you'll need to run bash container to access the RDS in VPC |
110 | | -``` |
111 | | -kubectl run -it --image ubuntu bash |
112 | | -kubectl exec -it <bash-pod-id> -- /bin/bash |
113 | | -``` |
114 | | - |
115 | | -In the container shell |
116 | | -``` |
117 | | -Apt-get update -y |
118 | | -Apt-get install pgcli |
119 | | -pgcli -h <rds-url> -U master_user -d postgres |
120 | | -CREATE DATABASE <database>; |
121 | | -create USER <db-user> with password '<db-password>'; |
122 | | -GRANT ALL PRIVILEGES ON DATABASE <database> to <db-user>; |
123 | | -``` |
124 | | - |
125 | | -### Accessing Database in VPC: |
126 | | -``` |
127 | | -kubectl run --restart=Never --image=alpine/socat db-gateway -- -d -d tcp-listen:5432,fork,reuseaddr tcp-connect:<RDS_ADDRESS>:5432 |
128 | | -kubectl port-forward db-gateway 5432:5432 |
129 | | -``` |
| 65 | +EC2 instance sizing can be configured in [terraform/environments/staging/main.tf](terraform/environments/staging/main.tf) |
0 commit comments