drminnaar / aws-dotnet-examples

A collection of independent .NET5 projects written in C# that demonstrate how to integrate with various AWS services using the AWS SDK for .NET

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

aws-dotnet-examples-github

AWS for .NET Developers

This project is a collection of independent .NET projects written in C# .NET 5 that demonstrate how to integrate with various AWS services using the AWS SDK for dotnet.


Toolchain

All projects have been built or tested on Windows 10 and Ubuntu 20.04. The following list is a summary of the primary tools, languages and frameworks used to build the application:

  • Visual Studio Code - Visual Studio Code is a source-code editor developed by Microsoft for Windows, Linux and macOS.
  • Ubuntu 20.04 - Ubuntu is an open source software operating system that runs from the desktop, to the cloud, to all your internet connected things.
  • .NET5 - .NET5 is a free and open-source managed software framework for Linux, Windows and macOS.
  • C# - A multi-paradigm programming language encompassing strong typing, imperative, declarative, functional, generic, object-oriented (class-based), and component-oriented programming disciplines.

AWS Services And Projects Summary

So far, projects have been created for the following AWS Services:

  • Amazon S3 - An AWS service that provides authentication, authorization, and user management for your web and mobile apps.

  • Amazon SNS - Amazon Simple Notification Service (Amazon SNS) is a web service that coordinates and manages the delivery or sending of messages to subscribing endpoints or clients.

  • Amazon SQS - Amazon Simple Queue Service (SQS) is a fully managed message queuing service that enables you to decouple and scale microservices, distributed systems, and serverless applications.

  • Amazon Cognito - Amazon Simple Notification Service (Amazon SNS) is a web service that coordinates and manages the delivery or sending of messages to subscribing endpoints or clients.

  • Amazon DynamoDb - Amazon DynamoDB is a fully managed NoSQL database service that provides fast and predictable performance with seamless scalability.


Projects

  • Cognito Mvc - This project demonstrates how to integrate an ASP.NET Core MVC web application with Amazon Cognito using the Amazon SDK.

    The following features have been implemented:

    • Signup for a new account
    • Confirm Signup (using confirmation code)
    • Sign into account
    • Sign out of account
  • Cognito Api - This project demonstrates how to integrate a .NET Core Web Api application with Amazon Cognito using the Amazon SDK.

    This Web Api application provides the following endpoints:

    • Signups
      • POST api/signups - Allows an api consumer to signup for a new account
    • Tokens
      • POST api/tokens - Provides JWT access token for valid account credentials
    • Values
      • GET api/values - An unauthorized endpoint that returns a list of values
      • GET api/values/123 - An authorized endpoint (Requires JWT access token) that returns a single value
  • S3 ConsoleApp - This project demonstrates how to integrate a .NET Core Console application with Amazon S3 using the Amazon SDK.

    The following features have been implemented:

    • List all S3 buckets
    • Create new S3 bucket
    • Delete S3 bucket
  • DynamoDb ConsoleApp - This project demonstrates how to integrate a .NET Core console application with DynamoDb using the Amazon SDK. This application provides the functionality required to both manage DynamoDb tables, and manage the data stored in a DynamoDB table

    The following features have been implemented:

    • Manage Tables
      • Create table
      • List and find tables
      • Wait for tables to be described (eventually consistent)
      • Delete table
    • Manage Book Table Data
      • Automatically creates Book table
      • Add books to Book table
      • Update books in Book table
      • Delete books from Book table
      • List and find books in Book table
  • SNS ConsoleApp - This project demonstrates how to integrate a .NET Core console application with SNS using the Amazon SDK. This application provides the functionality required to manage SNS topcs, subscriptions, and publications.

    The following features have been implemented:

    • Manage Topics
      • Create topic
      • List and find topics
      • Delete topic
    • Manage Subscriptions
      • Create an email subscription
      • Cancel a subscription
      • List subscriptions
    • Manage Publications
      • An example showing how to publish a 'Game Ranking' to a 'game-ranking' topic
  • SQS ConsoleApp - This project demonstrates how to integrate a .NET Core console application with SQS using the Amazon SDK.

    The following features have been implemented:

    • Manage Queues
      • Create queue
      • List queues
      • Delete queue
      • Get queue url
    • Manage Game Ranking Queue
      • Enqueue game ranking to queue
      • Dequeue game rankings from queue

AWS CLI

Even as a C#.NET developer, you will come to find the AWS CLI to be one of your most valuable tools. See the official AWS CLI documentation to learn more.

Installing AWS CLI

Please install Version 2 of the AWS CLI.

For installation instructions for all the major platforms, please visit the official AWS CLI installation documentation

List AWS CLI Configuration Data

From your terminal window, type the following commands:

# list aws cli configuration data
aws configure list

# list aws cli configuration profiles (only available from version 2)
aws configure list-profiles

Configure AWS CLI

Type the following command to create a default profile:

aws configure

AWS Access Key ID [None]: AKIAIOSFODNN7EXAMPLE
AWS Secret Access Key [None]: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
Default region name [None]: us-west-2
Default output format [None]: json

AWS configuration can be found in your home folder at the following locations:

