diff --git a/samcli/local/init/templates/cookiecutter-aws-sam-hello-dotnet/{{cookiecutter.project_name}}/README.md b/samcli/local/init/templates/cookiecutter-aws-sam-hello-dotnet/{{cookiecutter.project_name}}/README.md index b178a16dbc..17d9cf30ec 100644 --- a/samcli/local/init/templates/cookiecutter-aws-sam-hello-dotnet/{{cookiecutter.project_name}}/README.md +++ b/samcli/local/init/templates/cookiecutter-aws-sam-hello-dotnet/{{cookiecutter.project_name}}/README.md @@ -1,144 +1,137 @@ # {{ cookiecutter.project_name }} -This is a sample template for {{ cookiecutter.project_name }} +This project contains source code and supporting files for a serverless application that you can deploy with the SAM CLI. It includes the following files and folders. -## Requirements +- src - Code for the application's Lambda function. +- events - Invocation events that you can use to invoke the function. +- test - Unit tests for the application code. +- template.yaml - A template that defines the application's AWS resources. -* AWS CLI already configured with Administrator permission -* [Docker installed](https://www.docker.com/community-edition) -* [SAM CLI installed](https://github.com/awslabs/aws-sam-cli) -* [.NET Core installed](https://www.microsoft.com/net/download) +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. -Please see the [currently supported patch of each major version of .NET Core](https://github.com/aws/aws-lambda-dotnet#version-status) to ensure your functions are compatible with the AWS Lambda runtime. +If you prefer to use an integrated development environment (IDE) to build and test your application, you can use the AWS Toolkit. +The AWS Toolkit is an open source plug-in for popular IDEs that uses the 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. -## Recommended Tools for Visual Studio / Visual Studio Code Users +* [PyCharm](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [IntelliJ](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [VS Code](https://docs.aws.amazon.com/toolkit-for-vscode/latest/userguide/welcome.html) +* [Visual Studio](https://docs.aws.amazon.com/toolkit-for-visual-studio/latest/user-guide/welcome.html) -* [AWS Toolkit for Visual Studio](https://aws.amazon.com/visualstudio/) -* [AWS Extensions for .NET CLI](https://github.com/aws/aws-extensions-for-dotnet-cli) which are AWS extensions to the .NET CLI focused on building .NET Core and ASP.NET Core applications and deploying them to AWS services including Amazon Elastic Container Service, AWS Elastic Beanstalk and AWS Lambda. +## Deploy the sample application -> **Note: You do not need to have the [AWS Extensions for .NET CLI](https://github.com/aws/aws-extensions-for-dotnet-cli) installed, but are free to do so if you which to use them. Version 3 of the Amazon.Lambda.Tools does require .NET Core 2.1 for installation, but can be used to deploy older versions of .NET Core.** +The Serverless Application Model Command Line Interface (SAM CLI) is an extension of the AWS CLI that adds functionality for building and testing Lambda applications. It uses Docker to run your functions in an Amazon Linux environment that matches Lambda. It can also emulate your application's build environment and API. -## Setup process +To use the SAM CLI, you need the following tools. -### Folder Structure +* AWS CLI - [Install the AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html) and [configure it with your AWS credentials]. +* SAM CLI - [Install the SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) +* .NET Core - [Install .NET Core](https://www.microsoft.com/net/download) +* Docker - [Install Docker community edition](https://hub.docker.com/search/?type=edition&offering=community) -AWS Lambda C# runtime requires a flat folder with all dependencies including the application. SAM will use `CodeUri` property to know where to look up for both application and dependencies. `CodeUri` must be set to the path to folder containing your Lambda function source code and `.csproj` file. +The SAM CLI uses an Amazon S3 bucket to store your application's deployment artifacts. If you don't have a bucket suitable for this purpose, create one. Replace `BUCKET_NAME` in the commands in this section with a unique bucket name. -```yaml -... - HelloWorldFunction: - Type: AWS::Serverless::Function - Properties: - CodeUri: ./src/HelloWorld - ... +```bash +{{ cookiecutter.project_name }}$ aws s3 mb s3://BUCKET_NAME ``` -### Building your application +To prepare the application for deployment, use the `sam package` command. ```bash -sam build +{{ cookiecutter.project_name }}$ sam package \ + --output-template-file packaged.yaml \ + --s3-bucket BUCKET_NAME ``` -### Local development +The SAM CLI creates deployment packages, uploads them to the S3 bucket, and creates a new version of the template that refers to the artifacts in the bucket. -**Invoking function locally** +To deploy the application, use the `sam deploy` command. ```bash -sam local invoke --no-event +{{ cookiecutter.project_name }}$ sam deploy \ + --template-file packaged.yaml \ + --stack-name {{ cookiecutter.project_name }} \ + --capabilities CAPABILITY_IAM ``` -To invoke with an event you can pass in a json file to the command. +After deployment is complete you can run the following command to retrieve the API Gateway Endpoint URL: ```bash -sam local invoke -e event.json -``` +{{ cookiecutter.project_name }}$ aws cloudformation describe-stacks \ + --stack-name {{ cookiecutter.project_name }} \ + --query 'Stacks[].Outputs[?OutputKey==`HelloWorldApi`]' \ + --output table +``` +## Use the SAM CLI to build and test locally -**Invoking function locally through local API Gateway** +Build your application with the `sam build` command. ```bash -sam local start-api +{{ cookiecutter.project_name }}$ sam build ``` -**SAM Local** is used to emulate both Lambda and API Gateway locally and uses our `template.yaml` to understand how to bootstrap this environment (runtime, where the source code is, etc.) - The following excerpt is what the CLI will read in order to initialize an API and its routes: +The SAM CLI installs dependencies defined in `src/HelloWorld.csproj`, creates a deployment package, and saves it in the `.aws-sam/build` folder. -```yaml -... -Events: - HelloWorldFunction: - Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api - Properties: - Path: /hello - Method: get -``` - -If the previous command run successfully you should now be able to hit the following local endpoint to invoke your function `http://localhost:3000/hello` +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. -## Packaging and deployment - -First and foremost, we need an `S3 bucket` where we can upload our Lambda functions packaged as ZIP before we deploy anything - If you don't have a S3 bucket to store code artifacts then this is a good time to create one: +Run functions locally and invoke them with the `sam local invoke` command. ```bash -aws s3 mb s3://BUCKET_NAME +{{ cookiecutter.project_name }}$ sam local invoke HelloWorldFunction --event events/event.json ``` -Next, run the following command to package our Lambda function to S3: +The SAM CLI can also emulate your application's API. Use the `sam local start-api` to run the API locally on port 3000. ```bash -sam package \ - --output-template-file packaged.yaml \ - --s3-bucket REPLACE_THIS_WITH_YOUR_S3_BUCKET_NAME +{{ cookiecutter.project_name }}$ sam local start-api +{{ cookiecutter.project_name }}$ curl http://localhost:3000/ ``` -Next, the following command will create a Cloudformation Stack and deploy your SAM resources. +The 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. -```bash -sam deploy \ - --template-file packaged.yaml \ - --stack-name {{ cookiecutter.project_name.lower().replace(' ', '-') }} \ - --capabilities CAPABILITY_IAM +```yaml + Events: + HelloWorld: + Type: Api + Properties: + Path: /hello + Method: get ``` -> **See [Serverless Application Model (SAM) HOWTO Guide](https://github.com/awslabs/serverless-application-model/blob/master/HOWTO.md) for more details in how to get started.** +## Add a resource to your application +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 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. -After deployment is complete you can run the following command to retrieve the API Gateway Endpoint URL: +## Fetch, tail, and filter Lambda function logs -```bash -aws cloudformation describe-stacks \ - --stack-name {{ cookiecutter.project_name.lower().replace(' ', '-') }} \ - --query 'Stacks[].Outputs' -``` +To simplify troubleshooting, 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. -## Testing - -For testing our code, we use XUnit and you can use `dotnet test` to run tests defined under `test/` +`NOTE`: This command works for all AWS Lambda functions; not just the ones you deploy using SAM. ```bash -dotnet test test/HelloWorld.Test +{{ cookiecutter.project_name }}$ sam logs -n HelloWorldFunction --stack-name {{ cookiecutter.project_name }} --tail ``` -# Next Steps - -Create your own .NET Core solution template to use with SAM CLI. [Cookiecutter for AWS SAM and .NET](https://github.com/aws-samples/cookiecutter-aws-sam-dotnet) provides you with a sample implementation how to use cookiecutter templating library to standardise how you initialise your Serverless projects. +You can find more information and examples about filtering Lambda function logs in the [SAM CLI Documentation](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-logging.html). -``` bash - sam init --location gh:aws-samples/cookiecutter-aws-sam-dotnet -``` +## Unit tests -For more information and examples of how to use `sam init` run +Tests are defined in the `test` folder in this project. -``` bash -sam init --help +```bash +{{ cookiecutter.project_name }}$ dotnet test test/HelloWorld.Test ``` -## Bringing to the next level +## Cleanup -Here are a few ideas that you can use to get more acquainted as to how this overall process works: +To delete the sample application and the bucket that you created, use the AWS CLI. + +```bash +{{ cookiecutter.project_name }}$ aws cloudformation delete-stack --stack-name {{ cookiecutter.project_name }} +{{ cookiecutter.project_name }}$ aws s3 rb s3://BUCKET_NAME +``` -* Create an additional API resource (e.g. /hello/{proxy+}) and return the name requested through this new path -* Update unit test to capture that -* Package & Deploy +## Resources -Next, you can use the following resources to know more about beyond hello world samples and how others structure their Serverless applications: +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. -* [AWS Serverless Application Repository](https://aws.amazon.com/serverless/serverlessrepo/) +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/) diff --git a/samcli/local/init/templates/cookiecutter-aws-sam-hello-dotnet/{{cookiecutter.project_name}}/events/event.json b/samcli/local/init/templates/cookiecutter-aws-sam-hello-dotnet/{{cookiecutter.project_name}}/events/event.json new file mode 100644 index 0000000000..3822fadaaa --- /dev/null +++ b/samcli/local/init/templates/cookiecutter-aws-sam-hello-dotnet/{{cookiecutter.project_name}}/events/event.json @@ -0,0 +1,63 @@ +{ + "body": "{\"message\": \"hello world\"}", + "resource": "/{proxy+}", + "path": "/path/to/resource", + "httpMethod": "POST", + "isBase64Encoded": false, + "queryStringParameters": { + "foo": "bar" + }, + "pathParameters": { + "proxy": "/path/to/resource" + }, + "stageVariables": { + "baz": "qux" + }, + "headers": { + "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", + "Accept-Encoding": "gzip, deflate, sdch", + "Accept-Language": "en-US,en;q=0.8", + "Cache-Control": "max-age=0", + "CloudFront-Forwarded-Proto": "https", + "CloudFront-Is-Desktop-Viewer": "true", + "CloudFront-Is-Mobile-Viewer": "false", + "CloudFront-Is-SmartTV-Viewer": "false", + "CloudFront-Is-Tablet-Viewer": "false", + "CloudFront-Viewer-Country": "US", + "Host": "1234567890.execute-api.us-east-1.amazonaws.com", + "Upgrade-Insecure-Requests": "1", + "User-Agent": "Custom User Agent String", + "Via": "1.1 08f323deadbeefa7af34d5feb414ce27.cloudfront.net (CloudFront)", + "X-Amz-Cf-Id": "cDehVQoZnx43VYQb9j2-nvCh-9z396Uhbp027Y2JvkCPNLmGJHqlaA==", + "X-Forwarded-For": "127.0.0.1, 127.0.0.2", + "X-Forwarded-Port": "443", + "X-Forwarded-Proto": "https" + }, + "requestContext": { + "accountId": "123456789012", + "resourceId": "123456", + "stage": "prod", + "requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef", + "requestTime": "09/Apr/2015:12:34:56 +0000", + "requestTimeEpoch": 1428582896000, + "identity": { + "cognitoIdentityPoolId": null, + "accountId": null, + "cognitoIdentityId": null, + "caller": null, + "accessKey": null, + "sourceIp": "127.0.0.1", + "cognitoAuthenticationType": null, + "cognitoAuthenticationProvider": null, + "userArn": null, + "userAgent": "Custom User Agent String", + "user": null + }, + "path": "/prod/path/to/resource", + "resourcePath": "/{proxy+}", + "httpMethod": "POST", + "apiId": "1234567890", + "protocol": "HTTP/1.1" + } + } + \ No newline at end of file diff --git a/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/{{cookiecutter.project_name}}/README.md b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/{{cookiecutter.project_name}}/README.md index daa130ff80..a1729ec9d0 100644 --- a/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/{{cookiecutter.project_name}}/README.md +++ b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/{{cookiecutter.project_name}}/README.md @@ -1,146 +1,138 @@ # {{ cookiecutter.project_name }} -This is a sample template for {{ cookiecutter.project_name }} - Below is a brief explanation of what we have generated for you: +This project contains source code and supporting files for a serverless application that you can deploy with the SAM CLI. It includes the following files and folders. -```bash -. -├── HelloWorldFunction -│   ├── build.gradle <-- Java Dependencies -│   ├── gradle <-- Gradle related Boilerplate -│   │   └── wrapper -│   │   ├── gradle-wrapper.jar -│   │   └── gradle-wrapper.properties -│   ├── gradlew <-- Linux/Mac Gradle Wrapper -│   ├── gradlew.bat <-- Windows Gradle Wrapper -│   └── src -│   ├── main -│   │   └── java -│   │   └── helloworld <-- Source code for a lambda function -│   │   ├── App.java <-- Lambda function code -│   │   └── GatewayResponse.java <-- POJO for API Gateway Responses object -│   └── test <-- Unit tests -│   └── java -│   └── helloworld -│   └── AppTest.java -├── README.md <-- This instructions file -└── template.yaml -``` +- HelloWorldFunction/src/main - Code for the application's Lambda function. +- events - Invocation events that you can use to invoke the function. +- HelloWorldFunction/src/test - Unit tests for the application code. +- template.yaml - A template that defines the application's AWS resources. + +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. + +If you prefer to use an integrated development environment (IDE) to build and test your application, you can use the AWS Toolkit. +The AWS Toolkit is an open source plug-in for popular IDEs that uses the 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. + +* [PyCharm](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [IntelliJ](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [VS Code](https://docs.aws.amazon.com/toolkit-for-vscode/latest/userguide/welcome.html) +* [Visual Studio](https://docs.aws.amazon.com/toolkit-for-visual-studio/latest/user-guide/welcome.html) -## Requirements +## Deploy the sample application -* AWS CLI already configured with Administrator permission -* [Java SE Development Kit 8 installed](http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html) -* [Docker installed](https://www.docker.com/community-edition) +The Serverless Application Model Command Line Interface (SAM CLI) is an extension of the AWS CLI that adds functionality for building and testing Lambda applications. It uses Docker to run your functions in an Amazon Linux environment that matches Lambda. It can also emulate your application's build environment and API. -## Setup process +To use the SAM CLI, you need the following tools. -### Installing dependencies +* AWS CLI - [Install the AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html) and [configure it with your AWS credentials]. +* SAM CLI - [Install the SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) +* Java8 - [Install the Java SE Development Kit 8](http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html) +* Docker - [Install Docker community edition](https://hub.docker.com/search/?type=edition&offering=community) + +The SAM CLI uses an Amazon S3 bucket to store your application's deployment artifacts. If you don't have a bucket suitable for this purpose, create one. Replace `BUCKET_NAME` in the commands in this section with a unique bucket name. ```bash -sam build +{{ cookiecutter.project_name }}$ aws s3 mb s3://BUCKET_NAME ``` -You can also build on a Lambda like environment by using: +To prepare the application for deployment, use the `sam package` command. ```bash -sam build --use-container +{{ cookiecutter.project_name }}$ sam package \ + --output-template-file packaged.yaml \ + --s3-bucket BUCKET_NAME ``` -### Local development +The SAM CLI creates deployment packages, uploads them to the S3 bucket, and creates a new version of the template that refers to the artifacts in the bucket. -**Invoking function locally through local API Gateway** +To deploy the application, use the `sam deploy` command. ```bash -sam local start-api +{{ cookiecutter.project_name }}$ sam deploy \ + --template-file packaged.yaml \ + --stack-name {{ cookiecutter.project_name }} \ + --capabilities CAPABILITY_IAM ``` -If the previous command ran successfully you should now be able to hit the following local endpoint to invoke your function `http://localhost:3000/hello` - -**SAM CLI** is used to emulate both Lambda and API Gateway locally and uses our `template.yaml` to understand how to bootstrap this environment (runtime, where the source code is, etc.) - The following excerpt is what the CLI will read in order to initialize an API and its routes: +After deployment is complete you can run the following command to retrieve the API Gateway Endpoint URL: -```yaml -... -Events: - HelloWorld: - Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api - Properties: - Path: /hello - Method: get -``` +```bash +{{ cookiecutter.project_name }}$ aws cloudformation describe-stacks \ + --stack-name {{ cookiecutter.project_name }} \ + --query 'Stacks[].Outputs[?OutputKey==`HelloWorldApi`]' \ + --output table +``` -## Packaging and deployment +## Use the SAM CLI to build and test locally -Firstly, we need a `S3 bucket` where we can upload our Lambda functions packaged as ZIP before we deploy anything - If you don't have a S3 bucket to store code artifacts then this is a good time to create one: +Build your application with the `sam build` command. ```bash -aws s3 mb s3://BUCKET_NAME +{{ cookiecutter.project_name }}$ sam build ``` -Next, run the following command to package our Lambda function to S3: +The SAM CLI installs dependencies defined in `HelloWorldFunction/build.gradle`, creates a deployment package, and saves it in the `.aws-sam/build` folder. + +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. + +Run functions locally and invoke them with the `sam local invoke` command. ```bash -sam package \ - --output-template-file packaged.yaml \ - --s3-bucket REPLACE_THIS_WITH_YOUR_S3_BUCKET_NAME +{{ cookiecutter.project_name }}$ sam local invoke HelloWorldFunction --event events/event.json ``` -Next, the following command will create a Cloudformation Stack and deploy your SAM resources. +The SAM CLI can also emulate your application's API. Use the `sam local start-api` to run the API locally on port 3000. ```bash -sam deploy \ - --template-file packaged.yaml \ - --stack-name {{ cookiecutter.project_name.lower().replace(' ', '-') }} \ - --capabilities CAPABILITY_IAM +{{ cookiecutter.project_name }}$ sam local start-api +{{ cookiecutter.project_name }}$ curl http://localhost:3000/ ``` -> **See [Serverless Application Model (SAM) HOWTO Guide](https://github.com/awslabs/serverless-application-model/blob/master/HOWTO.md) for more details in how to get started.** - -After deployment is complete you can run the following command to retrieve the API Gateway Endpoint URL: +The 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. -```bash -aws cloudformation describe-stacks \ - --stack-name {{ cookiecutter.project_name.lower().replace(' ', '-') }} \ - --query 'Stacks[].Outputs' +```yaml + Events: + HelloWorld: + Type: Api + Properties: + Path: /hello + Method: get ``` -## Testing +## Add a resource to your application +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 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. -We use `JUnit` for testing our code and you can simply run the following command to run our tests: +## Fetch, tail, and filter Lambda function logs + +To simplify troubleshooting, 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. + +`NOTE`: This command works for all AWS Lambda functions; not just the ones you deploy using SAM. ```bash -gradle test +{{ cookiecutter.project_name }}$ sam logs -n HelloWorldFunction --stack-name {{ cookiecutter.project_name }} --tail ``` -# Appendix +You can find more information and examples about filtering Lambda function logs in the [SAM CLI Documentation](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-logging.html). -## AWS CLI commands +## Unit tests -AWS CLI commands to package, deploy and describe outputs defined within the cloudformation stack: +Tests are defined in the `HelloWorldFunction/src/test` folder in this project. ```bash -sam package \ - --template-file template.yaml \ - --output-template-file packaged.yaml \ - --s3-bucket REPLACE_THIS_WITH_YOUR_S3_BUCKET_NAME - -sam deploy \ - --template-file packaged.yaml \ - --stack-name {{ cookiecutter.project_name.lower().replace(' ', '-') }} \ - --capabilities CAPABILITY_IAM \ - --parameter-overrides MyParameterSample=MySampleValue - -aws cloudformation describe-stacks \ - --stack-name {{ cookiecutter.project_name.lower().replace(' ', '-') }} --query 'Stacks[].Outputs' +{{ cookiecutter.project_name }}$ cd HelloWorldFunction +HelloWorldFunction$ gradle test ``` -## Bringing to the next level +## Cleanup + +To delete the sample application and the bucket that you created, use the AWS CLI. -Here are a few ideas that you can use to get more acquainted as to how this overall process works: +```bash +{{ cookiecutter.project_name }}$ aws cloudformation delete-stack --stack-name {{ cookiecutter.project_name }} +{{ cookiecutter.project_name }}$ aws s3 rb s3://BUCKET_NAME +``` -* Create an additional API resource (e.g. /hello/{proxy+}) and return the name requested through this new path -* Update unit test to capture that -* Package & Deploy +## Resources -Next, you can use the following resources to know more about beyond hello world samples and how others structure their Serverless applications: +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. -* [AWS Serverless Application Repository](https://aws.amazon.com/serverless/serverlessrepo/) +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/) diff --git a/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/{{cookiecutter.project_name}}/events/event.json b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/{{cookiecutter.project_name}}/events/event.json new file mode 100644 index 0000000000..3822fadaaa --- /dev/null +++ b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/{{cookiecutter.project_name}}/events/event.json @@ -0,0 +1,63 @@ +{ + "body": "{\"message\": \"hello world\"}", + "resource": "/{proxy+}", + "path": "/path/to/resource", + "httpMethod": "POST", + "isBase64Encoded": false, + "queryStringParameters": { + "foo": "bar" + }, + "pathParameters": { + "proxy": "/path/to/resource" + }, + "stageVariables": { + "baz": "qux" + }, + "headers": { + "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", + "Accept-Encoding": "gzip, deflate, sdch", + "Accept-Language": "en-US,en;q=0.8", + "Cache-Control": "max-age=0", + "CloudFront-Forwarded-Proto": "https", + "CloudFront-Is-Desktop-Viewer": "true", + "CloudFront-Is-Mobile-Viewer": "false", + "CloudFront-Is-SmartTV-Viewer": "false", + "CloudFront-Is-Tablet-Viewer": "false", + "CloudFront-Viewer-Country": "US", + "Host": "1234567890.execute-api.us-east-1.amazonaws.com", + "Upgrade-Insecure-Requests": "1", + "User-Agent": "Custom User Agent String", + "Via": "1.1 08f323deadbeefa7af34d5feb414ce27.cloudfront.net (CloudFront)", + "X-Amz-Cf-Id": "cDehVQoZnx43VYQb9j2-nvCh-9z396Uhbp027Y2JvkCPNLmGJHqlaA==", + "X-Forwarded-For": "127.0.0.1, 127.0.0.2", + "X-Forwarded-Port": "443", + "X-Forwarded-Proto": "https" + }, + "requestContext": { + "accountId": "123456789012", + "resourceId": "123456", + "stage": "prod", + "requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef", + "requestTime": "09/Apr/2015:12:34:56 +0000", + "requestTimeEpoch": 1428582896000, + "identity": { + "cognitoIdentityPoolId": null, + "accountId": null, + "cognitoIdentityId": null, + "caller": null, + "accessKey": null, + "sourceIp": "127.0.0.1", + "cognitoAuthenticationType": null, + "cognitoAuthenticationProvider": null, + "userArn": null, + "userAgent": "Custom User Agent String", + "user": null + }, + "path": "/prod/path/to/resource", + "resourcePath": "/{proxy+}", + "httpMethod": "POST", + "apiId": "1234567890", + "protocol": "HTTP/1.1" + } + } + \ No newline at end of file diff --git a/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/{{cookiecutter.project_name}}/README.md b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/{{cookiecutter.project_name}}/README.md index 0d52d9f69c..5b46ee17f5 100644 --- a/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/{{cookiecutter.project_name}}/README.md +++ b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/{{cookiecutter.project_name}}/README.md @@ -1,152 +1,139 @@ # {{ cookiecutter.project_name }} -This is a sample template for {{ cookiecutter.project_name }} - Below is a brief explanation of what we have generated for you: +This project contains source code and supporting files for a serverless application that you can deploy with the SAM CLI. It includes the following files and folders. -```bash -├── README.md <-- This instructions file -├── HelloWorldFunction <-- Source for HelloWorldFunction Lambda Function -│ ├── pom.xml <-- Java dependencies -│ └── src -│ ├── main -│ │ └── java -│ │ └── helloworld -│ │ ├── App.java <-- Lambda function code -│ │ └── GatewayResponse.java <-- POJO for API Gateway Responses object -│ └── test <-- Unit tests -│ └── java -│ └── helloworld -│ └── AppTest.java -└── template.yaml -``` +- HelloWorldFunction/src/main - Code for the application's Lambda function. +- events - Invocation events that you can use to invoke the function. +- HelloWorldFunction/src/test - Unit tests for the application code. +- template.yaml - A template that defines the application's AWS resources. -## Requirements +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. -* AWS CLI already configured with Administrator permission -* [Java SE Development Kit 8 installed](http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html) -* [Docker installed](https://www.docker.com/community-edition) -* [Maven](https://maven.apache.org/install.html) +If you prefer to use an integrated development environment (IDE) to build and test your application, you can use the AWS Toolkit. +The AWS Toolkit is an open source plug-in for popular IDEs that uses the 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. -## Setup process +* [PyCharm](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [IntelliJ](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [VS Code](https://docs.aws.amazon.com/toolkit-for-vscode/latest/userguide/welcome.html) +* [Visual Studio](https://docs.aws.amazon.com/toolkit-for-visual-studio/latest/user-guide/welcome.html) -### Installing dependencies +## Deploy the sample application -```bash -sam build -``` +The Serverless Application Model Command Line Interface (SAM CLI) is an extension of the AWS CLI that adds functionality for building and testing Lambda applications. It uses Docker to run your functions in an Amazon Linux environment that matches Lambda. It can also emulate your application's build environment and API. + +To use the SAM CLI, you need the following tools. -You can also build on a Lambda like environment by using +* AWS CLI - [Install the AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html) and [configure it with your AWS credentials]. +* SAM CLI - [Install the SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) +* Java8 - [Install the Java SE Development Kit 8](http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html) +* Maven - [Install Maven](https://maven.apache.org/install.html) +* Docker - [Install Docker community edition](https://hub.docker.com/search/?type=edition&offering=community) + +The SAM CLI uses an Amazon S3 bucket to store your application's deployment artifacts. If you don't have a bucket suitable for this purpose, create one. Replace `BUCKET_NAME` in the commands in this section with a unique bucket name. ```bash -sam build --use-container +{{ cookiecutter.project_name }}$ aws s3 mb s3://BUCKET_NAME ``` -### Local development - -**Invoking function locally through local API Gateway** +To prepare the application for deployment, use the `sam package` command. ```bash -sam local start-api +{{ cookiecutter.project_name }}$ sam package \ + --output-template-file packaged.yaml \ + --s3-bucket BUCKET_NAME ``` -If the previous command ran successfully you should now be able to hit the following local endpoint to invoke your function `http://localhost:3000/hello` +The SAM CLI creates deployment packages, uploads them to the S3 bucket, and creates a new version of the template that refers to the artifacts in the bucket. -**SAM CLI** is used to emulate both Lambda and API Gateway locally and uses our `template.yaml` to understand how to bootstrap this environment (runtime, where the source code is, etc.) - The following excerpt is what the CLI will read in order to initialize an API and its routes: +To deploy the application, use the `sam deploy` command. -```yaml -... -Events: - HelloWorld: - Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api - Properties: - Path: /hello - Method: get +```bash +{{ cookiecutter.project_name }}$ sam deploy \ + --template-file packaged.yaml \ + --stack-name {{ cookiecutter.project_name }} \ + --capabilities CAPABILITY_IAM ``` -## Packaging and deployment +After deployment is complete you can run the following command to retrieve the API Gateway Endpoint URL: -AWS Lambda Java runtime accepts either a zip file or a standalone JAR file - We use the latter in this example. SAM will use `CodeUri` property to know where to look up for both application and dependencies: +```bash +{{ cookiecutter.project_name }}$ aws cloudformation describe-stacks \ + --stack-name {{ cookiecutter.project_name }} \ + --query 'Stacks[].Outputs[?OutputKey==`HelloWorldApi`]' \ + --output table +``` -```yaml -... - HelloWorldFunction: - Type: AWS::Serverless::Function - Properties: - CodeUri: target/HelloWorld-1.0.jar - Handler: helloworld.App::handleRequest -``` +## Use the SAM CLI to build and test locally -Firstly, we need a `S3 bucket` where we can upload our Lambda functions packaged as ZIP before we deploy anything - If you don't have a S3 bucket to store code artifacts then this is a good time to create one: +Build your application with the `sam build` command. ```bash -aws s3 mb s3://BUCKET_NAME +{{ cookiecutter.project_name }}$ sam build ``` -Next, run the following command to package our Lambda function to S3: +The SAM CLI installs dependencies defined in `HelloWorldFunction/pom.xml`, creates a deployment package, and saves it in the `.aws-sam/build` folder. + +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. + +Run functions locally and invoke them with the `sam local invoke` command. ```bash -sam package \ - --output-template-file packaged.yaml \ - --s3-bucket REPLACE_THIS_WITH_YOUR_S3_BUCKET_NAME +{{ cookiecutter.project_name }}$ sam local invoke HelloWorldFunction --event events/event.json ``` -Next, the following command will create a Cloudformation Stack and deploy your SAM resources. +The SAM CLI can also emulate your application's API. Use the `sam local start-api` to run the API locally on port 3000. ```bash -sam deploy \ - --template-file packaged.yaml \ - --stack-name {{ cookiecutter.project_name.lower().replace(' ', '-') }} \ - --capabilities CAPABILITY_IAM +{{ cookiecutter.project_name }}$ sam local start-api +{{ cookiecutter.project_name }}$ curl http://localhost:3000/ ``` -> **See [Serverless Application Model (SAM) HOWTO Guide](https://github.com/awslabs/serverless-application-model/blob/master/HOWTO.md) for more details in how to get started.** - -After deployment is complete you can run the following command to retrieve the API Gateway Endpoint URL: +The 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. -```bash -aws cloudformation describe-stacks \ - --stack-name {{ cookiecutter.project_name.lower().replace(' ', '-') }} \ - --query 'Stacks[].Outputs' +```yaml + Events: + HelloWorld: + Type: Api + Properties: + Path: /hello + Method: get ``` -## Testing +## Add a resource to your application +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 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. -We use `JUnit` for testing our code and you can simply run the following command to run our tests: +## Fetch, tail, and filter Lambda function logs + +To simplify troubleshooting, 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. + +`NOTE`: This command works for all AWS Lambda functions; not just the ones you deploy using SAM. ```bash -cd HelloWorldFunction -mvn test +{{ cookiecutter.project_name }}$ sam logs -n HelloWorldFunction --stack-name {{ cookiecutter.project_name }} --tail ``` -# Appendix +You can find more information and examples about filtering Lambda function logs in the [SAM CLI Documentation](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-logging.html). -## AWS CLI commands +## Unit tests -AWS CLI commands to package, deploy and describe outputs defined within the cloudformation stack: +Tests are defined in the `HelloWorldFunction/src/test` folder in this project. ```bash -sam package \ - --template-file template.yaml \ - --output-template-file packaged.yaml \ - --s3-bucket REPLACE_THIS_WITH_YOUR_S3_BUCKET_NAME - -sam deploy \ - --template-file packaged.yaml \ - --stack-name {{ cookiecutter.project_name.lower().replace(' ', '-') }} \ - --capabilities CAPABILITY_IAM \ - --parameter-overrides MyParameterSample=MySampleValue - -aws cloudformation describe-stacks \ - --stack-name {{ cookiecutter.project_name.lower().replace(' ', '-') }} --query 'Stacks[].Outputs' +{{ cookiecutter.project_name }}$ cd HelloWorldFunction +HelloWorldFunction$ mvn test ``` -## Bringing to the next level +## Cleanup + +To delete the sample application and the bucket that you created, use the AWS CLI. -Here are a few ideas that you can use to get more acquainted as to how this overall process works: +```bash +{{ cookiecutter.project_name }}$ aws cloudformation delete-stack --stack-name {{ cookiecutter.project_name }} +{{ cookiecutter.project_name }}$ aws s3 rb s3://BUCKET_NAME +``` -* Create an additional API resource (e.g. /hello/{proxy+}) and return the name requested through this new path -* Update unit test to capture that -* Package & Deploy +## Resources -Next, you can use the following resources to know more about beyond hello world samples and how others structure their Serverless applications: +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. -* [AWS Serverless Application Repository](https://aws.amazon.com/serverless/serverlessrepo/) +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/) diff --git a/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/{{cookiecutter.project_name}}/events/event.json b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/{{cookiecutter.project_name}}/events/event.json new file mode 100644 index 0000000000..3822fadaaa --- /dev/null +++ b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/{{cookiecutter.project_name}}/events/event.json @@ -0,0 +1,63 @@ +{ + "body": "{\"message\": \"hello world\"}", + "resource": "/{proxy+}", + "path": "/path/to/resource", + "httpMethod": "POST", + "isBase64Encoded": false, + "queryStringParameters": { + "foo": "bar" + }, + "pathParameters": { + "proxy": "/path/to/resource" + }, + "stageVariables": { + "baz": "qux" + }, + "headers": { + "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", + "Accept-Encoding": "gzip, deflate, sdch", + "Accept-Language": "en-US,en;q=0.8", + "Cache-Control": "max-age=0", + "CloudFront-Forwarded-Proto": "https", + "CloudFront-Is-Desktop-Viewer": "true", + "CloudFront-Is-Mobile-Viewer": "false", + "CloudFront-Is-SmartTV-Viewer": "false", + "CloudFront-Is-Tablet-Viewer": "false", + "CloudFront-Viewer-Country": "US", + "Host": "1234567890.execute-api.us-east-1.amazonaws.com", + "Upgrade-Insecure-Requests": "1", + "User-Agent": "Custom User Agent String", + "Via": "1.1 08f323deadbeefa7af34d5feb414ce27.cloudfront.net (CloudFront)", + "X-Amz-Cf-Id": "cDehVQoZnx43VYQb9j2-nvCh-9z396Uhbp027Y2JvkCPNLmGJHqlaA==", + "X-Forwarded-For": "127.0.0.1, 127.0.0.2", + "X-Forwarded-Port": "443", + "X-Forwarded-Proto": "https" + }, + "requestContext": { + "accountId": "123456789012", + "resourceId": "123456", + "stage": "prod", + "requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef", + "requestTime": "09/Apr/2015:12:34:56 +0000", + "requestTimeEpoch": 1428582896000, + "identity": { + "cognitoIdentityPoolId": null, + "accountId": null, + "cognitoIdentityId": null, + "caller": null, + "accessKey": null, + "sourceIp": "127.0.0.1", + "cognitoAuthenticationType": null, + "cognitoAuthenticationProvider": null, + "userArn": null, + "userAgent": "Custom User Agent String", + "user": null + }, + "path": "/prod/path/to/resource", + "resourcePath": "/{proxy+}", + "httpMethod": "POST", + "apiId": "1234567890", + "protocol": "HTTP/1.1" + } + } + \ No newline at end of file diff --git a/samcli/local/init/templates/cookiecutter-aws-sam-hello-python/{{cookiecutter.project_name}}/README.md b/samcli/local/init/templates/cookiecutter-aws-sam-hello-python/{{cookiecutter.project_name}}/README.md index 513a84f724..a12489cb39 100644 --- a/samcli/local/init/templates/cookiecutter-aws-sam-hello-python/{{cookiecutter.project_name}}/README.md +++ b/samcli/local/init/templates/cookiecutter-aws-sam-hello-python/{{cookiecutter.project_name}}/README.md @@ -1,214 +1,142 @@ # {{ cookiecutter.project_name }} -This is a sample template for {{ cookiecutter.project_name }} - Below is a brief explanation of what we have generated for you: +This project contains source code and supporting files for a serverless application that you can deploy with the SAM CLI. It includes the following files and folders. -```bash -. -├── README.md <-- This instructions file -├── event.json <-- API Gateway Proxy Integration event payload -├── hello_world <-- Source code for a lambda function -│   ├── __init__.py -│   ├── app.py <-- Lambda function code -│   ├── requirements.txt <-- Lambda function code -├── template.yaml <-- SAM Template -└── tests <-- Unit tests - └── unit - ├── __init__.py - └── test_handler.py -``` - -## Requirements - -* AWS CLI already configured with Administrator permission -{%- if cookiecutter.runtime == 'python2.7' %} -* [Python 2.7 installed](https://www.python.org/downloads/) -{%- else %} -* [Python 3 installed](https://www.python.org/downloads/) -{%- endif %} -* [Docker installed](https://www.docker.com/community-edition) +- hello_world - Code for the application's Lambda function. +- events - Invocation events that you can use to invoke the function. +- tests - Unit tests for the application code. +- template.yaml - A template that defines the application's AWS resources. -## Setup process - -### Local development - -**Invoking function locally using a local sample payload** - -```bash -sam local invoke HelloWorldFunction --event event.json -``` - -**Invoking function locally through local API Gateway** - -```bash -sam local start-api -``` +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. -If the previous command ran successfully you should now be able to hit the following local endpoint to invoke your function `http://localhost:3000/hello` +If you prefer to use an integrated development environment (IDE) to build and test your application, you can use the AWS Toolkit. +The AWS Toolkit is an open source plug-in for popular IDEs that uses the 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. -**SAM CLI** is used to emulate both Lambda and API Gateway locally and uses our `template.yaml` to understand how to bootstrap this environment (runtime, where the source code is, etc.) - The following excerpt is what the CLI will read in order to initialize an API and its routes: +* [PyCharm](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [IntelliJ](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [VS Code](https://docs.aws.amazon.com/toolkit-for-vscode/latest/userguide/welcome.html) +* [Visual Studio](https://docs.aws.amazon.com/toolkit-for-visual-studio/latest/user-guide/welcome.html) -```yaml -... -Events: - HelloWorld: - Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api - Properties: - Path: /hello - Method: get -``` +## Deploy the sample application -## Packaging and deployment +The Serverless Application Model Command Line Interface (SAM CLI) is an extension of the AWS CLI that adds functionality for building and testing Lambda applications. It uses Docker to run your functions in an Amazon Linux environment that matches Lambda. It can also emulate your application's build environment and API. -AWS Lambda Python runtime requires a flat folder with all dependencies including the application. SAM will use `CodeUri` property to know where to look up for both application and dependencies: +To use the SAM CLI, you need the following tools. -```yaml -... - HelloWorldFunction: - Type: AWS::Serverless::Function - Properties: - CodeUri: hello_world/ - ... -``` +* AWS CLI - [Install the AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html) and [configure it with your AWS credentials]. +* SAM CLI - [Install the SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) +{%- if cookiecutter.runtime == 'python2.7' %} +* [Python 2.7 installed](https://www.python.org/downloads/) +{%- else %} +* [Python 3 installed](https://www.python.org/downloads/) +{%- endif %} +* Docker - [Install Docker community edition](https://hub.docker.com/search/?type=edition&offering=community) -Firstly, we need a `S3 bucket` where we can upload our Lambda functions packaged as ZIP before we deploy anything - If you don't have a S3 bucket to store code artifacts then this is a good time to create one: +The SAM CLI uses an Amazon S3 bucket to store your application's deployment artifacts. If you don't have a bucket suitable for this purpose, create one. Replace `BUCKET_NAME` in the commands in this section with a unique bucket name. ```bash -aws s3 mb s3://BUCKET_NAME +{{ cookiecutter.project_name }}$ aws s3 mb s3://BUCKET_NAME ``` -Next, run the following command to package our Lambda function to S3: +To prepare the application for deployment, use the `sam package` command. ```bash -sam package \ +{{ cookiecutter.project_name }}$ sam package \ --output-template-file packaged.yaml \ - --s3-bucket REPLACE_THIS_WITH_YOUR_S3_BUCKET_NAME + --s3-bucket BUCKET_NAME ``` -Next, the following command will create a Cloudformation Stack and deploy your SAM resources. +The SAM CLI creates deployment packages, uploads them to the S3 bucket, and creates a new version of the template that refers to the artifacts in the bucket. + +To deploy the application, use the `sam deploy` command. ```bash -sam deploy \ +{{ cookiecutter.project_name }}$ sam deploy \ --template-file packaged.yaml \ - --stack-name {{ cookiecutter.project_name.lower().replace(' ', '-') }} \ + --stack-name {{ cookiecutter.project_name }} \ --capabilities CAPABILITY_IAM ``` -> **See [Serverless Application Model (SAM) HOWTO Guide](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-quick-start.html) for more details in how to get started.** - After deployment is complete you can run the following command to retrieve the API Gateway Endpoint URL: ```bash -aws cloudformation describe-stacks \ - --stack-name {{ cookiecutter.project_name.lower().replace(' ', '-') }} \ +{{ cookiecutter.project_name }}$ aws cloudformation describe-stacks \ + --stack-name {{ cookiecutter.project_name }} \ --query 'Stacks[].Outputs[?OutputKey==`HelloWorldApi`]' \ --output table ``` -## Fetch, tail, and filter Lambda function logs - -To simplify troubleshooting, SAM CLI has a command called sam logs. sam logs lets you fetch logs generated by your 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. +## Use the SAM CLI to build and test locally -`NOTE`: This command works for all AWS Lambda functions; not just the ones you deploy using SAM. +Build your application with the `sam build` command. ```bash -sam logs -n HelloWorldFunction --stack-name {{ cookiecutter.project_name.lower().replace(' ', '-') }} --tail +{{ cookiecutter.project_name }}$ sam build ``` -You can find more information and examples about filtering Lambda function logs in the [SAM CLI Documentation](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-logging.html). - -## Testing +The SAM CLI installs dependencies defined in `hello_world/requirements.txt`, creates a deployment package, and saves it in the `.aws-sam/build` folder. +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. -Next, we install test dependencies and we run `pytest` against our `tests` folder to run our initial unit tests: +Run functions locally and invoke them with the `sam local invoke` command. ```bash -pip install pytest pytest-mock --user -python -m pytest tests/ -v +{{ cookiecutter.project_name }}$ sam local invoke HelloWorldFunction --event events/event.json ``` -## Cleanup - -In order to delete our Serverless Application recently deployed you can use the following AWS CLI Command: +The SAM CLI can also emulate your application's API. Use the `sam local start-api` to run the API locally on port 3000. ```bash -aws cloudformation delete-stack --stack-name {{ cookiecutter.project_name.lower().replace(' ', '-') }} +{{ cookiecutter.project_name }}$ sam local start-api +{{ cookiecutter.project_name }}$ curl http://localhost:3000/ ``` -## Bringing to the next level - -Here are a few things you can try to get more acquainted with building serverless applications using SAM: - -### Learn how SAM Build can help you with dependencies - -* Uncomment lines on `app.py` -* Build the project with ``sam build --use-container`` -* Invoke with ``sam local invoke HelloWorldFunction --event event.json`` -* Update tests - -### Create an additional API resource - -* Create a catch all resource (e.g. /hello/{proxy+}) and return the name requested through this new path -* Update tests +The 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. -### Step-through debugging - -* **[Enable step-through debugging docs for supported runtimes]((https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-using-debugging.html))** - -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/) +```yaml + Events: + HelloWorld: + Type: Api + Properties: + Path: /hello + Method: get +``` -# Appendix +## Add a resource to your application +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 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. -## Building the project +## Fetch, tail, and filter Lambda function logs -[AWS Lambda requires a flat folder](https://docs.aws.amazon.com/lambda/latest/dg/lambda-python-how-to-create-deployment-package.html) with the application as well as its dependencies in deployment package. When you make changes to your source code or dependency manifest, -run the following command to build your project local testing and deployment: +To simplify troubleshooting, 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. -```bash -sam build -``` +`NOTE`: This command works for all AWS Lambda functions; not just the ones you deploy using SAM. -If your dependencies contain native modules that need to be compiled specifically for the operating system running on AWS Lambda, use this command to build inside a Lambda-like Docker container instead: ```bash -sam build --use-container +{{ cookiecutter.project_name }}$ sam logs -n HelloWorldFunction --stack-name {{ cookiecutter.project_name }} --tail ``` -By default, this command writes built artifacts to `.aws-sam/build` folder. +You can find more information and examples about filtering Lambda function logs in the [SAM CLI Documentation](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-logging.html). -## SAM and AWS CLI commands +## Unit tests -All commands used throughout this document +Tests are defined in the `tests` folder in this project. Use PIP to install the [pytest](https://docs.pytest.org/en/latest/) and run unit tests. ```bash -# Generate event.json via generate-event command -sam local generate-event apigateway aws-proxy > event.json - -# Invoke function locally with event.json as an input -sam local invoke HelloWorldFunction --event event.json - -# Run API Gateway locally -sam local start-api +{{ cookiecutter.project_name }}$ pip install pytest pytest-mock --user +{{ cookiecutter.project_name }}$ python -m pytest tests/ -v +``` -# Create S3 bucket -aws s3 mb s3://BUCKET_NAME +## Cleanup -# Package Lambda function defined locally and upload to S3 as an artifact -sam package \ - --output-template-file packaged.yaml \ - --s3-bucket REPLACE_THIS_WITH_YOUR_S3_BUCKET_NAME +To delete the sample application and the bucket that you created, use the AWS CLI. -# Deploy SAM template as a CloudFormation stack -sam deploy \ - --template-file packaged.yaml \ - --stack-name {{ cookiecutter.project_name.lower().replace(' ', '-') }} \ - --capabilities CAPABILITY_IAM +```bash +{{ cookiecutter.project_name }}$ aws cloudformation delete-stack --stack-name {{ cookiecutter.project_name }} +{{ cookiecutter.project_name }}$ aws s3 rb s3://BUCKET_NAME +``` -# Describe Output section of CloudFormation stack previously created -aws cloudformation describe-stacks \ - --stack-name {{ cookiecutter.project_name.lower().replace(' ', '-') }} \ - --query 'Stacks[].Outputs[?OutputKey==`HelloWorldApi`]' \ - --output table +## Resources -# Tail Lambda function Logs using Logical name defined in SAM Template -sam logs -n HelloWorldFunction --stack-name {{ cookiecutter.project_name.lower().replace(' ', '-') }} --tail -``` +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. +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/) diff --git a/samcli/local/init/templates/cookiecutter-aws-sam-hello-python/{{cookiecutter.project_name}}/event.json b/samcli/local/init/templates/cookiecutter-aws-sam-hello-python/{{cookiecutter.project_name}}/events/event.json similarity index 100% rename from samcli/local/init/templates/cookiecutter-aws-sam-hello-python/{{cookiecutter.project_name}}/event.json rename to samcli/local/init/templates/cookiecutter-aws-sam-hello-python/{{cookiecutter.project_name}}/events/event.json diff --git a/samcli/local/init/templates/cookiecutter-aws-sam-hello-ruby/{{cookiecutter.project_name}}/README.md b/samcli/local/init/templates/cookiecutter-aws-sam-hello-ruby/{{cookiecutter.project_name}}/README.md index 8c6ebb70fc..8c426579f1 100644 --- a/samcli/local/init/templates/cookiecutter-aws-sam-hello-ruby/{{cookiecutter.project_name}}/README.md +++ b/samcli/local/init/templates/cookiecutter-aws-sam-hello-ruby/{{cookiecutter.project_name}}/README.md @@ -1,209 +1,137 @@ # {{ cookiecutter.project_name }} -This is a sample template for {{ cookiecutter.project_name }} - Below is a brief explanation of what we have generated for you: +This project contains source code and supporting files for a serverless application that you can deploy with the SAM CLI. It includes the following files and folders. -```bash -. -├── README.md <-- This instructions file -├── event.json <-- API Gateway Proxy Integration event payload -├── hello_world <-- Source code for a lambda function -│ ├── app.rb <-- Lambda function code -│ ├── Gemfile <-- Ruby function dependencies -├── template.yaml <-- SAM template -├── Gemfile <-- Ruby test/documentation dependencies -└── tests <-- Unit tests - └── unit - └── test_handler.rb -``` - -## Requirements - -* AWS CLI already configured with at Administrator permission -* [Ruby 2.5 installed](https://www.ruby-lang.org/en/documentation/installation/) -* [Docker installed](https://www.docker.com/community-edition) +- hello_world - Code for the application's Lambda function. +- events - Invocation events that you can use to invoke the function. +- tests - Unit tests for the application code. +- template.yaml - A template that defines the application's AWS resources. -## Setup process +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. -### Local development - -**Invoking function locally using a local sample payload** - -```bash -sam local invoke HelloWorldFunction --event event.json -``` +If you prefer to use an integrated development environment (IDE) to build and test your application, you can use the AWS Toolkit. +The AWS Toolkit is an open source plug-in for popular IDEs that uses the 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. -**Invoking function locally through local API Gateway** +* [PyCharm](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [IntelliJ](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [VS Code](https://docs.aws.amazon.com/toolkit-for-vscode/latest/userguide/welcome.html) +* [Visual Studio](https://docs.aws.amazon.com/toolkit-for-visual-studio/latest/user-guide/welcome.html) -```bash -sam local start-api -``` - -If the previous command ran successfully you should now be able to hit the following local endpoint to invoke your function `http://localhost:3000/hello` - -**SAM CLI** is used to emulate both Lambda and API Gateway locally and uses our `template.yaml` to understand how to bootstrap this environment (runtime, where the source code is, etc.) - The following excerpt is what the CLI will read in order to initialize an API and its routes: - -```yaml -... -Events: - HelloWorld: - Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api - Properties: - Path: /hello - Method: get -``` +## Deploy the sample application -## Packaging and deployment +The Serverless Application Model Command Line Interface (SAM CLI) is an extension of the AWS CLI that adds functionality for building and testing Lambda applications. It uses Docker to run your functions in an Amazon Linux environment that matches Lambda. It can also emulate your application's build environment and API. -AWS Lambda Ruby runtime requires a flat folder with all dependencies including the application. SAM will use `CodeUri` property to know where to look up for both application and dependencies: +To use the SAM CLI, you need the following tools. -```yaml -... - HelloWorldFunction: - Type: AWS::Serverless::Function - Properties: - CodeUri: hello_world/ - ... -``` +* AWS CLI - [Install the AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html) and [configure it with your AWS credentials]. +* SAM CLI - [Install the SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) +* Ruby - [Install Ruby 2.5](https://www.ruby-lang.org/en/documentation/installation/) +* Docker - [Install Docker community edition](https://hub.docker.com/search/?type=edition&offering=community) -Firstly, we need a `S3 bucket` where we can upload our Lambda functions packaged as ZIP before we deploy anything - If you don't have a S3 bucket to store code artifacts then this is a good time to create one: +The SAM CLI uses an Amazon S3 bucket to store your application's deployment artifacts. If you don't have a bucket suitable for this purpose, create one. Replace `BUCKET_NAME` in the commands in this section with a unique bucket name. ```bash -aws s3 mb s3://BUCKET_NAME +{{ cookiecutter.project_name }}$ aws s3 mb s3://BUCKET_NAME ``` -Next, run the following command to package our Lambda function to S3: +To prepare the application for deployment, use the `sam package` command. ```bash -sam package \ +{{ cookiecutter.project_name }}$ sam package \ --output-template-file packaged.yaml \ - --s3-bucket REPLACE_THIS_WITH_YOUR_S3_BUCKET_NAME + --s3-bucket BUCKET_NAME ``` -Next, the following command will create a Cloudformation Stack and deploy your SAM resources. +The SAM CLI creates deployment packages, uploads them to the S3 bucket, and creates a new version of the template that refers to the artifacts in the bucket. + +To deploy the application, use the `sam deploy` command. ```bash -sam deploy \ +{{ cookiecutter.project_name }}$ sam deploy \ --template-file packaged.yaml \ - --stack-name {{ cookiecutter.project_name.lower().replace(' ', '-') }} \ + --stack-name {{ cookiecutter.project_name }} \ --capabilities CAPABILITY_IAM ``` -> **See [Serverless Application Model (SAM) HOWTO Guide](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-quick-start.html) for more details in how to get started.** - After deployment is complete you can run the following command to retrieve the API Gateway Endpoint URL: ```bash -aws cloudformation describe-stacks \ - --stack-name {{ cookiecutter.project_name.lower().replace(' ', '-') }} \ +{{ cookiecutter.project_name }}$ aws cloudformation describe-stacks \ + --stack-name {{ cookiecutter.project_name }} \ --query 'Stacks[].Outputs[?OutputKey==`HelloWorldApi`]' \ --output table ``` -## Fetch, tail, and filter Lambda function logs +## Use the SAM CLI to build and test locally -To simplify troubleshooting, SAM CLI has a command called sam logs. sam logs lets you fetch logs generated by your 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. - -`NOTE`: This command works for all AWS Lambda functions; not just the ones you deploy using SAM. +Build your application with the `sam build` command. ```bash -sam logs -n HelloWorldFunction --stack-name {{ cookiecutter.project_name.lower().replace(' ', '-') }} --tail +{{ cookiecutter.project_name }}$ sam build ``` -You can find more information and examples about filtering Lambda function logs in the [SAM CLI Documentation](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-logging.html). +The SAM CLI installs dependencies defined in `hello_world/Gemfile`, creates a deployment package, and saves it in the `.aws-sam/build` folder. -## Testing +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. -Run our initial unit tests: +Run functions locally and invoke them with the `sam local invoke` command. ```bash -ruby tests/unit/test_handler.rb +{{ cookiecutter.project_name }}$ sam local invoke HelloWorldFunction --event events/event.json ``` -## Cleanup - -In order to delete our Serverless Application recently deployed you can use the following AWS CLI Command: +The SAM CLI can also emulate your application's API. Use the `sam local start-api` to run the API locally on port 3000. ```bash -aws cloudformation delete-stack --stack-name {{ cookiecutter.project_name.lower().replace(' ', '-') }} +{{ cookiecutter.project_name }}$ sam local start-api +{{ cookiecutter.project_name }}$ curl http://localhost:3000/ ``` -## Bringing to the next level - -Here are a few things you can try to get more acquainted with building serverless applications using SAM: - -### Learn how SAM Build can help you with dependencies - -* Uncomment lines on `app.rb` -* Build the project with ``sam build --use-container`` -* Invoke with ``sam local invoke HelloWorldFunction --event event.json`` -* Update tests - -### Create an additional API resource - -* Create a catch all resource (e.g. /hello/{proxy+}) and return the name requested through this new path -* Update tests - -### Step-through debugging - -* **[Enable step-through debugging docs for supported runtimes]((https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-using-debugging.html))** - -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/) +The 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. +```yaml + Events: + HelloWorld: + Type: Api + Properties: + Path: /hello + Method: get +``` -# Appendix +## Add a resource to your application +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 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. -## Building the project +## Fetch, tail, and filter Lambda function logs -[AWS Lambda requires a flat folder](https://docs.aws.amazon.com/lambda/latest/dg/ruby-package.html) with the application as well as its dependencies. When you make changes to your source code or dependency manifest, -run the following command to build your project local testing and deployment: +To simplify troubleshooting, 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. -```bash -sam build -``` +`NOTE`: This command works for all AWS Lambda functions; not just the ones you deploy using SAM. -If your dependencies contain native modules that need to be compiled specifically for the operating system running on AWS Lambda, use this command to build inside a Lambda-like Docker container instead: ```bash -sam build --use-container +{{ cookiecutter.project_name }}$ sam logs -n HelloWorldFunction --stack-name {{ cookiecutter.project_name }} --tail ``` -By default, this command writes built artifacts to `.aws-sam/build` folder. - +You can find more information and examples about filtering Lambda function logs in the [SAM CLI Documentation](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-logging.html). -## SAM and AWS CLI commands +## Unit tests -All commands used throughout this document +Tests are defined in the `tests` folder in this project. ```bash -# Generate event.json via generate-event command -sam local generate-event apigateway aws-proxy > event.json - -# Invoke function locally with event.json as an input -sam local invoke HelloWorldFunction --event event.json - -# Run API Gateway locally -sam local start-api +{{ cookiecutter.project_name }}$ ruby tests/unit/test_handler.rb +``` -# Create S3 bucket -aws s3 mb s3://BUCKET_NAME +## Cleanup -# Package Lambda function defined locally and upload to S3 as an artifact -sam package \ - --output-template-file packaged.yaml \ - --s3-bucket REPLACE_THIS_WITH_YOUR_S3_BUCKET_NAME +To delete the sample application and the bucket that you created, use the AWS CLI. -# Deploy SAM template as a CloudFormation stack -sam deploy \ - --template-file packaged.yaml \ - --stack-name {{ cookiecutter.project_name.lower().replace(' ', '-') }} \ - --capabilities CAPABILITY_IAM +```bash +{{ cookiecutter.project_name }}$ aws cloudformation delete-stack --stack-name {{ cookiecutter.project_name }} +{{ cookiecutter.project_name }}$ aws s3 rb s3://BUCKET_NAME +``` -# Describe Output section of CloudFormation stack previously created -aws cloudformation describe-stacks \ - --stack-name {{ cookiecutter.project_name.lower().replace(' ', '-') }} \ - --query 'Stacks[].Outputs[?OutputKey==`HelloWorldApi`]' \ - --output table +## Resources -# Tail Lambda function Logs using Logical name defined in SAM Template -sam logs -n HelloWorldFunction --stack-name {{ cookiecutter.project_name.lower().replace(' ', '-') }} --tail -``` +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. +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/) diff --git a/samcli/local/init/templates/cookiecutter-aws-sam-hello-ruby/{{cookiecutter.project_name}}/event.json b/samcli/local/init/templates/cookiecutter-aws-sam-hello-ruby/{{cookiecutter.project_name}}/events/event.json similarity index 100% rename from samcli/local/init/templates/cookiecutter-aws-sam-hello-ruby/{{cookiecutter.project_name}}/event.json rename to samcli/local/init/templates/cookiecutter-aws-sam-hello-ruby/{{cookiecutter.project_name}}/events/event.json