You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jan 14, 2025. It is now read-only.
Copy file name to clipboardExpand all lines: DevOps/1_ServerlessApplicationModel/README.md
+30-36Lines changed: 30 additions & 36 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,6 @@
1
1
# Module 1: Serverless Application Model
2
2
3
-
In this module you'll learn about the [Serverless Application Model (SAM)](https://github.com/awslabs/serverless-application-model) and how you can use it to define a serverless RESTful API. You will also use [SAM Local](http://docs.aws.amazon.com/lambda/latest/dg/test-sam-local.html) to locally develop and rapidly test an API.
4
-
5
-
3
+
In this module you'll learn about the [Serverless Application Model (SAM)](https://github.com/awslabs/serverless-application-model) and how you can use it to define a serverless RESTful API. You will also use [SAM CLI](http://docs.aws.amazon.com/lambda/latest/dg/test-sam-local.html) to locally develop and rapidly test an API.
6
4
7
5
## Serverless Application Model (SAM) Overview
8
6
@@ -18,7 +16,6 @@ The Unicorn API includes Amazon API Gateway HTTP endpoints that trigger AWS Lamb
18
16
19
17
In this module, you will be working with a Lambda function that simply displays a welcome message. The Unicorn API components are defined in the [template.yml](uni-api/template.yml) SAM template. Next we'll review the Lambda function component in more detail.
20
18
21
-
22
19
### AWS::Serverless::Function
23
20
24
21
Below is a code snippet from the SAM template to list Unicorns:
@@ -116,51 +113,50 @@ git fetch --all
116
113
git reset --hard origin/master
117
114
```
118
115
116
+
## AWS SAM CLI
119
117
120
-
## SAM Local
121
-
122
-
[SAM Local](http://docs.aws.amazon.com/lambda/latest/dg/test-sam-local.html) is the AWS CLI tool for managing Serverless applications written with [Serverless Application Model (SAM)](https://github.com/awslabs/serverless-application-model). SAM Local can be used to test functions locally, start a local API Gateway from a SAM template, validate a SAM template, and generate sample payloads for various event sources.
118
+
[AWS SAM CLI](https://docs.aws.amazon.com/lambda/latest/dg/test-sam-cli.html) is the AWS CLI tool for managing Serverless applications written with [Serverless Application Model (SAM)](https://github.com/awslabs/serverless-application-model). SAM CLI can be used to test functions locally, start a local API Gateway from a SAM template, validate a SAM template, and generate sample payloads for various event sources.
123
119
124
120
### Installation
125
121
126
-
To complete this module, you will need to install SAM Local. If you already have SAM Local installed, you can skip this section.
122
+
To complete this module, you will need to install SAM CLI. If you already have SAM CLI installed, you can skip this section.
127
123
128
124
#### Prerequisites
129
125
130
-
Running Serverless projects and functions locally with SAM Local requires Docker to be installed and running. SAM Local will use the `DOCKER_HOST` environment variable to contact the docker daemon.
126
+
Running Serverless projects and functions locally with SAM CLI requires Docker to be installed and running. SAM CLI will use the `DOCKER_HOST` environment variable to contact the docker daemon.
127
+
128
+
* macOS: [Docker for Mac](https://store.docker.com/editions/community/docker-ce-desktop-mac)
For macOS and Windows users: SAM CLI requires that the project directory (or any parent directory) is listed in Docker file sharing options.
135
133
136
-
For macOS and Windows users: SAM local requires that the project directory (or any parent directory) is listed in Docker file sharing options.
134
+
Verify that docker is working, and that you can run docker commands from the CLI (e.g. `docker ps`). You do not need to install/fetch/pull any containers - SAM CLI will do it automatically as required.
137
135
138
-
Verify that docker is working, and that you can run docker commands from the CLI (e.g. `docker ps`). You do not need to install/fetch/pull any containers - SAM Local will do it automatically as required.
139
136
137
+
#### Windows, Linux, macOS with pip [Recommended]
140
138
141
-
#### Windows, Linux, macOS with NPM [Recommended]
139
+
The easiest way to install **`sam`** is to use [pip](https://pypi.org/project/pip/).
142
140
143
-
The easiest way to install **`sam`** is to use [NPM](https://www.npmjs.com).
141
+
To use pip, you must have [Python](https://www.python.org/) installed and added to your system's Environment path.
144
142
145
143
```bash
146
-
npm install -g aws-sam-local
144
+
pip install aws-sam-cli
147
145
```
148
146
149
-
If you get a permission error when using npm (such as `EACCES: permission denied`), please see the instructions on this page of the NPM documentation: [https://docs.npmjs.com/getting-started/fixing-npm-permissions](https://docs.npmjs.com/getting-started/fixing-npm-permissions).
150
-
151
147
Verify the installation worked:
148
+
152
149
```bash
153
150
sam --version
154
151
```
155
152
156
153
#### Binary release
157
154
158
-
We also release the CLI as binaries that you can download and instantly use. You can find them under [Releases](https://github.com/awslabs/aws-sam-local/releases) in the SAM Local repo.
159
-
155
+
We also release the CLI as binaries that you can download and instantly use. You can find them under [Releases](https://github.com/awslabs/aws-sam-cli/releases) in the SAM CLI repo.
160
156
161
157
#### Alternative Installation: Amazon Linux (EC2)
162
158
163
-
If you're unable to install SAM Local on your workstation, you may find it easier to use SAM Local on an Amazon Linux EC2 instance. In this case, you will not be performing work locally on your laptop, instead you will connect remotely into an EC2 instance to perform editing and testing.
159
+
If you're unable to install SAM CLI on your workstation, you may find it easier to use SAM CLI on an Amazon Linux EC2 instance. In this case, you will not be performing work locally on your laptop, instead you will connect remotely into an EC2 instance to perform editing and testing.
164
160
165
161
1. [Create a keypair](http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-key-pairs.html), if you do not have one already.
166
162
@@ -182,19 +178,19 @@ If you're unable to install SAM Local on your workstation, you may find it easie
182
178
183
179
1. The Git repository has been cloned to the home directory on launch, `/home/ec2-user/uni-api`.
184
180
185
-
###### Setup Port Forwarding Configuration
181
+
##### Setup Port Forwarding Configuration
186
182
187
-
SAM Local can start an HTTP server locally on EC2 instance on port 3000. In order to view content on that HTTP server through the browser on your laptop, you need to configure port forwarding.
183
+
SAM CLI can start an HTTP server locally on EC2 instance on port 3000. In order to view content on that HTTP server through the browser on your laptop, you need to configure port forwarding.
188
184
189
-
###### Port Forwarding on MacOS
185
+
##### Port Forwarding on MacOS
190
186
191
187
On your workstation, open a new terminal and run the following command. In the command, replace ***~/mykeypair.pem*** with the location and file name of your .pem file and replace ***ec2-###-##-##-###.compute-1.amazonaws.com*** with the public DNS name of your EC2 developer instance:
In this section, you will use SAM Local on your workstation to run the Unicorn API and test changes to it without having to deploy to AWS.
227
+
In this section, you will use SAM CLI on your workstation to run the Unicorn API and test changes to it without having to deploy to AWS.
234
228
235
229
### Run API Gateway locally
236
230
@@ -248,17 +242,17 @@ In this section, you will use SAM Local on your workstation to run the Unicorn A
248
242
249
243
This will spawn a local API Gateway to test HTTP request/response functionality. Features hot-reloading to allow you to quickly develop, and iterate over your functions. **`sam`** will automatically find any functions within your SAM template that have `Api` event sources defined, and mount them at the defined HTTP paths.
250
244
251
-
If this is your first time running SAM Local, there will be a delay as the Docker images are downloaded. Once successful, you should see output similar to the screenshot below:
245
+
If this is your first time running SAM CLI, there will be a delay as the Docker images are downloaded. Once successful, you should see output similar to the screenshot below:
252
246
253
247

254
248
255
249
1. Open a browser and enter `http://127.0.0.1:3000/unicorns` in the address bar. Confirm that you see the output below:
256
250
257
251

