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
Copy file name to clipboardExpand all lines: apigw-rest-api-lambda-python/README.md
+23-20Lines changed: 23 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ This project contains source code and supporting files for a serverless applicat
6
6
- events - Invocation events that you can use to invoke the function.
7
7
- template.yaml - A template that defines the application's AWS resources.
8
8
9
-
The application uses several AWS resources, including Lambda functions and an API Gateway API. These resources are defined in the `template.yaml` file in this project. You can update the template to add AWS resources through the same deployment process that updates your application code.
9
+
The application uses several AWS resources. These resources are defined in the `template.yaml` file in this project. You can update the template to add AWS resources through the same deployment process that updates your application code. This application will deploy a Lambda function, as well as an API Gateway REST API that will be automatically created based on the Lambda function's Event mapping.
10
10
11
11
If you prefer to use an integrated development environment (IDE) to build and test your application, you can use the AWS Toolkit.
12
12
The AWS Toolkit is an open source plug-in for popular IDEs that uses the AWS SAM CLI to build and deploy serverless applications on AWS. The AWS Toolkit also adds a simplified step-through debugging experience for Lambda function code. See the following links to get started.
@@ -29,38 +29,36 @@ The Serverless Application Model Command Line Interface (SAM CLI) is an extensio
29
29
30
30
To use the AWS SAM CLI, you need the following tools.
31
31
32
+
### Requirements
33
+
32
34
* AWS SAM CLI - [Install the SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html)
* Docker - [Install Docker community edition](https://hub.docker.com/search/?type=edition&offering=community)
35
37
36
38
To build and deploy your application for the first time, run the following in your shell:
37
39
38
40
```bash
39
-
sam build --use-container
40
-
sam deploy --guided
41
+
$ cd apig-rest-api-lambda-python
42
+
$ sam build --use-container
43
+
$ sam deploy --guided
41
44
```
42
45
43
46
The first command will build the source of your application. The second command will package and deploy your application to AWS, with a series of prompts:
44
47
45
48
***Stack Name**: The name of the stack to deploy to CloudFormation. This should be unique to your account and region, and a good starting point would be something matching your project name.
46
49
***AWS Region**: The AWS region you want to deploy your app to.
47
50
***Confirm changes before deploy**: If set to yes, any change sets will be shown to you before execution for manual review. If set to no, the AWS SAM CLI will automatically deploy application changes.
51
+
***HelloWorldFunction has no authentication. Is this okay? [y/N]:**: Select `y` for the purposes of this sample application. As a result, anyone will be able to call this example REST API without any form of authentication. For production applications, you should [enable authentication for the API Gateway](https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-control-access-to-api.html) using one of several available options and [follow the API Gateway security best practices](https://docs.aws.amazon.com/apigateway/latest/developerguide/security-best-practices.html). If `N` is selected for this question, this deployment will not succeed.
48
52
***Allow AWS SAM CLI IAM role creation**: Many AWS SAM templates, including this example, create AWS IAM roles required for the AWS Lambda function(s) included to access AWS services. By default, these are scoped down to minimum required permissions. To deploy an AWS CloudFormation stack which creates or modifies IAM roles, the `CAPABILITY_IAM` value for `capabilities` must be provided. If permission isn't provided through this prompt, to deploy this example you must explicitly pass `--capabilities CAPABILITY_IAM` to the `sam deploy` command.
49
53
***Save arguments to samconfig.toml**: If set to yes, your choices will be saved to a configuration file inside the project, so that in the future you can just re-run `sam deploy` without parameters to deploy changes to your application.
50
54
51
55
You can find your API Gateway Endpoint URL in the output values displayed after deployment.
52
56
53
-
## Use the AWS SAM CLI to build and test locally
54
-
55
-
Build your application with the `sam build --use-container` command.
56
-
57
-
```bash
58
-
$ sam build --use-container
59
-
```
57
+
## Use the AWS SAM CLI to test locally
60
58
61
-
The SAM CLI installs dependencies defined in `hello_world/requirements.txt`, creates a deployment package, and saves it in the `.aws-sam/build` folder.
59
+
The SAM CLI installs dependencies defined in `hello_world/package.json`, creates a deployment package, and saves it in the `.aws-sam/build` folder.
62
60
63
-
Test a single function by invoking it directly with a test event. An event is a JSON document that represents the input that the function receives from the event source. Test events are included in the `events` folder in this project.
61
+
Ypu can test a single function by invoking it directly with a test event. An event is a JSON document that represents the input that the function receives from the event source. Test events are included in the `events` folder in this project.
64
62
65
63
Run functions locally and invoke them with the `sam local invoke` command.
66
64
@@ -75,7 +73,7 @@ $ sam local start-api
75
73
$ curl http://localhost:3000/
76
74
```
77
75
78
-
The AWS SAM CLI reads the application template to determine the API's routes and the functions that they invoke. The `Events` property on each function's definition includes the route and method for each path.
76
+
The AWS SAM CLI reads the application template to determine the API's routes and the functions they invoke. The `Events` property on each function's definition includes the route and method for each path.
79
77
80
78
```yaml
81
79
Events:
@@ -86,11 +84,18 @@ The AWS SAM CLI reads the application template to determine the API's routes and
86
84
Method: get
87
85
```
88
86
89
-
## Add a resource to your application
90
-
The application template uses AWS Serverless Application Model (AWS SAM) to define application resources. AWS SAM is an extension of AWS CloudFormation with a simpler syntax for configuring common serverless application resources such as functions, triggers, and APIs. For resources not included in [the AWS SAM specification](https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md), you can use standard [AWS CloudFormation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-template-resource-type-ref.html) resource types.
87
+
## Use the AWS SAM CLI to test remotely
88
+
After you have deployed your application, you can remotely invoke your Lambda function to test it in the cloud.
91
89
92
-
## Fetch, tail, and filter Lambda function logs
90
+
Invoke functions remotely with the `sam remote invoke` command.
91
+
92
+
```bash
93
+
$ sam remote invoke HelloWorldFunction --event-file events/event.json
94
+
```
93
95
96
+
You can also go to the API Gateway endpoint URL that was output after the deployment of your application, which will similarly invoke your deployed Lambda function.
97
+
98
+
## Fetch, tail, and filter Lambda function logs
94
99
To simplify troubleshooting, AWS SAM CLI has a command called `sam logs`. `sam logs` lets you fetch logs generated by your deployed Lambda function from the command line. In addition to printing the logs on the terminal, this command has several nifty features to help you quickly find the bug.
95
100
96
101
`NOTE`: This command works for all AWS Lambda functions; not just the ones you deploy using AWS SAM.
@@ -107,11 +112,9 @@ You can find more information and examples about filtering Lambda function logs
107
112
To delete the sample application that you created, use the AWS CLI. Assuming you used your project name for the stack name, you can run the following:
108
113
109
114
```bash
110
-
sam delete --stack-name "YOUR_STACK_NAME_HERE"
115
+
$ sam delete
111
116
```
112
117
113
118
## Resources
114
119
115
-
See the [AWS SAM developer guide](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/what-is-sam.html) for an introduction to SAM specification, the SAM CLI, and serverless application concepts.
116
-
117
-
Next, you can use AWS Serverless Application Repository to deploy ready to use Apps that go beyond hello world samples and learn how authors developed their applications: [AWS Serverless Application Repository main page](https://aws.amazon.com/serverless/serverlessrepo/)
120
+
See the [AWS SAM developer guide](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/what-is-sam.html) for an introduction to SAM specification, the SAM CLI, and serverless application concepts.
0 commit comments