AWSSDK.CloudFormation (for AWS CloudFormation)
Tags: dotnet, cloud, aws, csharp
Published: 2021-09-11 14:26:31

✦ The Fourth SDK we are going to talk about is `AWSSDK.CloudFormation`.

First of all, let's have a brief about `Amazon CloudFormation` in AWS...
#➽What is Amazon CloudFormation?
☞ `AWS CloudFormation` is a service that helps you model and set up your **AWS resources** so that you can spend less time managing those resources and more time focusing on your applications that run in AWS. 
☞ You create a template that describes all the AWS resources that you want (like `Amazon EC2` instances or `Amazon RDS` DB instances), and `CloudFormation` takes care of provisioning and configuring those resources for you.
☞ You don't need to individually create and configure AWS resources and figure out what's dependent on what; `CloudFormation` handles that.
☞ Read more about [Amazon CloudFormation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html)
_____________________________________________________
#➽Installing AWSSDK.CloudFormation :
☞`AWSSDK.CloudFormation` is installed mainly from [Nuget] (https://www.nuget.org/)
☞There is 3 ways to install `AWSSDK.CloudFormation`, they are the same as installing `AWSSDK.S3` from [Part 1 of this series](https://dev.to/ahmedadel/aws-sdks-for-net-1-awssdk-s3-5e36)
☞let's use the easiest one, from Package Manager Console by using the Install-Package command. 
```csharp
PM> Install-Package AWSSDK.CloudFormation
```
_____________________________________________________
🌟 Second step is to connect to our AWS account using __ Access keys (Access Key ID and Secret Access Key)__, this was explained before briefly in the [first article under (Get AWS Access keys) ](https://dev.to/ahmedadel/aws-sdks-for-net-1-awssdk-s3-5e36#%E2%9E%BDso-how-can-we-use-it-)
_________________________________________________________
✦ The AWS SDK for .NET provides APIs for `AWS CloudFormation` clients. The APIs enable you to work with `AWS CloudFormation` features such as **templates** and **stacks**.
✦ The example uses the low-level API. The application takes no arguments, but simply gathers information for all stacks that are accessible to the user's credentials and then displays information
about those stacks.
 ```csharp
using System;
using System.Threading.Tasks;
using Amazon.CloudFormation;
using Amazon.CloudFormation.Model;

namespace CFNListResources
{
    class Program
    {
        static async Task Main(string[] args)
        {
            // Create the CloudFormation client
            var cfnClient = new AmazonCloudFormationClient();

            // List the resources for each stack
            await ListResources(cfnClient, await cfnClient.DescribeStacksAsync());
        }

        //
        // Method to list stack resources and other information
        private static async Task ListResources(
          IAmazonCloudFormation cfnClient, DescribeStacksResponse responseDescribeStacks)
        {
            Console.WriteLine("Getting CloudFormation stack information...");

            foreach (Stack stack in responseDescribeStacks.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 cfnClient.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------------------------------------------------");
        }
    }
}
```

__References:__ [AWS official Documentation](https://docs.aws.amazon.com/index.html)