Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/q23isline/study-sam-dotnet
AWS SAM と .NET の勉強用リポジトリ
https://github.com/q23isline/study-sam-dotnet
aws-sam csharp dotnet github-actions
Last synced: about 1 month ago
JSON representation
AWS SAM と .NET の勉強用リポジトリ
- Host: GitHub
- URL: https://github.com/q23isline/study-sam-dotnet
- Owner: q23isline
- License: mit
- Created: 2024-08-11T06:01:14.000Z (4 months ago)
- Default Branch: develop
- Last Pushed: 2024-08-13T12:37:13.000Z (4 months ago)
- Last Synced: 2024-08-14T07:24:13.474Z (4 months ago)
- Topics: aws-sam, csharp, dotnet, github-actions
- Language: C#
- Homepage:
- Size: 47.9 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# study-sam-dotnet
[![LICENSE](https://img.shields.io/badge/license-MIT-green.svg)](./LICENSE)
![releases](https://img.shields.io/github/release/q23isline/study-sam-dotnet.svg?logo=github)
[![GitHub Actions](https://github.com/q23isline/study-sam-dotnet/actions/workflows/dotnet.yml/badge.svg)](https://github.com/q23isline/study-sam-dotnet/actions/workflows/dotnet.yml)
[![Open in Visual Studio Code](https://img.shields.io/static/v1?logo=visualstudiocode&label=&message=Open%20in%20Visual%20Studio%20Code&labelColor=555555&color=007acc&logoColor=007acc)](https://open.vscode.dev/q23isline/study-sam-dotnet)[![.NET](https://img.shields.io/static/v1?logo=dotnet&label=.NET&message=v8&labelColor=555555&color=512bd4&logoColor=ffffff)](https://dotnet.microsoft.com/ja-jp/)
## はじめにやること
1. 先にこちらの環境構築をする
-
2. ソースダウンロード```bash
git clone 'https://github.com/q23isline/study-sam-dotnet.git'
```3. リポジトリのカレントディレクトリへ移動
```bash
cd study-sam-dotnet
```4. アプリ立ち上げ
```bash
sam build
sam local start-api --debug > logs/debug.log 2>&1 --docker-network study_vue3_backend
```## 日常的にやること
### システム起動
```bash
sam local start-api --debug > logs/debug.log 2>&1 --docker-network study_vue3_backend
```### システム終了
ターミナルで Ctrl + c
### バックエンドのビルド
```bash
sam build
```## 動作確認
### URL
-
## バックエンドフォーマット解析単体実行
```bash
docker run --rm -w /app -v $(pwd):/app mcr.microsoft.com/dotnet/sdk:8.0 dotnet format --verify-no-changes ./src/ServerlessAPI/ServerlessAPI.csproj
```## AWS SAM テンプレートを検証
```bash
sam validate --lint
```---
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.
- 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.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.* [CLion](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html)
* [GoLand](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)
* [WebStorm](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html)
* [Rider](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html)
* [PhpStorm](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html)
* [PyCharm](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html)
* [RubyMine](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html)
* [DataGrip](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)## Deploy the sample application
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.
* 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)To build and deploy your application for the first time, run the following in your shell:
```bash
sam build
sam deploy --guided
```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:
* **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.
* **AWS Region**: The AWS region you want to deploy your app to.
* **Parameter AppBucketName**: This template includes a parameter to name the S3 bucket you will create as a part of the new application. This name needs to be globally unique.
* **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.
* **Allow 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.
* **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.## Use the SAM CLI to build and test locally
Build your application with the `sam build` command.
```bash
study-sam-dotnet$ sam build
```The SAM CLI installs dependencies defined in `src/ServerlessAPI/ServerlessAPI.csproj`, 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
study-sam-dotnet$ sam local invoke NetCodeWebAPIServerless --event events/event.json
```The AWS SAM CLI can also emulate your application's API. Use the `sam local start-api` command to run the API locally on port 3000.
```bash
study-sam-dotnet$ sam local start-api
study-sam-dotnet$ curl http://localhost:3000/
```## Add a resource to your application
The application template uses 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 that aren't included in the [AWS SAM specification](https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md), you can use the standard [AWS CloudFormation resource types](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-template-resource-type-ref.html).
Update `template.yaml` to add a dead-letter queue to your application. In the **Resources** section, add a resource named **MyQueue** with the type **AWS::SQS::Queue**. Then add a property to the **AWS::Serverless::Function** resource named **DeadLetterQueue** that targets the queue's Amazon Resource Name (ARN), and a policy that grants the function permission to access the queue.
```yaml
Resources:
MyQueue:
Type: AWS::SQS::Queue
NetCodeWebAPIServerless:
Type: AWS::Serverless::Function
Properties:
CodeUri: ./src/ServerlessAPI/
Handler: ServerlessAPI::ServerlessAPI.Function::FunctionHandler
Runtime: dotnet8
MemorySize: 1042
DeadLetterQueue:
Type: SQS
TargetArn: !GetAtt MyQueue.Arn
Policies:
- SQSSendMessagePolicy:
QueueName: !GetAtt MyQueue.QueueName
```The dead-letter queue is a location for Lambda to send events that could not be processed. It's only used if you invoke your function asynchronously, but it's useful here to show how you can modify your application's resources and function configuration.
Deploy the updated application.
```bash
study-sam-dotnet$ sam build
study-sam-dotnet$ sam deploy
```Open the [**Applications**](https://console.aws.amazon.com/lambda/home#/applications) page of the Lambda console, and choose your application. When the deployment completes, view the application resources on the **Overview** tab to see the new resource. Then, choose the function to see the updated configuration that specifies the dead-letter queue.
## 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
study-sam-dotnet$ sam logs -n NetCodeWebAPIServerless --stack-name study-sam-dotnet --tail
```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).
## Unit tests
Tests are defined in the `test` folder in this project.
```bash
study-sam-dotnet$ dotnet test test/ServerlessAPI.Tests/ServerlessAPI.Tests.csproj
```## Cleanup
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:
```bash
sam delete --stack-name study-sam-dotnet
```## Resources
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/)