# windows
~\aws\credentials
~\aws\config

# linux / macOS
~/aws/credentials
~/aws/config

Configure Additional AWS CLI Profiles

Type the following command to create additional profiles:

aws configure --profile s3demo
aws configure --profile sqsdemo
aws configure --profile dynamodbdemo

AWS Project Configuration

We need to provide some AWS configuration before we can start using the AWS SDK in our projects. In this section, I provide 3 options that one can use to provide the configuration that the AWS SDK requires at runtime.

  • provide configuration in appsettings.json file
  • provide configuration using environment variables
  • provide configuration using dotnet user secrets

It is also considered bad practice to store any AWS configuration in the application configuration files. Therefore, the 2 preferred approaches to storing your AWS configuration are as follows:

  • provide configuration using environment variables
  • provide configuration using dotnet user secrets

IMPORTANT

NEVER store any AWS credentials (access_key_id, secret__access__key_id) in configuration files

Using appsettings

This approach is not considered the best approach as it uses an application configuration file that can be committed to source control with sensitive credentials.

Add the following configuration section to your appsettings.json file:

// appsettings.json

{
    "AWS": {
        "Region": "us-east-1",
        "Profile": "aws-demo"
    }
}

Using Environment Variables

From your terminal, add the following environment variables:

  • AWS_PROFILE
  • AWS_REGION
# Powershell

$env:AWS_PROFILE = "aws-demo"
$env:AWS_REGION = "us-east-1"
# Linux / macOS

AWS_PROFILE='aws-demo'
AWS_REGION='us-east-1'
# Windows CMD

set AWS_PROFILE=aws-demo
set AWS_REGION=us-east-1

Using dotnet User Secrets

NOTE

This is the approach being used for all projects in this project

Working with user secrets involves using the Secret Manager tool. According to the official Microsoft documentation:

The Secret Manager tool stores sensitive data during the development of an ASP.NET Core project. In this context, a piece of sensitive data is an app secret. App secrets are stored in a separate location from the project tree. The app secrets are associated with a specific project or shared across several projects. The app secrets aren't checked into source control

For more information, see the official documentation

WARNING

The Secret Manager tool doesn't encrypt the stored secrets and shouldn't be treated as a trusted store. It's for development purposes only. The keys and values are stored in a JSON configuration file in the user profile directory.

Step 1 - Initialise User Secrets

From the terminal, type the following command to initialize user-secrets,

cd ./Aws.S3Demo.ConsoleApp
dotnet user-secrets init

The following entry is added to the project file:

<PropertyGroup>
  <UserSecretsId>567f0dg2-06b1-5567-94a5-8c855a1a7972</UserSecretsId>
</PropertyGroup>

Use the following commands to access the user secrets location and secrets.json file:

# Powershell

cat $env:APPDATA\Microsoft\UserSecrets\<USER_SECRETS_ID>\secrets.json

type $env:APPDATA\Microsoft\UserSecrets\<USER_SECRETS_ID>\secrets.json
# Linux / macOS

cat ~/.microsoft/usersecrets/<USER_SECRETS_ID>/secrets.json
# Windows CMD

type %APPDATA%\Microsoft\UserSecrets\<USER_SECRETS_ID>\secrets.json

NOTE

Substitute <USER_SECRETS_ID> with the GUID found in the project file.

<UserSecretsId>567f0dg2-06b1-5567-94a5-8c855a1a7972</UserSecretsId>

Step 2 - Add User Secrets

We need to add 2 user secrets representing the 2 values required by the AWS SDK:

  • Profile
  • Region

In the section Using appsettings, we had the following configuration:

{
    "AWS": {
        "Region": "us-east-1",
        "Profile": "aws-demo"
    }
}

We now need to add 2 user secrets that align with the aforementioned AWS configuration:

dotnet user-secrets set "AWS:Profile" "aws-demo"
dotnet user-secrets set "AWS:Region" "us-east-1"

Step 3 - List User Secrets

dotnet user-secrets list

Step 4 - Remove and Clear User Secrets

To individually remove user secrets, use the following commands:

dotnet user-secrets remove "AWS:Profile"
dotnet user-secrets remove "AWS:Region"

Alternatively, you can clear all user secrets using the following command:

dotnet user-secrets clear

Signup With AWS Free Tier

WARNING

You need a valid credit card to signup for the AWS Free Tier

AWS offers a generous free tier with a relatively straight forward signup process. Unfortunately, you will need a credit card to complete the signup process. Go to Signup.

NOTE

Check out this official getting started video (5 mins) that takes you through the signup process

For more information about the AWS Free Tier, see the following links:


Learning

AWS is vast and there are many great resources to learn about AWS. The following list is a curated list of learning resources that can be used to learn and remain current with AWS technology.

Official AWS Resources


Certification

AWS

Udemy

ACloudGuru

Whizlabs


Versioning

I use SemVer for versioning. For the versions available, see the tags on this repository.


Authors


About

A collection of independent .NET5 projects written in C# that demonstrate how to integrate with various AWS services using the AWS SDK for .NET


Languages

Language:C# 84.8%Language:HTML 13.2%Language:CSS 2.0%