Skip to content

Commit aca95b2

Browse files
committed
Fix indentation
1 parent f98ca2f commit aca95b2

File tree

1 file changed

+59
-59
lines changed

1 file changed

+59
-59
lines changed

README.md

Lines changed: 59 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -13,112 +13,113 @@ GET '/contents': This requires a valid jwt token, and returns the un-encrpyted c
1313
### Run the Api using Flask Server
1414
1. Install python dependencies. These dependencies are kept in a requirements.txt file. To install them, use pip:
1515

16-
```bash
16+
```bash
1717
pip install -r requirements.txt
18-
```
18+
```
1919

20-
2. Setting up environment
20+
1. Setting up environment
2121

22-
The following environment variable is required:
22+
The following environment variable is required:
2323

24-
**JWT_SECRET** - The secret used to make the JWT token, for the purpose of this course it can be any string.
24+
**JWT_SECRET** - The secret used to make the JWT token, for the purpose of this course it can be any string.
2525

26-
The following environment variable is optional:
26+
The following environment variable is optional:
2727

28-
**LOG_LEVEL** - The level of logging. Will default to 'INFO', but when debugging an app locally, you may want to set it to 'DEBUG'
28+
**LOG_LEVEL** - The level of logging. Will default to 'INFO', but when debugging an app locally, you may want to set it to 'DEBUG'
2929

30-
```bash
30+
```bash
3131
export JWT_SECRET=myjwtsecret
3232
export LOG_LEVEL=DEBUG
3333
```
3434

3535
3. Run the app using the Flask server, from the flask-app directory, run:
36-
```bash
36+
```bash
3737
python app/main.py
3838
```
3939

40-
To try the api endpoints, open a new shell and run, replacing '\<EMAIL\>' and '\<PASSWORD\>' with and any values:
40+
To try the api endpoints, open a new shell and run, replacing '\<EMAIL\>' and '\<PASSWORD\>' with and any values:
4141

42-
```bash
42+
```bash
4343
export TOKEN=`curl -d '{"email":"<EMAIL>","password":"<PASSWORD>"}' -H "Content-Type: application/json" -X POST localhost:80/auth | jq -r '.token'`
4444
```
4545

46-
This calls the endpoint 'localhost:80/auth' with the '{"email":"<EMAIL>","password":"<PASSWORD>"}' as the message body. The return value is a jwt token based on the secret you supplied. We are assigning that secret to the environment variable 'TOKEN'. To see the jwt token, run:
46+
This calls the endpoint 'localhost:80/auth' with the '{"email":"<EMAIL>","password":"<PASSWORD>"}' as the message body. The return value is a jwt token based on the secret you supplied. We are assigning that secret to the environment variable 'TOKEN'. To see the jwt token, run:
4747

48-
```bash
48+
```bash
4949
echo $TOKEN
5050
```
51-
To call the 'contents' endpoint, which decrpyts the token and returns it content, run:
51+
To call the 'contents' endpoint, which decrpyts the token and returns it content, run:
5252

53-
```bash
53+
```bash
5454
curl --request GET 'http://127.0.0.1:80/contents' -H "Authorization: Bearer ${TOKEN}" | jq .
5555
```
56-
You should see the email that you passed in as one of the values.
56+
You should see the email that you passed in as one of the values.
5757

5858
### Dockerize and Run Locally
5959

6060
1. Install Docker: [installation instructions](https://docs.docker.com/install/)
6161

62-
2. Create a Docker file. A Docker file decribes how to build a Docker image. Create a file named 'Dockerfile' in the app repo. The contents of the file describe the steps in creating a Docker image. Your Dockerfile should:
62+
2. Create a Docker file. A Docker file decribes how to build a Docker image. Create a file named 'Dockerfile' in the app repo. The contents of the file describe the steps in creating a Docker image. Your Dockerfile should:
6363
- use the 'python:strech' image as a source image
6464
- Setup an app directory for your code
6565
- Install needed python requirements
6666
- Define an entrypoint which will run the main app using the gunicorn WSGI server
6767

68-
gunicorn should be run with the arguments:
68+
gunicorn should be run with the arguments:
6969

70-
```
70+
```
7171
gunicorn -b :8080 main:APP
7272
```
7373

7474

7575
3. Create a file named 'env_file' and use it to set the environment variables which will be run locally in your container. Here we do not need the export command, just an equals sign:
7676

7777

78-
\<VARIABLE-NAME\>=\<VARIABLE-VALUE\>
78+
\<VARIABLE-NAME\>=\<VARIABLE-VALUE\>
7979

8080
4. Build a Local Docker Image
81-
To build a Docker image run:
81+
To build a Docker image run:
8282
```
8383
docker build -t jwt-api-test .
8484
```
8585

