Skip to content
This repository was archived by the owner on Nov 4, 2022. It is now read-only.

Commit efe632c

Browse files
committed
Minor updates and cleanup
What ----- - Minor cleanup to workshop instructions - Update cleanup script so that it waits for stacks to delete before moving to the next one
1 parent 82fe67c commit efe632c

File tree

13 files changed

+133
-53
lines changed

13 files changed

+133
-53
lines changed

workshop/content/buildpipe/done/_index.en.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,19 @@ CodeBuild created the `sam-app-dev` stack during the `DeployTest` Pipeline step.
3434
CodeBuild created `sam-app-prod` during the `DeployProd` step.
3535

3636
Look at the `Outputs` tab for each of these CloudFormation stacks to see the API endpoints. You can
37-
use `curl` or other methods to verify the functionality of your two new APIs.
37+
use `curl` or other methods to verify the functionality of your two new APIs. You can export the URL
38+
endpoints for both stages in a terminal.
39+
40+
```bash
41+
export DEV_ENDPOINT=$(aws cloudformation describe-stacks --stack-name sam-app-dev | jq -r '.Stacks[].Outputs[].OutputValue | select(startswith("https://"))')
42+
export PROD_ENDPOINT=$(aws cloudformation describe-stacks --stack-name sam-app-prod | jq -r '.Stacks[].Outputs[].OutputValue | select(startswith("https://"))')
43+
44+
echo "Dev endpoint: $DEV_ENDPOINT"
45+
echo "Prod endpoint: $PROD_ENDPOINT"
46+
47+
curl -s $DEV_ENDPOINT
48+
curl -s $PROD_ENDPOINT
49+
```
3850

3951
![API endpoints](/images/chapter4-pipelines/sam-app-dev-cfn-outputs.png)
4052

workshop/content/buildpipe/gitpush/_index.en.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ Branch 'main' set up to track remote branch 'main' from 'origin'.
5050

5151
### Verify in CodeCommit
5252

