diff --git a/.doc_gen/metadata/cloudformation_metadata.yaml b/.doc_gen/metadata/cloudformation_metadata.yaml new file mode 100644 index 00000000000..220072e805d --- /dev/null +++ b/.doc_gen/metadata/cloudformation_metadata.yaml @@ -0,0 +1,16 @@ +cloudformation_Hello: + title: Hello &CFN; + title_abbrev: Hello &CFN; + synopsis: get started using &CFN;. + category: Hello + languages: + .NET: + versions: + - sdk_version: 3 + github: dotnetv3/CloudFormation + excerpts: + - description: + snippet_tags: + - CloudFormation.dotnetv3.CloudFormationActions.HelloCloudFormation + services: + cloudformation: {DescribeStackResources} diff --git a/dotnetv3/CloudFormation/Actions/CloudFormationActions.csproj b/dotnetv3/CloudFormation/Actions/CloudFormationActions.csproj new file mode 100644 index 00000000000..56173087047 --- /dev/null +++ b/dotnetv3/CloudFormation/Actions/CloudFormationActions.csproj @@ -0,0 +1,19 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + + + + + + diff --git a/dotnetv3/CloudFormation/Actions/HelloCloudFormation.cs b/dotnetv3/CloudFormation/Actions/HelloCloudFormation.cs new file mode 100644 index 00000000000..1fe2b96f727 --- /dev/null +++ b/dotnetv3/CloudFormation/Actions/HelloCloudFormation.cs @@ -0,0 +1,111 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +// snippet-start:[CloudFormation.dotnetv3.CloudFormationActions.HelloCloudFormation] +using Amazon.CloudFormation; +using Amazon.CloudFormation.Model; +using Amazon.Runtime; + +namespace CloudFormationActions; + +public static class HelloCloudFormation +{ + public static IAmazonCloudFormation _amazonCloudFormation; + + 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(); + } + + /// + /// Method to list stack resources and other information. + /// + /// True if successful. + public static async Task ListResources() + { + try + { + Console.WriteLine("Getting CloudFormation stack information..."); + + // Get all stacks using the stack paginator. + var paginatorForDescribeStacks = + _amazonCloudFormation.Paginators.DescribeStacks( + new DescribeStacksRequest()); + 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.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.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.dotnetv3.CloudFormationActions.HelloCloudFormation] \ No newline at end of file diff --git a/dotnetv3/CloudFormation/CloudFormationExamples.sln b/dotnetv3/CloudFormation/CloudFormationExamples.sln new file mode 100644 index 00000000000..ed8f4c34a97 --- /dev/null +++ b/dotnetv3/CloudFormation/CloudFormationExamples.sln @@ -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 diff --git a/dotnetv3/CloudFormation/README.md b/dotnetv3/CloudFormation/README.md new file mode 100644 index 00000000000..22403926f6e --- /dev/null +++ b/dotnetv3/CloudFormation/README.md @@ -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. + + + + +_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). + + + + +## Code examples + +### Prerequisites + +For prerequisites, see the [README](../README.md#Prerequisites) in the `dotnetv3` folder. + + + + + +### Get started + +- [Hello CloudFormation](Actions/HelloCloudFormation.cs#L4) (`DescribeStackResources`) + + + + + +## Run the examples + +### Instructions + +For general instructions to run the examples, see the +[README](../README.md#building-and-running-the-code-examples) in the `dotnetv3` 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. + + + + + +#### 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 `dotnetv3` folder. + + + + + + +## 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) + + + + +--- + +Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + +SPDX-License-Identifier: Apache-2.0 \ No newline at end of file diff --git a/dotnetv3/CloudFormation/Tests/CloudFormationTests.cs b/dotnetv3/CloudFormation/Tests/CloudFormationTests.cs new file mode 100644 index 00000000000..4bd7467ea33 --- /dev/null +++ b/dotnetv3/CloudFormation/Tests/CloudFormationTests.cs @@ -0,0 +1,29 @@ +// 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 +{ + /// + /// Run the list resources action. Should return true. + /// + /// + [Fact] + [Order(1)] + [Trait("Category", "Integration")] + public async Task TestListResources() + { + // Arrange. + HelloCloudFormation._amazonCloudFormation = new AmazonCloudFormationClient(); + + // Act. + var success = await HelloCloudFormation.ListResources(); + + // Assert. + Assert.True(success); + } +} \ No newline at end of file diff --git a/dotnetv3/CloudFormation/Tests/CloudFormationTests.csproj b/dotnetv3/CloudFormation/Tests/CloudFormationTests.csproj new file mode 100644 index 00000000000..9dd757d53f8 --- /dev/null +++ b/dotnetv3/CloudFormation/Tests/CloudFormationTests.csproj @@ -0,0 +1,40 @@ + + + + net6.0 + enable + enable + + false + + + + + + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + PreserveNewest + testsettings.json + + + + + + + + diff --git a/dotnetv3/CloudFormation/Tests/Usings.cs b/dotnetv3/CloudFormation/Tests/Usings.cs new file mode 100644 index 00000000000..f3d4698204b --- /dev/null +++ b/dotnetv3/CloudFormation/Tests/Usings.cs @@ -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")] \ No newline at end of file diff --git a/dotnetv3/README.md b/dotnetv3/README.md index 286bbcf17ac..544595a5fcf 100644 --- a/dotnetv3/README.md +++ b/dotnetv3/README.md @@ -34,7 +34,7 @@ Developer Guide](https://docs.aws.amazon.com/sdk-for-net/latest/developer-guide/ - AWS credentials, either configured in a local AWS credentials file, or by setting the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables. -For more information, see the [AWS Tools and SDKs Shared Configuration and Credentials Reference Guide](https://docs.aws.amazon.com/credref/latest/refdocs/overview.html). +For more information, see the authentication topics in the [AWS SDK for .NET Developer Guide](https://docs.aws.amazon.com/sdk-for-net/v3/developer-guide/creds-idc.html) and the [AWS Tools and SDKs Shared Configuration and Credentials Reference Guide](https://docs.aws.amazon.com/sdkref/latest/guide/access.html). ## Building and running the code examples diff --git a/rustv1/examples/cloudformation/README.md b/rustv1/examples/cloudformation/README.md index 86e21a1803e..f1f8789aa78 100644 --- a/rustv1/examples/cloudformation/README.md +++ b/rustv1/examples/cloudformation/README.md @@ -1,94 +1,76 @@ -# AWS SDK for Rust code examples for CloudFormation +# CloudFormation code examples for the SDK for Rust -## Purpose +## Overview -These examples demonstrate how to perform several CloudFormation operations using the alpha version of the AWS SDK for Rust. -AWS CloudFormation (CloudFormation) enables you to use a template file to create and delete a collection of resources together as a single unit (a stack). +Shows how to use the AWS SDK for Rust to work with AWS CloudFormation. -## Code examples + + -- [Create a CloudFormation stack](src/bin/create-stack.rs) (CreateStack) -- [Delete a CloudFormation stack](src/bin/delete-stack.rs) (DeleteStack) -- [Get CloudFormation stack status](src/bin/describe-stack.rs) (DescribeStacks) -- [Lists your CloudFormation stacks](src/bin/list-stacks.rs) (ListStacks) +_CloudFormation enables you to create and provision AWS infrastructure deployments predictably and repeatedly._ ## ⚠ Important -- We recommend that you grant this code least privilege, - or at most 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) - in the AWS Identity and Access Management User Guide. -- This code has not been tested in all AWS Regions. - Some AWS services are available only in specific - [Regions](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services). -- Running this code might result in charges to your AWS account. +* 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). + + + -## Running the code examples +## Code examples ### Prerequisites -You must have an AWS account, and have configured your default credentials and AWS Region as described in [https://github.com/awslabs/aws-sdk-rust](https://github.com/awslabs/aws-sdk-rust). +For prerequisites, see the [README](../../README.md#Prerequisites) in the `rustv1` folder. + -### create-stack + + -This example creates a CloudFormation stack in the region. + +### Single actions +- [Create a CloudFormation stack](src/bin/create-stack.rs) (CreateStack) +- [Delete a CloudFormation stack](src/bin/delete-stack.rs) (DeleteStack) +- [Get CloudFormation stack status](src/bin/describe-stack.rs) (DescribeStacks) +- [Lists your CloudFormation stacks](src/bin/list-stacks.rs) (ListStacks) + -`cargo run --bin create-stack -- -s STACK-NAME -t TEMPLATE-FILE [-d DEFAULT-REGION] [-v]` +## Run the examples -- _STACK-NAME_ is name of the stack. -- _TEMPLATE-FILE_ is name of the template file, in either JSON or YAML format. -- _DEFAULT-REGION_ is name of the AWS Region, such as __us-east-1__, where the stacks are located. - If not supplied, uses the value of the __AWS_DEFAULT_REGION__ or __AWS_REGION__ environment variable. - If the environment variable is not set, defaults to __us-west-2__. -- __-v__ displays additional information. +### Instructions -### delete-stack -This example deletes a CloudFormation stack in the region. + + -`cargo run --bin delete-stack -- -s STACK-NAME [-d DEFAULT-REGION] [-v]` -- _STACK-NAME_ is name of the stack. -- _DEFAULT-REGION_ is name of the AWS Region, such as __us-east-1__, where the stacks are located. - If not supplied, uses the value of the __AWS_DEFAULT_REGION__ or __AWS_REGION__ environment variable. - If the environment variable is not set, defaults to __us-west-2__. -- __-v__ displays additional information. -### describe-stack +### Tests -This example retrieves the status of a CloudFormation stack in the region. +⚠ Running tests might result in charges to your AWS account. -`cargo run --bin describe-stack -- -s STACK-NAME [-d DEFAULT-REGION] [-v]` -- _STACK-NAME_ is name of the stack. - If the stack does not exist, the code panics. -- _DEFAULT-REGION_ is name of the AWS Region, such as __us-east-1__, where the stacks are located. - If not supplied, uses the value of the __AWS_DEFAULT_REGION__ or __AWS_REGION__ environment variable. - If the environment variable is not set, defaults to __us-west-2__. -- __-v__ displays additional information. +To find instructions for running these tests, see the [README](../../README.md#Tests) +in the `rustv1` folder. -### list-stacks -This example lists the name and status of your CloudFormation stacks in the region. -`cargo run --bin list-stacks -- [-d DEFAULT-REGION] [-v]` + + -- _DEFAULT-REGION_ is name of the AWS Region, such as __us-east-1__, where the stacks are located. - If not supplied, uses the value of the __AWS_DEFAULT_REGION__ or __AWS_REGION__ environment variable. - If the environment variable is not set, defaults to __us-west-2__. -- __-v__ displays additional information. +## Additional resources -## 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 Rust CloudFormation reference](https://docs.rs/aws-sdk-cloudformation/latest/aws_sdk_cloudformation/) -- [AWS SDK for Rust repo](https://github.com/awslabs/aws-sdk-rust) -- [AWS SDK for Rust API Reference for CloudFormation](https://docs.rs/aws-sdk-cloudformation) -- [AWS SDK for Rust Developer Guide](https://docs.aws.amazon.com/sdk-for-rust/latest/dg) + + -## Contributing +--- -To propose a new code example to the AWS documentation team, -see [CONTRIBUTING.md](https://github.com/awsdocs/aws-doc-sdk-examples/blob/master/CONTRIBUTING.md). -The team prefers to create code examples that show broad scenarios rather than individual API calls. +Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. SPDX-License-Identifier: Apache-2.0 +SPDX-License-Identifier: Apache-2.0 \ No newline at end of file