Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.NET v4 folders: adding CloudFormation and CloudWatch #7197

Merged
merged 1 commit into from
Jan 22, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions dotnetv4/CloudWatch/Actions/CloudWatchActions.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AWSSDK.CloudWatch" Version="4.0.0-preview.4" />
<PackageReference Include="AWSSDK.Extensions.NETCore.Setup" Version="4.0.0-preview.4" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.0" />
<PackageReference Include="System.Text.Json" Version="9.0.0" />
</ItemGroup>

</Project>
93 changes: 93 additions & 0 deletions dotnetv4/CloudWatch/Actions/CloudWatchClasses.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

using System.Text.Json.Serialization;

namespace CloudWatchActions;

public class Left
{
[JsonPropertyName("min")]
public int Min { get; set; }

[JsonPropertyName("max")]
public int Max { get; set; }
}

public class Properties
{
[JsonPropertyName("markdown")]
public string Markdown { get; set; } = null!;

[JsonPropertyName("metrics")]
public List<List<object>> Metrics { get; set; } = null!;

[JsonPropertyName("view")]
public string View { get; set; } = null!;

[JsonPropertyName("region")]
public string Region { get; set; } = null!;

[JsonPropertyName("stat")]
public string Stat { get; set; } = null!;

[JsonPropertyName("period")]
public int? Period { get; set; } = null!;

[JsonPropertyName("yAxis")]
public YAxis YAxis { get; set; } = null!;

[JsonPropertyName("stacked")]
public bool? Stacked { get; set; } = null!;

[JsonPropertyName("title")]
public string Title { get; set; } = null!;

[JsonPropertyName("setPeriodToTimeRange")]
public bool? SetPeriodToTimeRange { get; set; } = null!;

[JsonPropertyName("liveData")]
public bool? LiveData { get; set; } = null!;

[JsonPropertyName("sparkline")]
public bool? Sparkline { get; set; } = null!;

[JsonPropertyName("trend")]
public bool? Trend { get; set; } = null!;

[JsonPropertyName("alarms")]
public List<string> Alarms { get; set; } = null!;
}

public class DashboardModel
{
[JsonPropertyName("widgets")]
public List<Widget> Widgets { get; set; } = null!;
}

public class Widget
{
[JsonPropertyName("type")]
public string Type { get; set; } = null!;

[JsonPropertyName("x")]
public int X { get; set; }

[JsonPropertyName("y")]
public int Y { get; set; }

[JsonPropertyName("width")]
public int Width { get; set; }

[JsonPropertyName("height")]
public int Height { get; set; }

[JsonPropertyName("properties")]
public Properties Properties { get; set; } = null!;
}

public class YAxis
{
[JsonPropertyName("left")]
public Left Left { get; set; } = null!;
}
534 changes: 534 additions & 0 deletions dotnetv4/CloudWatch/Actions/CloudWatchWrapper.cs

Large diffs are not rendered by default.

47 changes: 47 additions & 0 deletions dotnetv4/CloudWatch/Actions/HelloCloudWatch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

// snippet-start:[CloudWatch.dotnetv4.HelloCloudWatch]
using Amazon.CloudWatch;
using Amazon.CloudWatch.Model;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace CloudWatchActions;