53-
Navigate to the [AWS CodeCommit console](https://console.aws.amazon.com/codesuite/codecommit/home), find your _sam-app_ repository and click on it to view its contents. Make sure your code is there. You should see a screen like the following:
53+
Navigate to the [AWS CodeCommit console](https://console.aws.amazon.com/codesuite/codecommit/home),
54+
find your `sam-app` repository and click on it to view its contents. Make sure your code is there.
55+
You should see a screen like the following:
5456

5557
![VerifyCodeCommit](/images/screenshot-verify-codecommit.png)
58+
59+
#### Now that you repo has your code let's start building a CI/CD pipeline!

workshop/content/buildpipe/howto/_index.en.md

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ weight = 20
66

77
The best way to automate the creation of CI/CD pipelines is by provisioning them programmatically
88
using Infrastructure as Code (IaC). This is useful in a microservices environment, where you may have
9-
a pipeline per microservice. In such environments there could be dozens, or even hundreds,
9+
a pipeline per service. In such environments there could be dozens, or even hundreds,
1010
of CI/CD pipelines. Having an automated way to create those CI/CD pipelines enables developers move quickly
1111
without the burden of building them manually. SAM Pipelines, which you'll be using, is a tool to
1212
ease that burden.
@@ -30,8 +30,6 @@ test our serverless application locally. AWS SAM is a toolset meant to increase
3030
developing serverless applications and provides capabilities such as `sam local` that are not
3131
present in other IaC tools.
3232

33-
In this section you will be using another feature from SAM to create a CI/CD pipeline.
34-
3533
### Introducing AWS SAM Pipelines
3634

3735
[**SAM Pipelines**](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-cli-command-reference-sam-pipeline-bootstrap.html)
@@ -49,14 +47,14 @@ As of this writing, SAM Pipelines can bootstrap CI/CD pipelines for the followin
4947
{{% notice note %}}
5048
SAM Pipelines is a feature which bootstraps CI/CD pipelines for the listed providers. This saves
5149
saves you the work of setting them up from scratch. However, you can use SAM as a deployment tool
52-
with _any_ CI/CD provider. You use the `sam build` and `sam deploy` commands to build and deploy SAM
50+
with _any_ CI/CD provider. You use various `sam` commands to build and deploy SAM
5351
applications regardless of your CI/CD toolset. Furthermore, the configurations SAM Pipelines creates
54-
are a convienence to get you started quickly. You are free to edit these CI/CD configuration
52+
are a convienence to get you started. You are free to edit these CI/CD configuration
5553
files after SAM creates them.
5654
{{% /notice %}}
5755

5856
SAM Pipelines creates appropriate configuration files for your CI/CD provider of choice. For
59-
exampoe, when using
57+
example, when using
6058
GitHub Actions SAM will synthesize a `.github/workflows/pipeline.yaml` file. This file defines your CI/CD
6159
pipeline using GitHub Actions. In this workshop we will be using AWS CodePipeline. As you will soon
6260
see, SAM creates multiple files, one of which is a CloudFormation template named
@@ -68,11 +66,11 @@ serverless application automatically.
6866
At the end of this section we will have a self-updating CI/CD pipeline using CodePipeline that will
6967
perform the following steps.
7068

71-
1. Trigger after a commit to the `main` branch
72-
1. Look for changes to the pipeline itself, and self-update using CloudFormation
73-
1. Run unit tests via CodeBuild
74-
1. Build and package the application code via CodeBuild
75-
1. Deploy to a dev/test environment
76-
1. Deploy to a production environment
69+
1. Trigger after a commit to the `main` branch (`Source` in the screenshot below)
70+
1. Look for changes to the pipeline itself, and self-update using CloudFormation (`UpdtatePipeline`)
71+
1. Run unit tests via CodeBuild (`UnitTest`)
72+
1. Build and package the application code via CodeBuild (`BuildAndPackage`)
73+
1. Deploy to a dev/test environment (`DeployTest`)
74+
1. Deploy to a production environment (`DeployProd`)
7775

7876
![](/images/sam-pipeline-architecture.png)

workshop/content/buildpipe/sampipeinit/_index.en.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ cd ~/environment/sam-app
2929
sam pipeline init --bootstrap
3030
```
3131

32-
A list of the questions and required anwers for this workshop is enumerated below. Note that numbers
33-
may be different when choosing from an enumerated list. The full output and answers are provided
32+
A list of the questions and required anwers for this workshop is enumerated below. **Note that numbers
33+
may be different when choosing from an enumerated list.** The full output and answers are provided
3434
below as an additional reference.
3535

3636
1. Select a pipeline template to get started: `AWS Quick Start Pipeline Templates` (1)

workshop/content/canaries/add-error/_index.en.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ exports.lambdaHandler = async (event, context) => {
7171

7272
`~/environment/sam-app/hello_world/app.py`
7373

74-
```python
74+
```python {hl_lines=["3-4"]}
7575
def lambda_handler(event, context):
7676

7777
if True:
@@ -100,6 +100,7 @@ def test_lambda_handler(apigw_event, mocker):
100100
In the terminal, run the following commands from the root directory of your `sam-app` project.
101101

102102
```
103+
cd ~/environment/sam-app
103104
git add .
104105
git commit -m "Breaking the lambda function on purpose"
105106
git push
@@ -111,14 +112,15 @@ Once you've pushed the code you will need to generate traffic on your production
111112
endpoint. **If you don't generate traffic for your Lambda function the CloudWatch alarm will not be
112113
triggered!**
113114

114-
Fetch the API Gateway endpoint for your production deployment. This command will export the
115-
production URL to the environment variable `PROD_ENDPOINT`.
115+
{{%expand "If you haven't exported the PROD_ENDPOINT, run the following command." %}}
116116

117117
```bash
118118
export PROD_ENDPOINT=$(aws cloudformation describe-stacks --stack-name sam-app-prod | jq -r '.Stacks[].Outputs[].OutputValue | select(startswith("https://"))')
119119
echo "$PROD_ENDPOINT"
120120
```
121121

122+
{{% /expand%}}
123+
122124
Start a `watch` command which will hit this endpoint twice per second.
123125

124126
```bash

workshop/content/canaries/codedeploy/_index.en.md

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Remember to update the unit tests!
2020

2121
`~/environment/sam-app/hello-world/app.js`
2222

23-
```javascript
23+
```javascript {hl_lines=["4"]}
2424
response = {
2525
statusCode: 200,
2626
body: JSON.stringify({
@@ -31,7 +31,7 @@ response = {
3131

3232
`~/environment/node-sam-app/hello-world/tests/unit/test-handler.js `
3333

34-
```javascript
34+
```javascript {hl_lines=["12"]}
3535
describe("Tests index", function () {
3636
it("verifies successful response", async () => {
3737
const result = await app.lambdaHandler(event, context)
@@ -54,7 +54,7 @@ describe("Tests index", function () {
5454

5555
`~/environment/sam-app/hello_world/app.py `
5656

57-
```python
57+
```python {hl_lines=["4"]}
5858
return {
5959
"statusCode": 200,
6060
"body": json.dumps({
@@ -65,7 +65,7 @@ return {
6565

6666
`~/environment/sam-app/tests/unit/test_handler.py`
6767

68-
```python
68+
```python {hl_lines=["7"]}
6969
def test_lambda_handler(apigw_event, mocker):
7070
ret = app.lambda_handler(apigw_event, "")
7171
data = json.loads(ret["body"])
@@ -96,16 +96,21 @@ completes.
9696
First, get the `https` endpoint of your dev stage. In a terminal on your Cloud9 enviroment run the
9797
following command.
9898

99+
{{%expand "Export the HTTP endpoints if you haven't already" %}}
100+
99101
```bash
100-
aws cloudformation describe-stacks --stack-name sam-app-dev | jq -r '.Stacks[].Outputs[].OutputValue | select(startswith("https://"))'
102+
export DEV_ENDPOINT=$(aws cloudformation describe-stacks --stack-name sam-app-dev | jq -r '.Stacks[].Outputs[].OutputValue | select(startswith("https://"))')
103+
export PROD_ENDPOINT=$(aws cloudformation describe-stacks --stack-name sam-app-prod | jq -r '.Stacks[].Outputs[].OutputValue | select(startswith("https://"))')
101104
```
102105

103-
Copy the url returned by the previous command run the following `watch` command. This will hit your
104-
API endpoint every second and print the return value to the screen. This command will also append
105-
the output to the `outputs.txt` file. You can run this command from any directory.
106+
{{% /expand%}}
107+
108+
Hit your `dev` API endpoint every second and print the return value to the screen. This command will
109+
also append the output to the `outputs.txt` file that you can inspect later. You can run this
110+
command from any directory.
106111

107112
```bash
108-
watch -n 1 "curl -s https://123123123.execute-api.us-east-2.amazonaws.com/Prod/hello/ | jq '.message' 2>&1 | tee -a outputs.txt"
113+
watch -n 1 "curl -s $DEV_ENDPOINT | jq '.message' 2>&1 | tee -a outputs.txt"
109114
```
110115

111116
You should see `Hello my friend` in the terminal. Now that your script is logging the API output turn
@@ -117,7 +122,8 @@ the `In Progress` status navigate to the CodeDeploy console.
117122
![CanaryCodeDeploy](/images/screenshot-canary-codedeploy-00.png)
118123

119124
In the CodeDeploy console click on `Deployments`. You should see your deployment `In progress`. If
120-
you do not see a deployment, click the refresh icon. Click on the Deployment Id to see the details.
125+
you do not see a deployment, click the refresh icon. **This may take a few minutes to show up!**
126+
Click on the Deployment Id to see the details.
121127

122128
![CanaryCodeDeploy](/images/screenshot-canary-codedeploy-0.png)
123129

@@ -130,6 +136,8 @@ ellapsed. In this case we specified the interval to be 5 minutes.
130136
When you are in this stage, take a look at your terminal where you started the `watch` command. You
131137
will see the message occasionally flash to `I'm using canary deployments`.
132138

139+
![CanaryDeploymentMessages](/images/code-pipeline-canary.gif)
140+
133141
After five minutes CodeDeploy will shift the remaining traffic to the new version and the deployment
134142
will be done:
135143

workshop/content/canaries/sam/_index.en.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ with your `template.yaml` file.
1212
{{< tabs >}}
1313
{{% tab name="Node" %}}
1414

15+
`~environment/sam-app/template.yaml`
16+
1517
```yaml {linenos=true,hl_lines=["2-4"],linenostart=20}
1618
Runtime: nodejs14.x
1719
AutoPublishAlias: live
@@ -24,6 +26,8 @@ Architectures:
2426
{{% /tab %}}
2527
{{% tab name="python" %}}
2628
29+
`~environment/sam-app/template.yaml`
30+
2731
```yaml {linenos=true,hl_lines=["2-4"],linenostart=20}
2832
Runtime: python3.7
2933
AutoPublishAlias: live

workshop/content/cleanup/delete-cf-stacks.en.md

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,17 @@ need to make adjustments. The cleanup script in its entirety is shown below.
1919
```bash
2020
#!/bin/bash
2121

22-
ROLE_NAME=Cloud9-MyAwesomeAdmin
22+
ROLE_NAME=Cloud9-AwesomeAdmin
23+
24+
# Function to get the status of a CFN template
25+
get_stack_status () {
26+
status=$(aws cloudformation describe-stacks --stack-name $1)
27+
if [ $? -eq 0 ]; then
28+
echo "$status" | jq -r ".Stacks[].StackStatus"
29+
else
30+
echo "DELETE_COMPLETE"
31+
fi
32+
}
2333

2434
cat > cfn-policy.json<< EOF
2535
{
@@ -42,18 +52,32 @@ ACCOUNT_ID=$(aws sts get-caller-identity --query "Account" --output text)
4252
## Create a temporary Admin role
4353
aws iam create-role --role-name "$ROLE_NAME" --assume-role-policy-document file://cfn-policy.json
4454
aws iam attach-role-policy --role-name "$ROLE_NAME" --policy-arn "arn:aws:iam::aws:policy/AdministratorAccess"
55+
rm cfn-policy.json
4556

4657
## Wait a bit for this role to stick. It cannot be used immediately after creation.
47-
sleep 15
58+
sleep 10
4859

4960
## Delete stacks
5061
stacks=(sam-app-prod sam-app-dev sam-app-pipeline aws-sam-cli-managed-prod-pipeline-resources aws-sam-cli-managed-dev-pipeline-resources)
5162

52-
for name in "${stacks[@]}"
63+
for stack_name in "${stacks[@]}"
5364
do
54-
echo "Deleting stack: $name..."
55-
aws cloudformation delete-stack --role-arn "arn:aws:iam::$ACCOUNT_ID:role/$ROLE_NAME" --stack-name "$name"
56-
sleep 10
65+
echo "Deleting stack: $stack_name..."
66+
67+
aws cloudformation delete-stack \
68+
--role-arn "arn:aws:iam::$ACCOUNT_ID:role/$ROLE_NAME" \
69+
--stack-name "$stack_name"
70+
71+
status="$(get_stack_status $stack_name)"
72+
73+
# Wait until the stack is actually deleted
74+
while [ "$status" != "DELETE_COMPLETE" ]
75+
do
76+
sleep 5
77+
status="$(get_stack_status $stack_name)"
78+
echo "$stack_name: $status"
79+
done
80+
5781
done
5882

5983
## Delete the temporary Admin role

workshop/content/local/change/_index.en.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ simple change. Change the response message to return `hello my friend` instead o
1313

1414
`~environment/sam-app/hello-world/app.js`
1515

16-
```js
16+
```js {hl_lines=["8"]}
1717
let response
1818

1919
exports.lambdaHandler = async (event, context) => {
@@ -37,7 +37,7 @@ exports.lambdaHandler = async (event, context) => {
3737
{{% tab name="python" %}}
3838
`~environment/sam-app/hello_world/app.py`
3939

40-
```python
40+
```python {hl_lines=["7"]}
4141
import json
4242

4343
def lambda_handler(event, context):

workshop/content/sam/code/_index.en.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@ date = 2019-10-02T15:21:26-07:00
44
weight = 15
55
+++
66

7-
{{% notice note %}}
8-
If you consider yourself an expert using Lambda functions, you can probably skip this page.
9-
{{% /notice%}}
10-
117
Let's take a look at the code of the Hello World Lambda function.
128

13-
**Note** that your function may have additional commented out code, those lines have been removed
14-
from the following example for clarity.
9+
{{% notice note %}}
10+
Your function may have additional comments. Those lines have been removed from the following example
11+
for clarity.
12+
{{% /notice%}}
1513

1614
{{< tabs >}}
1715
{{% tab name="Node" %}}

workshop/content/sam/init/_index.en.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,14 @@ Which runtime would you like to use?
5959
14 - ruby2.7
6060
```
6161

62-
- Node: `nodejs14.x`
63-
- Python: `python3.7`
64-
- Java: `python3.7`
65-
- C#: `dotnet6`
62+
{{< tabs >}}
63+
{{% tab name="Node" %}}
64+
`nodejs14.x`
65+
{{% /tab %}}
66+
{{% tab name="python" %}}
67+
`python3.7`
68+
{{% /tab %}}
69+
{{% /tabs %}}
6670

6771
Select `Zip` as the package type and leave `sam-app` as the `Project name`.
6872

0 commit comments

Comments
 (0)