Skip to content

Commit

Permalink
feat(init): Update all READMEs to match new NodeJS README (#1321)
Browse files Browse the repository at this point in the history
  • Loading branch information
jfuss committed Aug 9, 2019
1 parent 2f96a9a commit 5e15771
Show file tree
Hide file tree
Showing 10 changed files with 581 additions and 564 deletions.
@@ -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/)
@@ -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"
}
}

0 comments on commit 5e15771

Please sign in to comment.