258
252
259
-
Congratulations! You have successfully used SAM Local to start a local development environment. Keep the window open that is running the `sam local start-api` command until this module is complete.
253
+
Congratulations! You have successfully used SAM CLI to start a local development environment. Keep the window open that is running the `sam local start-api` command until this module is complete.
260
254
261
-
Next, let's look at how you can use SAM Local to test changes to your API.
255
+
Next, let's look at how you can use SAM CLI to test changes to your API.
262
256
263
257
### Test code changes locally
264
258
@@ -279,9 +273,9 @@ HOW TO update code and verify results (expand for details)
279
273
</details>
280
274
<p>
281
275
282
-
Congratulations! You've successfully test changes to a Lambda function in a local development environment. You may now kill the SAM Local running process.
276
+
Congratulations! You've successfully test changes to a Lambda function in a local development environment. You may now kill the SAM CLI running process.
283
277
284
-
To learn more about development with SAM Local, check out [AWS Documentation](http://docs.aws.amazon.com/lambda/latest/dg/test-sam-local.html) and SAM Local[GitHub repository](https://github.com/awslabs/aws-sam-local).
278
+
To learn more about development with SAM CLI, check out [AWS Documentation](http://docs.aws.amazon.com/lambda/latest/dg/test-sam-local.html) and SAM CLI[GitHub repository](https://github.com/awslabs/aws-sam-local).
0 commit comments