public static class HelloCloudWatch
{
static async Task Main(string[] args)
{
// Use the AWS .NET Core Setup package to set up dependency injection for the Amazon CloudWatch service.
// Use your AWS profile name, or leave it blank to use the default profile.
using var host = Host.CreateDefaultBuilder(args)
.ConfigureServices((_, services) =>
services.AddAWSService<IAmazonCloudWatch>()
).Build();

// Now the client is available for injection.
var cloudWatchClient = host.Services.GetRequiredService<IAmazonCloudWatch>();

// You can use await and any of the async methods to get a response.
var metricNamespace = "AWS/Billing";
var response = await cloudWatchClient.ListMetricsAsync(new ListMetricsRequest
{
Namespace = metricNamespace
});
Console.WriteLine($"Hello Amazon CloudWatch! Following are some metrics available in the {metricNamespace} namespace:");
Console.WriteLine();
if (response.Metrics != null)
{
foreach (var metric in response.Metrics.Take(5))
{
Console.WriteLine($"\tMetric: {metric.MetricName}");
Console.WriteLine($"\tNamespace: {metric.Namespace}");
Console.WriteLine(
$"\tDimensions: {string.Join(", ", metric.Dimensions.Select(m => $"{m.Name}:{m.Value}"))}");
Console.WriteLine();
}
}
}
}
// snippet-end:[CloudWatch.dotnetv4.HelloCloudWatch]
48 changes: 48 additions & 0 deletions dotnetv4/CloudWatch/CloudWatchExamples.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.2.32630.192
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Actions", "Actions", "{7907FB6A-1353-4735-95DC-EEC5DF8C0649}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Scenarios", "Scenarios", "{B987097B-189C-4D0B-99BC-E67CD705BCA0}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{5455D423-2AFC-4BC6-B79D-9DC4270D8F7D}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CloudWatchActions", "Actions\CloudWatchActions.csproj", "{F019ADD9-E2BA-4670-963C-EF778E97E8CF}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CloudWatchScenario", "Scenarios\CloudWatchScenario.csproj", "{B2CDED30-54AF-4AE6-8AC1-5FB15A4F1264}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CloudWatchTests", "Tests\CloudWatchTests.csproj", "{F7DF95C8-F0D7-483C-9653-BEF71ACE61B5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{F019ADD9-E2BA-4670-963C-EF778E97E8CF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F019ADD9-E2BA-4670-963C-EF778E97E8CF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F019ADD9-E2BA-4670-963C-EF778E97E8CF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F019ADD9-E2BA-4670-963C-EF778E97E8CF}.Release|Any CPU.Build.0 = Release|Any CPU
{B2CDED30-54AF-4AE6-8AC1-5FB15A4F1264}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B2CDED30-54AF-4AE6-8AC1-5FB15A4F1264}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B2CDED30-54AF-4AE6-8AC1-5FB15A4F1264}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B2CDED30-54AF-4AE6-8AC1-5FB15A4F1264}.Release|Any CPU.Build.0 = Release|Any CPU
{F7DF95C8-F0D7-483C-9653-BEF71ACE61B5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F7DF95C8-F0D7-483C-9653-BEF71ACE61B5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F7DF95C8-F0D7-483C-9653-BEF71ACE61B5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F7DF95C8-F0D7-483C-9653-BEF71ACE61B5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{F019ADD9-E2BA-4670-963C-EF778E97E8CF} = {7907FB6A-1353-4735-95DC-EEC5DF8C0649}
{B2CDED30-54AF-4AE6-8AC1-5FB15A4F1264} = {B987097B-189C-4D0B-99BC-E67CD705BCA0}
{F7DF95C8-F0D7-483C-9653-BEF71ACE61B5} = {5455D423-2AFC-4BC6-B79D-9DC4270D8F7D}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {870D888D-5C8B-4057-8722-F73ECF38E513}
EndGlobalSection
EndGlobal
150 changes: 150 additions & 0 deletions dotnetv4/CloudWatch/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
# CloudWatch code examples for the SDK for .NET

## Overview

Shows how to use the AWS SDK for .NET to work with Amazon CloudWatch.

<!--custom.overview.start-->
<!--custom.overview.end-->

_CloudWatch provides a reliable, scalable, and flexible monitoring solution that you can start using within minutes._

## ⚠ Important

* Running this code might result in charges to your AWS account. For more details, see [AWS Pricing](https://aws.amazon.com/pricing/) and [Free Tier](https://aws.amazon.com/free/).
* Running the tests might result in charges to your AWS account.
* We recommend that you grant your code least privilege. At most, grant only the minimum permissions required to perform the task. For more information, see [Grant least privilege](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege).
* This code is not tested in every AWS Region. For more information, see [AWS Regional Services](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services).

<!--custom.important.start-->
<!--custom.important.end-->

## Code examples

### Prerequisites

For prerequisites, see the [README](../README.md#Prerequisites) in the `dotnetv4` folder.


<!--custom.prerequisites.start-->
To enable billing metrics and statistics for these examples, make sure to
[enable billing alerts](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/monitor_estimated_charges_with_cloudwatch.html#turning_on_billing_metrics) for your account.
<!--custom.prerequisites.end-->

### Get started

- [Hello CloudWatch](Actions/HelloCloudWatch.cs#L4) (`ListMetrics`)


### Basics

Code examples that show you how to perform the essential operations within a service.

- [Learn the basics](Scenarios/CloudWatchScenario.cs)


### Single actions

Code excerpts that show you how to call individual service functions.

- [DeleteAlarms](Actions/CloudWatchWrapper.cs#L396)
- [DeleteAnomalyDetector](Actions/CloudWatchWrapper.cs#L494)
- [DeleteDashboards](Actions/CloudWatchWrapper.cs#L512)
- [DescribeAlarmHistory](Actions/CloudWatchWrapper.cs#L369)
- [DescribeAlarms](Actions/CloudWatchWrapper.cs#L326)
- [DescribeAlarmsForMetric](Actions/CloudWatchWrapper.cs#L349)
- [DescribeAnomalyDetectors](Actions/CloudWatchWrapper.cs#L468)
- [DisableAlarmActions](Actions/CloudWatchWrapper.cs#L414)
- [EnableAlarmActions](Actions/CloudWatchWrapper.cs#L432)
- [GetDashboard](Actions/CloudWatchWrapper.cs#L115)
- [GetMetricData](Actions/CloudWatchWrapper.cs#L226)
- [GetMetricStatistics](Actions/CloudWatchWrapper.cs#L61)
- [GetMetricWidgetImage](Actions/CloudWatchWrapper.cs#L175)
- [ListDashboards](Actions/CloudWatchWrapper.cs#L134)
- [ListMetrics](Actions/CloudWatchWrapper.cs#L33)
- [PutAnomalyDetector](Actions/CloudWatchWrapper.cs#L450)
- [PutDashboard](Actions/CloudWatchWrapper.cs#L91)
- [PutMetricAlarm](Actions/CloudWatchWrapper.cs#L265)
- [PutMetricData](Actions/CloudWatchWrapper.cs#L154)


<!--custom.examples.start-->
<!--custom.examples.end-->

## Run the examples

### Instructions

For general instructions to run the examples, see the
[README](../README.md#building-and-running-the-code-examples) in the `dotnetv4` folder.

Some projects might include a settings.json file. Before compiling the project,
you can change these values to match your own account and resources. Alternatively,
add a settings.local.json file with your local settings, which will be loaded automatically
when the application runs.

After the example compiles, you can run it from the command line. To do so, navigate to
the folder that contains the .csproj file and run the following command:

```
dotnet run
```

Alternatively, you can run the example from within your IDE.


<!--custom.instructions.start-->
<!--custom.instructions.end-->

#### Hello CloudWatch

This example shows you how to get started using CloudWatch.


#### Learn the basics

This example shows you how to do the following:

- List CloudWatch namespaces and metrics.
- Get statistics for a metric and for estimated billing.
- Create and update a dashboard.
- Create and add data to a metric.
- Create and trigger an alarm, then view alarm history.
- Add an anomaly detector.
- Get a metric image, then clean up resources.

<!--custom.basic_prereqs.cloudwatch_GetStartedMetricsDashboardsAlarms.start-->
<!--custom.basic_prereqs.cloudwatch_GetStartedMetricsDashboardsAlarms.end-->


<!--custom.basics.cloudwatch_GetStartedMetricsDashboardsAlarms.start-->
<!--custom.basics.cloudwatch_GetStartedMetricsDashboardsAlarms.end-->


### Tests

⚠ Running tests might result in charges to your AWS account.


To find instructions for running these tests, see the [README](../README.md#Tests)
in the `dotnetv3` folder.



<!--custom.tests.start-->
<!--custom.tests.end-->

## Additional resources

- [CloudWatch User Guide](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/WhatIsCloudWatch.html)
- [CloudWatch API Reference](https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/Welcome.html)
- [SDK for .NET CloudWatch reference](https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/CloudWatch/NCloudWatch.html)

<!--custom.resources.start-->
<!--custom.resources.end-->

---

Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.

SPDX-License-Identifier: Apache-2.0
792 changes: 792 additions & 0 deletions dotnetv4/CloudWatch/Scenarios/CloudWatchScenario.cs

Large diffs are not rendered by default.

35 changes: 35 additions & 0 deletions dotnetv4/CloudWatch/Scenarios/CloudWatchScenario.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AWSSDK.CloudWatch" Version="4.0.0-preview.4" />
<PackageReference Include="AWSSDK.Extensions.NETCore.Setup" Version="4.0.0-preview.4" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.0" />
<PackageReference Include="System.Text.Json" Version="9.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Actions\CloudWatchActions.csproj" />
</ItemGroup>

<ItemGroup>
<Content Include="settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="settings.*.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<DependentUpon>settings.json</DependentUpon>
</Content>
</ItemGroup>

</Project>
90 changes: 90 additions & 0 deletions dotnetv4/CloudWatch/Scenarios/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
{
"dashboardName": "example-new-dashboard",
"exampleAlarmName": "example-metric-alarm",
"accountId": "1234567890",
"region": "us-east-1",
"emailTopic": "Default_CloudWatch_Alarms_Topic",
"customMetricNamespace": "example-namespace",
"customMetricName": "example-custom-metric",
"dashboardExampleBody": {
"widgets": [
{
"height": 6,
"width": 6,
"y": 0,
"x": 0,
"type": "text",
"properties": {
"markdown": "# Code Example Dashboard \nThis dashboard was created by example code.\n"
}
},
{
"height": 8,
"width": 8,
"y": 0,
"x": 6,
"type": "metric",
"properties": {
"metrics": [
[
"AWS/Billing",
"EstimatedCharges",
"Currency",
"USD",
{ "region": "us-east-1" }
]
],
"view": "timeSeries",
"region": "us-east-1",
"stat": "Maximum",
"period": 86400,
"yAxis": {
"left": {
"min": 0,
"max": 100
}
},
"stacked": false,
"title": "Estimated Billing",
"setPeriodToTimeRange": false,
"liveData": true,
"sparkline": true,
"trend": true
}
},
{
"height": 8,
"width": 8,
"y": 0,
"x": 14,
"type": "metric",
"properties": {
"metrics": [
[ "AWS/Usage", "CallCount", "Type", "API", "Resource", "ListMetrics", "Service", "CloudWatch", "Class", "None" ],
[ "...", "GetMetricStatistics", ".", ".", ".", "." ],
[ "...", "GetMetricData", ".", ".", ".", "." ],
[ "...", "PutDashboard", ".", ".", ".", "." ],
[ "...", "PutMetricData", ".", ".", ".", "." ]
],
"view": "timeSeries",
"yAxis": {
"left": {
"min": 0,
"max": 200
}
},
"stacked": false,
"region": "us-east-1",
"stat": "Sum",
"period": 300,
"title": "CloudWatch Usage",
"setPeriodToTimeRange": false,
"liveData": true,
"sparkline": true,
"trend": true
}
}
]
}
}

506 changes: 506 additions & 0 deletions dotnetv4/CloudWatch/Tests/CloudWatchTests.cs

Large diffs are not rendered by default.

44 changes: 44 additions & 0 deletions dotnetv4/CloudWatch/Tests/CloudWatchTests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Dangl.Xunit.Extensions.Ordering" Version="2.1.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="System.Text.Json" Version="9.0.0" />
<PackageReference Include="System.Text.RegularExpressions" Version="4.3.1" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<Content Include="testsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="testsettings.*.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<DependentUpon>testsettings.json</DependentUpon>
</Content>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Actions\CloudWatchActions.csproj" />
</ItemGroup>

</Project>
12 changes: 12 additions & 0 deletions dotnetv4/CloudWatch/Tests/Usings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

global using Xunit;
global using Xunit.Extensions.Ordering;

// Optional.
[assembly: CollectionBehavior(DisableTestParallelization = true)]
// Optional.
[assembly: TestCaseOrderer("Xunit.Extensions.Ordering.TestCaseOrderer", "Xunit.Extensions.Ordering")]
// Optional.
[assembly: TestCollectionOrderer("Xunit.Extensions.Ordering.CollectionOrderer", "Xunit.Extensions.Ordering")]
89 changes: 89 additions & 0 deletions dotnetv4/CloudWatch/Tests/testsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
{
"dashboardName": "example-test-dashboard",
"exampleAlarmName": "example-test-metric-alarm",
"accountId": "1234567890",
"region": "us-east-1",
"emailTopic": "Default_CloudWatch_Alarms_Topic",
"customMetricNamespace": "example-test-namespace",
"customMetricName": "example-test-custom-metric",
"dashboardExampleBody": {
"widgets": [
{
"height": 6,
"width": 6,
"y": 0,
"x": 0,
"type": "text",
"properties": {
"markdown": "# Code Example Dashboard \nThis dashboard was created by example code.\n"
}
},
{
"height": 8,
"width": 8,
"y": 0,
"x": 6,
"type": "metric",
"properties": {
"metrics": [
[
"AWS/Billing",
"EstimatedCharges",
"Currency",
"USD",
{ "region": "us-east-1" }
]
],
"view": "timeSeries",
"region": "us-east-1",
"stat": "Maximum",
"period": 86400,
"yAxis": {
"left": {
"min": 0,
"max": 100
}
},
"stacked": false,
"title": "Estimated Billing",
"setPeriodToTimeRange": false,
"liveData": true,
"sparkline": true,
"trend": true
}
},
{
"height": 8,
"width": 8,
"y": 0,
"x": 14,
"type": "metric",
"properties": {
"metrics": [
[ "AWS/Usage", "CallCount", "Type", "API", "Resource", "ListMetrics", "Service", "CloudWatch", "Class", "None" ],
[ "...", "GetMetricStatistics", ".", ".", ".", "." ],
[ "...", "GetMetricData", ".", ".", ".", "." ],
[ "...", "PutDashboard", ".", ".", ".", "." ],
[ "...", "PutMetricData", ".", ".", ".", "." ]
],
"view": "timeSeries",
"yAxis": {
"left": {
"min": 0,
"max": 200
}
},
"stacked": false,
"region": "us-east-1",
"stat": "Sum",
"period": 300,
"title": "CloudWatch Usage",
"setPeriodToTimeRange": false,
"liveData": true,
"sparkline": true,
"trend": true
}
}
]
}
}
23 changes: 23 additions & 0 deletions dotnetv4/Cloudformation/Actions/CloudFormationActions.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AWSSDK.CloudFormation" Version="4.0.0-preview.4" />
<PackageReference Include="AWSSDK.Extensions.NETCore.Setup" Version="4.0.0-preview.4" />
<PackageReference Include="AWSSDK.SSO" Version="4.0.0-preview.4" />
<PackageReference Include="AWSSDK.SSOOIDC" Version="4.0.0-preview.4" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.0" />
<PackageReference Include="System.Text.Json" Version="9.0.0" />
</ItemGroup>

</Project>
116 changes: 116 additions & 0 deletions dotnetv4/Cloudformation/Actions/HelloCloudFormation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

// snippet-start:[CloudFormation.dotnetv4.CloudFormationActions.HelloCloudFormation]
using Amazon.CloudFormation;
using Amazon.CloudFormation.Model;
using Amazon.Runtime;

namespace CloudFormationActions;

public static class HelloCloudFormation
{
public static IAmazonCloudFormation _amazonCloudFormation = null!;

static async Task Main(string[] args)
{
// Create the CloudFormation client
_amazonCloudFormation = new AmazonCloudFormationClient();
Console.WriteLine($"\nIn Region: {_amazonCloudFormation.Config.RegionEndpoint}");

// List the resources for each stack
await ListResources();
}

/// <summary>
/// Method to list stack resources and other information.
/// </summary>
/// <returns>True if successful.</returns>
public static async Task<bool> ListResources()
{
try
{
Console.WriteLine("Getting CloudFormation stack information...");

// Get all stacks using the stack paginator.
var paginatorForDescribeStacks =
_amazonCloudFormation.Paginators.DescribeStacks(
new DescribeStacksRequest());
if (paginatorForDescribeStacks.Stacks != null)
{
await foreach (Stack stack in paginatorForDescribeStacks.Stacks)
{
// Basic information for each stack
Console.WriteLine(
"\n------------------------------------------------");
Console.WriteLine($"\nStack: {stack.StackName}");
Console.WriteLine($" Status: {stack.StackStatus.Value}");
Console.WriteLine($" Created: {stack.CreationTime}");

// The tags of each stack (etc.)
if (stack.Tags != null && stack.Tags.Count > 0)
{
Console.WriteLine(" Tags:");
foreach (Tag tag in stack.Tags)
Console.WriteLine($" {tag.Key}, {tag.Value}");
}

// The resources of each stack
DescribeStackResourcesResponse responseDescribeResources =
await _amazonCloudFormation.DescribeStackResourcesAsync(
new DescribeStackResourcesRequest
{
StackName = stack.StackName
});
if (responseDescribeResources.StackResources != null && responseDescribeResources.StackResources.Count > 0)
{
Console.WriteLine(" Resources:");
foreach (StackResource resource in responseDescribeResources
.StackResources)
Console.WriteLine(
$" {resource.LogicalResourceId}: {resource.ResourceStatus}");
}
}
}

Console.WriteLine("\n------------------------------------------------");
return true;
}
catch (AmazonCloudFormationException ex)
{
Console.WriteLine("Unable to get stack information:\n" + ex.Message);
return false;
}
catch (AmazonServiceException ex)
{
if (ex.Message.Contains("Unable to get IAM security credentials"))
{
Console.WriteLine(ex.Message);
Console.WriteLine("If you are usnig SSO, be sure to install" +
" the AWSSDK.SSO and AWSSDK.SSOOIDC packages.");
}
else
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
}

return false;
}
catch (ArgumentNullException ex)
{
if (ex.Message.Contains("Options property cannot be empty: ClientName"))
{
Console.WriteLine(ex.Message);
Console.WriteLine("If you are using SSO, have you logged in?");
}
else
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
}

return false;
}
}
}// snippet-end:[CloudFormation.dotnetv4.CloudFormationActions.HelloCloudFormation]
39 changes: 39 additions & 0 deletions dotnetv4/Cloudformation/CloudFormationExamples.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.2.32630.192
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Actions", "Actions", "{7907FB6A-1353-4735-95DC-EEC5DF8C0649}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{5455D423-2AFC-4BC6-B79D-9DC4270D8F7D}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CloudFormationActions", "Actions\CloudFormationActions.csproj", "{796910FA-6E94-460B-8CB4-97DF01B9ADC8}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CloudFormationTests", "Tests\CloudFormationTests.csproj", "{6046A2FC-6A39-4C2D-8DD9-AA3740B17B88}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{796910FA-6E94-460B-8CB4-97DF01B9ADC8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{796910FA-6E94-460B-8CB4-97DF01B9ADC8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{796910FA-6E94-460B-8CB4-97DF01B9ADC8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{796910FA-6E94-460B-8CB4-97DF01B9ADC8}.Release|Any CPU.Build.0 = Release|Any CPU
{6046A2FC-6A39-4C2D-8DD9-AA3740B17B88}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6046A2FC-6A39-4C2D-8DD9-AA3740B17B88}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6046A2FC-6A39-4C2D-8DD9-AA3740B17B88}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6046A2FC-6A39-4C2D-8DD9-AA3740B17B88}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{796910FA-6E94-460B-8CB4-97DF01B9ADC8} = {7907FB6A-1353-4735-95DC-EEC5DF8C0649}
{6046A2FC-6A39-4C2D-8DD9-AA3740B17B88} = {5455D423-2AFC-4BC6-B79D-9DC4270D8F7D}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {870D888D-5C8B-4057-8722-F73ECF38E513}
EndGlobalSection
EndGlobal
97 changes: 97 additions & 0 deletions dotnetv4/Cloudformation/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# CloudFormation code examples for the SDK for .NET

## Overview

Shows how to use the AWS SDK for .NET to work with AWS CloudFormation.

<!--custom.overview.start-->
<!--custom.overview.end-->

_CloudFormation enables you to create and provision AWS infrastructure deployments predictably and repeatedly._

## ⚠ Important

* Running this code might result in charges to your AWS account. For more details, see [AWS Pricing](https://aws.amazon.com/pricing/) and [Free Tier](https://aws.amazon.com/free/).
* Running the tests might result in charges to your AWS account.
* We recommend that you grant your code least privilege. At most, grant only the minimum permissions required to perform the task. For more information, see [Grant least privilege](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege).
* This code is not tested in every AWS Region. For more information, see [AWS Regional Services](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services).

<!--custom.important.start-->
<!--custom.important.end-->

## Code examples

### Prerequisites

For prerequisites, see the [README](../README.md#Prerequisites) in the `dotnetv4` folder.


<!--custom.prerequisites.start-->
<!--custom.prerequisites.end-->

### Get started

- [Hello CloudFormation](Actions/HelloCloudFormation.cs#L4) (`DescribeStackResources`)


<!--custom.examples.start-->
<!--custom.examples.end-->

## Run the examples

### Instructions

For general instructions to run the examples, see the
[README](../README.md#building-and-running-the-code-examples) in the `dotnetv4` folder.

Some projects might include a settings.json file. Before compiling the project,
you can change these values to match your own account and resources. Alternatively,
add a settings.local.json file with your local settings, which will be loaded automatically
when the application runs.

After the example compiles, you can run it from the command line. To do so, navigate to
the folder that contains the .csproj file and run the following command:

```
dotnet run
```

Alternatively, you can run the example from within your IDE.


<!--custom.instructions.start-->
<!--custom.instructions.end-->

#### Hello CloudFormation

This example shows you how to get started using CloudFormation.



### Tests

⚠ Running tests might result in charges to your AWS account.


To find instructions for running these tests, see the [README](../README.md#Tests)
in the `dotnetv4` folder.



<!--custom.tests.start-->
<!--custom.tests.end-->

## Additional resources

- [CloudFormation User Guide](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html)
- [CloudFormation API Reference](https://docs.aws.amazon.com/AWSCloudFormation/latest/APIReference/Welcome.html)
- [SDK for .NET CloudFormation reference](https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/CloudFormation/NCloudFormation.html)

<!--custom.resources.start-->
<!--custom.resources.end-->

---

Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.

SPDX-License-Identifier: Apache-2.0
28 changes: 28 additions & 0 deletions dotnetv4/Cloudformation/Tests/CloudFormationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

using Amazon.CloudFormation;
using CloudFormationActions;

namespace CloudFormationTests;

public class CloudFormationTests
{
/// <summary>
/// Run the list resources action. Should return true.
/// </summary>
/// <returns></returns>
[Fact]
[Trait("Category", "Integration")]
public async Task TestListResources()
{
// Arrange.
HelloCloudFormation._amazonCloudFormation = new AmazonCloudFormationClient();

// Act.
var success = await HelloCloudFormation.ListResources();

// Assert.
Assert.True(success);
}
}
43 changes: 43 additions & 0 deletions dotnetv4/Cloudformation/Tests/CloudFormationTests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AWSSDK.CloudFormation" Version="4.0.0-preview.4" />
<PackageReference Include="AWSSDK.SSO" Version="4.0.0-preview.4" />
<PackageReference Include="AWSSDK.SSOOIDC" Version="4.0.0-preview.4" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="9.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="System.Text.Json" Version="9.0.0" />
<PackageReference Include="System.Text.RegularExpressions" Version="4.3.1" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<Content Include="testsettings.*.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<DependentUpon>testsettings.json</DependentUpon>
</Content>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Actions\CloudFormationActions.csproj" />
</ItemGroup>

</Project>
7 changes: 7 additions & 0 deletions dotnetv4/Cloudformation/Tests/Usings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

global using Xunit;

// Optional.
[assembly: CollectionBehavior(DisableTestParallelization = true)]
39 changes: 39 additions & 0 deletions dotnetv4/DotNetV4Examples.sln
Original file line number Diff line number Diff line change
@@ -95,6 +95,20 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Converse", "Bedrock-runtime
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BedrockRuntimeActions", "Bedrock-runtime\Actions\BedrockRuntimeActions.csproj", "{05E93A3E-CFA0-4980-8EE5-CD25C7ED766D}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Cloudformation", "Cloudformation", "{5FBEAD92-9234-4824-9320-2052D236C9CD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CloudFormationTests", "Cloudformation\Tests\CloudFormationTests.csproj", "{AAFC86EB-49D7-4FD8-8C79-C42C129EB75A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CloudFormationActions", "Cloudformation\Actions\CloudFormationActions.csproj", "{98A11016-DD41-4848-A848-51D703951A91}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "CloudWatch", "CloudWatch", "{CED87D19-7F82-4D67-8A30-3EE085D07E45}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CloudWatchTests", "CloudWatch\Tests\CloudWatchTests.csproj", "{106FBE12-6FF7-40DC-9B3C-E5F67F335B32}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CloudWatchScenario", "CloudWatch\Scenarios\CloudWatchScenario.csproj", "{565A9701-3D9C-49F8-86B7-D256A1D9E074}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CloudWatchActions", "CloudWatch\Actions\CloudWatchActions.csproj", "{EAF4A3B8-5CD0-48ED-B848-0EA6D451B8D3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -237,6 +251,26 @@ Global
{05E93A3E-CFA0-4980-8EE5-CD25C7ED766D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{05E93A3E-CFA0-4980-8EE5-CD25C7ED766D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{05E93A3E-CFA0-4980-8EE5-CD25C7ED766D}.Release|Any CPU.Build.0 = Release|Any CPU
{AAFC86EB-49D7-4FD8-8C79-C42C129EB75A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AAFC86EB-49D7-4FD8-8C79-C42C129EB75A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AAFC86EB-49D7-4FD8-8C79-C42C129EB75A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AAFC86EB-49D7-4FD8-8C79-C42C129EB75A}.Release|Any CPU.Build.0 = Release|Any CPU
{98A11016-DD41-4848-A848-51D703951A91}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{98A11016-DD41-4848-A848-51D703951A91}.Debug|Any CPU.Build.0 = Debug|Any CPU
{98A11016-DD41-4848-A848-51D703951A91}.Release|Any CPU.ActiveCfg = Release|Any CPU
{98A11016-DD41-4848-A848-51D703951A91}.Release|Any CPU.Build.0 = Release|Any CPU
{106FBE12-6FF7-40DC-9B3C-E5F67F335B32}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{106FBE12-6FF7-40DC-9B3C-E5F67F335B32}.Debug|Any CPU.Build.0 = Debug|Any CPU
{106FBE12-6FF7-40DC-9B3C-E5F67F335B32}.Release|Any CPU.ActiveCfg = Release|Any CPU
{106FBE12-6FF7-40DC-9B3C-E5F67F335B32}.Release|Any CPU.Build.0 = Release|Any CPU
{565A9701-3D9C-49F8-86B7-D256A1D9E074}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{565A9701-3D9C-49F8-86B7-D256A1D9E074}.Debug|Any CPU.Build.0 = Debug|Any CPU
{565A9701-3D9C-49F8-86B7-D256A1D9E074}.Release|Any CPU.ActiveCfg = Release|Any CPU
{565A9701-3D9C-49F8-86B7-D256A1D9E074}.Release|Any CPU.Build.0 = Release|Any CPU
{EAF4A3B8-5CD0-48ED-B848-0EA6D451B8D3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EAF4A3B8-5CD0-48ED-B848-0EA6D451B8D3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EAF4A3B8-5CD0-48ED-B848-0EA6D451B8D3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EAF4A3B8-5CD0-48ED-B848-0EA6D451B8D3}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@@ -284,6 +318,11 @@ Global
{48041B99-B3B8-4970-B9AA-AB2591EA5E55} = {017F0D68-919C-41EF-9E33-087D91BA55CE}
{8D8B72F0-E17E-4A85-93B1-D035E5B81A33} = {017F0D68-919C-41EF-9E33-087D91BA55CE}
{05E93A3E-CFA0-4980-8EE5-CD25C7ED766D} = {D859B39C-9106-4D3D-8C57-11B15FA8106B}
{AAFC86EB-49D7-4FD8-8C79-C42C129EB75A} = {5FBEAD92-9234-4824-9320-2052D236C9CD}
{98A11016-DD41-4848-A848-51D703951A91} = {5FBEAD92-9234-4824-9320-2052D236C9CD}
{106FBE12-6FF7-40DC-9B3C-E5F67F335B32} = {CED87D19-7F82-4D67-8A30-3EE085D07E45}
{565A9701-3D9C-49F8-86B7-D256A1D9E074} = {CED87D19-7F82-4D67-8A30-3EE085D07E45}
{EAF4A3B8-5CD0-48ED-B848-0EA6D451B8D3} = {CED87D19-7F82-4D67-8A30-3EE085D07E45}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {08502818-E8E1-4A91-A51C-4C8C8D4FF9CA}