8686
5. Run the image locally, using the 'gunicorn' server:
87-
```
87+
```
8888
docker run --env-file=env_file -p 80:8080 jwt-api-test
8989
```
9090
91-
To use the endpoints use the same curl commands as before:
91+
To use the endpoints use the same curl commands as before:
9292
93-
```bash
93+
```bash
9494
export TOKEN=`curl -d '{"email":"<EMAIL>","password":"<PASSWORD>"}' -H "Content-Type: application/json" -X POST localhost:80/auth | jq -r '.token'`
95-
96-
curl --request GET 'http://127.0.0.1:80/contents' -H "Authorization: Bearer ${TOKEN}" | jq .
9795
```
96+
```bash
97+
curl --request GET 'http://127.0.0.1:80/contents' -H "Authorization: Bearer ${TOKEN}" | jq .
98+
```
9899

99100
## Deployment to Kubernetes using CodePipeline and CodeBuild
100101

101-
### Deploy a Kubernetes Cluster
102+
### Create a Kubernetes (EKS) Cluster
102103

103104
1. Install aws cli
104105

105-
```bash
106+
```bash
106107
pip install awscli --upgrade --user
107108
```
108109
109-
Note: If you are using a Python virtual environment, the command will be:
110+
Note: If you are using a Python virtual environment, the command will be:
110111
111-
```bash
112+
```bash
112113
pip install awscli --upgrade
113114
```
114115

115116
2.
116117
[Generate a aws access key id and secret key](https://docs.aws.amazon.com/general/latest/gr/aws-sec-cred-types.html#access-keys-and-secret-access-keys)
117118

118119
3. Setup your environment to use these keys:
119-
If you not already have a aws 'credentials' file setup, run:
120+
If you not already have a aws 'credentials' file setup, run:
120121

121-
```bash
122+
```bash
122123
aws configure
123124
```
124125
And use the credentials you generated in step 2. Your aws commandline tools will now use these credentials.
@@ -128,64 +129,63 @@ And use the credentials you generated in step 2. Your aws commandline tools will
128129
The 'eksctl' tool allow interaction wth a EKS cluster from the command line. To install, follow the [directions for your platform](https://docs.aws.amazon.com/eks/latest/userguide/eksctl.html)
129130
130131
5. Create a EKS cluster
131-
6.
132-
```bash
132+
133+
```bash
133134
eksctl create cluster --name simple-jwt-api --version 1.12 --nodegroup-name standard-workers --nodes 3 --nodes-min 1 --nodes-max 4 --node-ami auto
134135
```
135136

136137
This will take some time to do. Progress can be checked by visiting the aws console and selecting EKS from the services.
137138

138139
6. Check the cluster is ready:
139-
7.
140-
```bash
140+
141+
```bash
141142
kubectl get nodes
142143
```
143144
144145
If the nodes are up and healthy, the cluster should be ready.
145146
146-
7. Create an IAM role that CodeBuild can use to interact with EKS:
147+
### Create Pipeline
148+
You will now create a pipeline which watches your Github. When changes are checked in, it will build a new image and deploy it to your cluster.
147149
148-
```bash
149-
ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)
150150
151-
TRUST="{ \"Version\": \"2012-10-17\", \"Statement\": [ { \"Effect\": \"Allow\", \"Principal\": { \"AWS\": \"arn:aws:iam::${ACCOUNT_ID}:root\" }, \"Action\": \"sts:AssumeRole\" } ] }"
151+
1. Create an IAM role that CodeBuild can use to interact with EKS:
152152
153+
```bash
154+
ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)
155+
TRUST="{ \"Version\": \"2012-10-17\", \"Statement\": [ { \"Effect\": \"Allow\", \"Principal\": { \"AWS\": \"arn:aws:iam::${ACCOUNT_ID}:root\" }, \"Action\": \"sts:AssumeRole\" } ] }"
153156
echo '{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "eks:Describe*", "ssm:GetParameters" ], "Resource": "*" } ] }' > /tmp/iam-role-policy
154-
155157
aws iam create-role --role-name UdacityFlaskDeployCBKubectlRole --assume-role-policy-document "$TRUST" --output text --query 'Role.Arn'
156-
157158
aws iam put-role-policy --role-name UdacityFlaskDeployCBKubectlRole --policy-name eks-describe --policy-document file:///tmp/iam-role-policy
158-
159159
```
160160

161161
You have now created a role named 'UdacityFlaskDeployCBKubectlRole'
162162

163-
8. Grant the role access to the cluster.
163+
1. Grant the role access to the cluster.
164164
The 'aws-auth ConfigMap' is used to grant role based access control to your cluster.
165165

