Skip to content

Commit

Permalink
Merge pull request #248 from commjoen/feature/aws-shared-state
Browse files Browse the repository at this point in the history
Add S3 backend
  • Loading branch information
commjoen authored Apr 4, 2022
2 parents 7c802db + 79d4f90 commit 2dca667
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 12 deletions.
17 changes: 16 additions & 1 deletion aws/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ In this setup we integrate the secrets-exercise online with AWS EKS and let Pods
We use managed node groups so as we don't want the hassle of managing the EC2 instances ourselves, and Fargate doesn't suit our needs since we use a StatefulSet. If you want to know more about integrating secrets with EKS, check [EKS and SSM Parameter Store](https://docs.aws.amazon.com/systems-manager/latest/userguide/integrating_csi_driver.html) and [EKS and Secrets Manager](https://docs.aws.amazon.com/secretsmanager/latest/userguide/integrating_csi_driver.html).
Please make sure that the account in which you run this exercise has either CloudTrail enabled, or is not linked to your current organization and/or DTAP environment.


## Pre-requisites

Have the following tools installed:
Expand All @@ -20,6 +19,20 @@ Have the following tools installed:

Make sure you have an active account at AWS for which you have configured the credentials on the system where you will execute the steps below. In this example we stored the credentials under an aws profile as `awsuser`.

### Multi-user setup: shared state

If you want to host a multi-user setup, you will probably want to share the state file so that everyone can try related challenges. We have provided a starter to easily do so using a Terraform S3 backend.

First, create an s3 bucket (optionally add `-var="region=YOUR_DESIRED_REGION"` to the apply to use a region other than the default eu-west-1):

```bash
cd shared-state
terraform init
terraform apply
```

The bucket name should be in the output. Please use that to configure the terraform backend in `main.tf`.

## Installation

The terraform code is loosely based on [this EKS managed Node Group TF example](https://github.com/terraform-aws-modules/terraform-aws-eks/tree/master/examples/managed_node_groups).
Expand All @@ -46,6 +59,7 @@ Are you done playing? Please run `terraform destroy` twice to clean up.
Run `AWS_PROFILE=<your_profile> k8s-vault-aws-start.sh` and connect to [http://localhost:8080](http://localhost:8080) when it's ready to accept connections (you'll read the line `Forwarding from 127.0.0.1:8080 -> 8080` in your console). Now challenge 9 and 10 should be available as well.

### Resume it

When you stopped the `k8s-vault-aws-start.sh` script and want to resume the port forward run: `k8s-vault-aws-resume.sh`. This is because if you run the start script again it will replace the secret in the vault and not update the secret-challenge application with the new secret.

### Clean it up
Expand All @@ -54,6 +68,7 @@ When you're done:

1. Kill the port forward.
2. Run `terraform destroy` to clean up the infrastructure.
1. If you've deployed the `shared-state` s3 bucket, also `cd shared-state` and `terraform destroy` there.
3. Run `unset KUBECONFIG` to unset the KUBECONFIG env var.
4. Run `rm ~/.kube/wrongsecrets` to remove the kubeconfig file.
5. Run `rm terraform.ts*` to remove local state files.
Expand Down
20 changes: 20 additions & 0 deletions aws/main.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@

terraform {
required_version = ">= 0.13.1, <= 2.0.0"

required_providers {
aws = ">= 3.22.0, <5.0.0"
random = "~> 3.0"
http = "~> 2.1"
}

# Set your region and bucket name (output from shared state) in the placeholder below
# Then uncomment and apply!
# backend "s3" {
# region = "eu-west-1" # Change if desired
# bucket = ""
# key = "wrongsecrets/terraform.tfstate"
# }
}


locals {
vpc_cidr = "172.16.0.0/16"

Expand Down
33 changes: 33 additions & 0 deletions aws/shared-state/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
terraform {
required_providers {
aws = {
version = "~> 4.0"
}
}
}

variable "region" {
description = "The AWS region to use"
type = string
default = "eu-west-1"
}

provider "aws" {
region = var.region
}

resource "aws_s3_bucket" "state" {}

resource "aws_s3_bucket_server_side_encryption_configuration" "encryption" {
bucket = aws_s3_bucket.state.id
rule {
apply_server_side_encryption_by_default {
sse_algorithm = "AES256"
}
}
}

output "s3_bucket_name" {
description = "Name of the terraform state bucket"
value = aws_s3_bucket.state.id
}
11 changes: 0 additions & 11 deletions aws/versions.tf

This file was deleted.

0 comments on commit 2dca667

Please sign in to comment.