Skip to content

Commit 0188a10

Browse files
authored
Various local dev improvements (#973)
1 parent e706119 commit 0188a10

15 files changed

+111
-165
lines changed

.github/workflows/pr.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ jobs:
4242
uses: actions/checkout@v4
4343
- name: Make shell
4444
run: |
45-
make shell shell_simple_command='ls'
45+
bash hack/exec.sh '' 'ls -la'
4646
4747
pre-commit:
4848
name: "Pre-commit hooks"

Makefile

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ shell_command=''
55
shell_simple_command=''
66
glob='-'
77

8-
98
.PHONY: install
109
install:
1110
cd website; npm install
@@ -24,7 +23,7 @@ test:
2423

2524
.PHONY: shell
2625
shell:
27-
bash hack/shell.sh $(environment) $(shell_command) $(shell_simple_command)
26+
bash hack/shell.sh $(environment)
2827

2928
.PHONY: reset-environment
3029
reset-environment:
@@ -34,18 +33,10 @@ reset-environment:
3433
delete-environment:
3534
bash hack/shell.sh $(environment) delete-environment
3635

37-
.PHONY: update-helm-versions
38-
update-helm-versions:
39-
bash hack/update-helm-versions.sh
40-
41-
.PHONY: verify-helm-metadata
42-
verify-helm-metadata:
43-
bash hack/verify-helm-metadata.sh
44-
4536
.PHONY: create-infrastructure
4637
create-infrastructure:
47-
bash hack/create-infrastructure.sh $(environment)
38+
bash hack/exec.sh $(environment) 'cat /cluster/eksctl/cluster.yaml | envsubst | eksctl create cluster -f -'
4839

4940
.PHONY: destroy-infrastructure
5041
destroy-infrastructure:
51-
bash hack/destroy-infrastructure.sh $(environment)
42+
bash hack/exec.sh $(environment) 'cat /cluster/eksctl/cluster.yaml | envsubst | eksctl delete cluster --wait --force --disable-nodegroup-eviction --timeout 45m -f -'

docs/authoring_content.md

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ The following pre-requisites are necessary to work on the content:
2323
- Installed locally:
2424
- Docker
2525
- `make`
26-
- `terraform`
2726
- `jq`
2827
- `npm`
2928
- `kubectl`
@@ -65,9 +64,16 @@ There are some additional things to set up which are not required but will make
6564

6665
### Creating the infrastructure
6766

68-
When creating your content you will want to test the commands you specify against infrastructure that mirrors what will be used in the actual workshop by learners. This can easily by done locally and will use the cluster configuration in `./cluster/eksctl/cluster.yaml`.
67+
When creating your content you will want to test the commands you specify against infrastructure that mirrors what will be used in the actual workshop by learners. This can easily by done locally and with some convenience scripts that have been included.
6968

70-
Ensure that your AWS credentials are set so eksctl is able to authenticate against your IAM account. It will source credentials following the standard mechanism used by the likes of the AWS CLI, which you can find documented [here](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-authentication.html).
69+
> [!TIP]
70+
> Why should you use the `make` commands and the associated convenience scripts instead of "doing it yourself"? The various scripts provided are intended to provide an environment consistent with what the end-user of the workshop will use. This is important because the workshop has a number of 3rd party dependencies that are carefully managed with regards to versioning.
71+
72+
Many of the convenience scripts we'll use will make calls to AWS APIs so will need to be able to authenticate. Getting AWS credentials in to a container in a portable way can be a challenge, and there are several options available:
73+
74+
1. Set `ASSUME_ROLE` environment variable in the terminal where you run the `make` commands to the ARN of an IAM role that you can assume with your current credentials. This will use the STS service to generate temporary credentials that will be injected in to the container. Example: `export ASSUME_ROLE='arn:aws:iam::123456789012:role/my-role'`
75+
1. Set `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` environment variables in the terminal where you run the `make` commands. It is recommended that these credentials be temporary. These variables will be injected in to the container.
76+
1. If you are developing on an EC2 instance which has an instance profile that provides the necessary IAM permissions then no action is needed as the container will automatically assume the role of the EC2 on which you're authoring your content.
7177

7278
You can then use the following convenience command to create the infrastructure:
7379

@@ -85,20 +91,16 @@ make destroy-infrastructure
8591

8692
When in the process of creating the content its likely you'll need to be fairly interactive in testing commands etc. During a real workshop users would do this on the Cloud9 IDE, but for our purposes for developing content quickly this is a poor experience because it is designed to refresh content automatically from GitHub. As a result it is recommended to _NOT use the Cloud9 IDE_ created by the Cloud Formation in this repository and instead use the flow below.
8793

88-
The repository provides a mechanism to easily create an interactive shell with access to the EKS cluster created by `make create-infrastructure`. This shell will automatically pick up changes to the content on your local machine and mirrors the Cloud9 used in a real workshop in terms of tools and setup.
89-
90-
To use this utility you must:
91-
92-
- Already run `make create-infrastructure`
93-
- Have some AWS credentials available in your current shell session (ie. you `aws` CLI must work)
94+
The repository provides a mechanism to easily create an interactive shell with access to the EKS cluster created by `make create-infrastructure`. This shell will automatically pick up changes to the content on your local machine and mirrors the Cloud9 used in a real workshop in terms of tools and setup. As such to use this utility you must have already run `make create-infrastructure`.
9495

95-
The shell session created will have AWS credentials injected, so you will immediately be able to use the `aws` CLI and `kubectl` commands with no further configuration:
96+
The shell session created will have AWS credentials injected, so you will immediately be able to use the `aws` CLI and `kubectl` commands with no further configuration.
9697

97-
If using [finch CLI](https://github.com/runfinch/finch) instead of `docker` CLI you need to set two environment variable `CONTAINER_CLI` or run `make` with the variable set like `CONTAINER_CLI=finch make shell` here how to set the variable in the terminal session for every command.
98-
99-
```bash
100-
export CONTAINER_CLI=finch
101-
```
98+
> [!NOTE]
99+
> If using [finch CLI](https://github.com/runfinch/finch) instead of `docker` CLI you need to set two environment variable `CONTAINER_CLI` or run `make` with the variable set like `CONTAINER_CLI=finch make shell` here how to set the variable in the terminal session for every command.
100+
>
101+
> ```bash
102+
> export CONTAINER_CLI=finch
103+
> ```
102104
103105
Run `make shell`:
104106

hack/create-infrastructure.sh

Lines changed: 0 additions & 25 deletions
This file was deleted.

hack/destroy-infrastructure.sh

Lines changed: 0 additions & 13 deletions
This file was deleted.

hack/exec.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/bin/bash
2+
3+
environment=$1
4+
shift 1
5+
shell_command=$@
6+
7+
set -Eeuo pipefail
8+
9+
# You can run script with finch like CONTAINER_CLI=finch ./shell.sh <terraform_context> <shell_command>
10+
CONTAINER_CLI=${CONTAINER_CLI:-docker}
11+
12+
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
13+
14+
source $SCRIPT_DIR/lib/common-env.sh
15+
16+
echo "Building container images..."
17+
18+
container_image='eks-workshop-environment'
19+
20+
(cd $SCRIPT_DIR/../lab && $CONTAINER_CLI build -q -t $container_image .)
21+
22+
source $SCRIPT_DIR/lib/generate-aws-creds.sh
23+
24+
echo "Executing command in container..."
25+
26+
$CONTAINER_CLI run --rm \
27+
-v $SCRIPT_DIR/../manifests:/manifests \
28+
-v $SCRIPT_DIR/../cluster:/cluster \
29+
--entrypoint /bin/bash \
30+
-e 'EKS_CLUSTER_NAME' -e 'AWS_REGION' \
31+
$aws_credential_args $container_image -c "$shell_command"

hack/lib/generate-aws-creds.sh

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
1-
echo "Generating temporary AWS credentials..."
1+
aws_credential_args=""
22

3-
ACCESS_VARS=$(aws sts assume-role --role-arn $ASSUME_ROLE --role-session-name ${EKS_CLUSTER_NAME}-shell --output json | jq -r '.Credentials | "export AWS_ACCESS_KEY_ID=\(.AccessKeyId) AWS_SECRET_ACCESS_KEY=\(.SecretAccessKey) AWS_SESSION_TOKEN=\(.SessionToken)"')
3+
ASSUME_ROLE=${ASSUME_ROLE:-""}
4+
AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID:-""}
45

5-
# TODO: This should probably not use eval
6-
eval "$ACCESS_VARS"
6+
if [ ! -z "$AWS_ACCESS_KEY_ID" ]; then
7+
echo "Using environment AWS_ACCESS_KEY_ID/AWS_SECRET_ACCESS_KEY"
8+
9+
aws_credential_args="-e AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID -e AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY -e AWS_SESSION_TOKEN=$AWS_SESSION_TOKEN"
10+
elif [ ! -z "$ASSUME_ROLE" ]; then
11+
echo "Generating temporary AWS credentials..."
12+
13+
ACCESS_VARS=$(aws sts assume-role --role-arn $ASSUME_ROLE --role-session-name ${EKS_CLUSTER_NAME}-shell --output json | jq -r '.Credentials | "export AWS_ACCESS_KEY_ID=\(.AccessKeyId) AWS_SECRET_ACCESS_KEY=\(.SecretAccessKey) AWS_SESSION_TOKEN=\(.SessionToken)"')
14+
15+
# TODO: This should probably not use eval
16+
eval "$ACCESS_VARS"
17+
18+
aws_credential_args="-e AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID -e AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY -e AWS_SESSION_TOKEN=$AWS_SESSION_TOKEN"
19+
else
20+
echo "Inheriting credentials from instance profile"
21+
fi

hack/refresh-terraform-lock.sh

Lines changed: 0 additions & 13 deletions
This file was deleted.

hack/run-tests.sh

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ set -u
1010
# You can run script with finch like CONTAINER_CLI=finch ./run-tests.sh <terraform_context> <module>
1111
CONTAINER_CLI=${CONTAINER_CLI:-docker}
1212

13-
# Right now the container images are only designed for amd64
14-
export DOCKER_DEFAULT_PLATFORM=linux/amd64
15-
1613
AWS_EKS_WORKSHOP_TEST_FLAGS=${AWS_EKS_WORKSHOP_TEST_FLAGS:-""}
1714

1815
if [[ "$module" == '-' && "$glob" == '-' ]]; then
@@ -40,15 +37,7 @@ container_image='eks-workshop-test'
4037

4138
(cd $SCRIPT_DIR/../test && $CONTAINER_CLI build -q -t $container_image .)
4239

43-
aws_credential_args=""
44-
45-
ASSUME_ROLE=${ASSUME_ROLE:-""}
46-
47-
if [ ! -z "$ASSUME_ROLE" ]; then
48-
source $SCRIPT_DIR/lib/generate-aws-creds.sh
49-
50-
aws_credential_args="-e AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID -e AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY -e AWS_SESSION_TOKEN=$AWS_SESSION_TOKEN"
51-
fi
40+
source $SCRIPT_DIR/lib/generate-aws-creds.sh
5241

5342
BACKGROUND=${BACKGROUND:-""}
5443

hack/shell.sh

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,12 @@
22

33
environment=$1
44
shell_command=$2
5-
shell_simple_command=$3
65

76
set -Eeuo pipefail
87

98
# You can run script with finch like CONTAINER_CLI=finch ./shell.sh <terraform_context> <shell_command>
109
CONTAINER_CLI=${CONTAINER_CLI:-docker}
1110

12-
# Right now the container images are only designed for amd64
13-
export DOCKER_DEFAULT_PLATFORM=linux/amd64
14-
15-
AWS_DEFAULT_REGION=${AWS_DEFAULT_REGION:-""}
16-
17-
if [ ! -z "$AWS_DEFAULT_REGION" ]; then
18-
echo "Error: AWS_DEFAULT_REGION must be set"
19-
exit 1
20-
fi
21-
2211
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
2312

2413
source $SCRIPT_DIR/lib/common-env.sh
@@ -29,25 +18,10 @@ container_image='eks-workshop-environment'
2918

3019
(cd $SCRIPT_DIR/../lab && $CONTAINER_CLI build -q -t $container_image .)
3120

32-
aws_credential_args=""
33-
34-
ASSUME_ROLE=${ASSUME_ROLE:-""}
35-
36-
if [ ! -z "$ASSUME_ROLE" ]; then
37-
source $SCRIPT_DIR/lib/generate-aws-creds.sh
38-
39-
aws_credential_args="-e AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID -e AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY -e AWS_SESSION_TOKEN=$AWS_SESSION_TOKEN"
40-
fi
41-
42-
command_args=""
21+
source $SCRIPT_DIR/lib/generate-aws-creds.sh
4322

4423
interactive_args=""
4524

46-
if [ ! -z "$shell_simple_command" ]; then
47-
export EKS_CLUSTER_NAME=''
48-
shell_command="$shell_simple_command"
49-
fi
50-
5125
if [ -z "$shell_command" ]; then
5226
echo "Starting shell in container..."
5327
interactive_args="-it"

0 commit comments

Comments
 (0)