166-
```
166+
```
167167
ROLE=" - rolearn: arn:aws:iam::$ACCOUNT_ID:role/UdacityFlaskDeployCBKubectlRole\n username: build\n groups:\n - system:masters"
168168
kubectl get -n kube-system configmap/aws-auth -o yaml | awk "/mapRoles: \|/{print;print \"$ROLE\";next}1" > /tmp/aws-auth-patch.yml
169169
kubectl patch configmap/aws-auth -n kube-system --patch "$(cat /tmp/aws-auth-patch.yml)"
170170
```
171171
172-
9. Generate a GitHub access token.
173-
A Github acces token will allow CodePipeline to monitor when a repo is changed. A token can be generated [here](https://github.com/settings/tokens/=).
172+
1. Generate a GitHub access token.
173+
A Github acces token will allow CodePipeline to monitor when a repo is changed. A token can be generated [here](https://github.com/settings/tokens/=).
174174
This token should be saved somewhere that is secure.
175175
176-
10. The file buildspec.yml instructs CodeBuild. We need a way to pass your jwt secret to the app in kubernetes securly. You will be using AWS parameter-store to do this. First add the following to your buildspec.yml file:
176+
1. The file *buildspec.yml* instructs CodeBuild. We need a way to pass your jwt secret to the app in kubernetes securly. You will be using AWS parameter-store to do this. First add the following to your buildspec.yml file:
177177
178-
```yaml
178+
```yaml
179179
env:
180180
parameter-store:
181181
JWT_SECRET: JWT_SECRET
182182
```
183183

184-
This lets CodeBuild know to set an evironment variable based on a value in the parameter-store.
184+
This lets CodeBuild know to set an evironment variable based on a value in the parameter-store.
185185

186-
11. Put secret into AWS Parameter Store
186+
1. Put secret into AWS Parameter Store
187187

188-
```
188+
```
189189
aws ssm put-parameter --name JWT_SECRET --value "YourJWTSecret" --type SecureString
190190
```
191191

@@ -199,7 +199,7 @@ aws ssm put-parameter --name JWT_SECRET --value "YourJWTSecret" --type SecureStr
199199

200200
Save this file.
201201

202-
11. Create a stack for CodePipeline
202+
1. Create a stack for CodePipeline
203203
- Go the the [CloudFormation service](https://us-east-2.console.aws.amazon.com/cloudformation/) in the aws console.
204204
- Press the 'Create Stack' button.
205205
- Choose the 'Upload template to S3' option and upload the template file 'ci-cd-codepipeline.cfn.yml'
@@ -209,25 +209,25 @@ aws ssm put-parameter --name JWT_SECRET --value "YourJWTSecret" --type SecureStr
209209
210210
You can check it's status in the [CloudFormation console](https://us-east-2.console.aws.amazon.com/cloudformation/).
211211

212-
15. Check the pipeline works. Once the stack is successfully created, commit a change to the master branch of your github repo. Then, in the aws console go to the [CodePipeline UI](https://us-east-2.console.aws.amazon.com/codesuite/codepipeline). You should see that the build is running.
212+
1. Check the pipeline works. Once the stack is successfully created, commit a change to the master branch of your github repo. Then, in the aws console go to the [CodePipeline UI](https://us-east-2.console.aws.amazon.com/codesuite/codepipeline). You should see that the build is running.
213213

214214
16. To test your api endpoints, get the external ip for your service:
215215

216216

217-
```
217+
```
218218
kubectl get services simple-jwt-api -o wide
219219
```
220220

221-
Now use the external ip url to test the app:
221+
Now use the external ip url to test the app:
222222

223-
```
223+
```
224224
export TOKEN=`curl -d '{"email":"<EMAIL>","password":"<PASSWORD>"}' -H "Content-Type: application/json" -X POST <EXTERNAL-IP URL>:80/auth | jq -r '.token'`
225225
curl --request GET '<EXTERNAL-IP URL>:80/contents' -H "Authorization: Bearer ${TOKEN}" | jq
226226
```
227227
228228
17. Paste the external id from above below this line for the reviewer to use:
229229
230-
**EXTERNAL IP**:
230+
**EXTERNAL IP**:
231231
232232
18. Add running tests as part of the build.
233233
@@ -240,5 +240,5 @@ curl --request GET '<EXTERNAL-IP URL>:80/contents' -H "Authorization: Bearer ${T
240240
- Open the *test_main.py* file
241241
- Add `assert False` to any of the tests
242242
- Commit your code and push it to Github
243-
- Check that the build fails in [CodePipeline]((https://us-east-2.console.aws.amazon.com/codesuite/codepipeline)
243+
- Check that the build fails in [CodePipeline](https://us-east-2.console.aws.amazon.com/codesuite/codepipeline)
244244

0 commit comments

Comments
 (0)