Automated Enterprise Deployment of AWS Config
Tags: tutorial, aws, devops
Published: 2022-04-14 15:35:48

<!-- wp:heading {"level":3} -->
<h3>AWS Config - powerful tool but a deployment headache</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Seasoned users of AWS will be familiar with the <a href="https://aws.amazon.com/config/">AWS Config</a> service.  Config is a powerful tool that lets you continuously monitor and record the configuration of AWS resources in your account.   Using Config, you can easily see changes to configuration of a given AWS resource on a timeline, see who made a given configuration change, and see relationships between resources in your account.   Config also offers a powerful feature that allows you to setup governance rules to alert you when resources don't match your desired guidelines.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>AWS recommends setting up AWS Config in each of your AWS account(s) as a best practice.  Experienced users of Config know that this can rapidly become a cumbersome process if you have more than a few AWS accounts.   AWS Config is a regional service which means you need to enable it in every region in which you operate.  This can be a mind-numbing process using the AWS Console, and even using AWS CLI it is a complicated multi-step affair:</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>Create an S3 bucket</li><li>Create an IAM Role</li><li>Create an SNS topic</li><li>Create configuration recorder</li><li>Create a delivery channel</li><li>Start the Configuration recorder</li></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>Now imagine doing this doing this twenty times (once for each region) across 150 AWS accounts and I think you can begin to imagine the pain and suffering involved.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>For admins of large multi-account environments that live within an AWS Organization, I'm happy to share there is a much easier way to deploy Config across all accounts and regions at once and for new accounts in your organization to have Config enabled automatically.  The magic?  CloudFormation StackSets to the rescue!</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>The Solution</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>In this solution, we will be setting up an AWS Organization wide Config service deployment, with all configuration recorder data being logged to a single centralized Amazon S3 bucket.   Config service will be enabled in every account and region leveraging <a href="https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/what-is-cfnstacksets.html">CloudFormation StackSets</a>.</p>
<!-- /wp:paragraph -->


![AWS Config Org Deployment Architecture](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9kt5f3q99mbak86dqyaq.png)

<!-- wp:heading {"level":3} -->
<h3>Prerequisites</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>In order to deploy this solution successfully there are a few requirements that must be taken care of first.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":5} -->
<h5>AWS Organizations</h5>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>In order to leverage AWS StackSets, your AWS accounts must be members of an AWS Organization.  Your accounts should be organized into one or more Organizational Units (OUs).  I also recommend creating a "Testing" OU where you can try out your StackSet deployments on a test account before unleashing them on all accounts in your organization.   In addition your AWS Organization should have <a href="https://docs.aws.amazon.com/organizations/latest/userguide/orgs_manage_org_support-all-features.html">all features enabled</a>, not just consolidated billing.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":5} -->
<h5>Enable Stack Sets Trusted Access</h5>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>We will be creating a StackSet with "service-managed" permissions.  I highly recommend this option because with service-managed permissions, you do not need to worry about creating IAM roles in each account you will be deploying in...StackSets will take care of that for you automatically.  <a href="https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/stacksets-concepts.html#stacksets-concepts-stackset-permission-models">This AWS documentation page</a> provides more details on the different permissions models for StackSets deployments.  Before you can start deploying StackSets with service-managed permissions, you must <a href="https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/stacksets-orgs-enable-trusted-access.html">enabled trusted access with AWS Organizations</a>.   There are multiple ways to enable this, but the simplest way to do so is via the CloudFormation console.   </p>
<!-- /wp:paragraph -->

<!-- wp:group -->
<div class="wp-block-group"><!-- wp:columns -->
<div class="wp-block-columns"><!-- wp:column {"width":"100%"} -->
<div class="wp-block-column" style="flex-basis:100%"><!-- wp:list {"ordered":true} -->
<ol><li>Sign into your Organizations management account as a user with Administrator Access</li><li>Navigate to the AWS CloudFormation console</li><li>In the left-side navigation pane, choose <strong>StackSets</strong>.  If trusted access is not currently enabled, a banner will appear at the top of the page that prompts you to enable trusted access.</li><li>Click the Enable trusted access button to enable this feature.</li></ol>
<!-- /wp:list --></div>
<!-- /wp:group -->


![Enable Trusted Access button](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/exj5a5mjyn0tsm37ivhe.png)

Trusted access is successfully enabled when the following green banner displays:


![Successfully enabled trusted access](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u3xhv3u0iy3bh6661dey.png)

<!-- wp:heading {"level":5} -->
<h5>Centralized S3 Bucket</h5>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Rather than deploying an S3 bucket for each linked member account, this architecture relies on a single S3 bucket that all accounts write their logs to.  If you are managing a large multi-account environment, you probably already have a centralized logging account, and this is where you should place this new bucket for config logging.  Create an appropriately named S3 bucket in your logging account and ensure that the Block Public Access settings are enabled for the bucket.  Then apply a bucket policy on the bucket that allows every account in the organization to access the bucket as well as the AWS Config service to access the bucket. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Below is a example bucket policy that demonstrates this configuration by leveraging the <strong>aws:PrincipalOrgID</strong> conditional to allow access from every account in the org and then uses Principal "Service": "config.amazonaws.com" to allow AWS Config the access it needs to the bucket.</p>
<!-- /wp:paragraph -->

```
{
    "Version": "2008-10-17",
    "Id": "Policy1357935677554",
    "Statement": [
        {
            "Sid": "ListBucketAccess",
            "Effect": "Allow",
            "Principal": "*",
            "Action": [
                "s3:ListBucket",
                "s3:GetObject"
            ],
            "Resource": [
                "arn:aws:s3:::my-config-bucket",
                "arn:aws:s3:::my-config-bucket/*"
            ],
            "Condition": {
                "StringEquals": {
                    "aws:PrincipalOrgID": "o-1111111111"
                }
            }
        },
        {
            "Sid": "PutObjectAccess",
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:PutObject",
            "Resource": "arn:aws:s3:::my-config-bucket/AWSLogs/*",
            "Condition": {
                "StringEquals": {
                    "aws:PrincipalOrgID": "o-1111111111"
                }
            }
        },
        {
            "Sid": "AWSConfigBucketPermissionsCheck",
            "Effect": "Allow",
            "Principal": {
                "Service": "config.amazonaws.com"
            },
            "Action": "s3:GetBucketAcl",
            "Resource": "arn:aws:s3:::my-config-bucket"
        },
        {
            "Sid": "AWSConfigBucketExistenceCheck",
            "Effect": "Allow",
            "Principal": {
                "Service": "config.amazonaws.com"
            },
            "Action": "s3:ListBucket",
            "Resource": "arn:aws:s3:::my-config-bucket"
        },
        {
            "Sid": "AWSConfigBucketDelivery",
            "Effect": "Allow",
            "Principal": {
                "Service": "config.amazonaws.com"
            },
            "Action": "s3:PutObject",
            "Resource": "arn:aws:s3:::my-config-bucket/AWSLogs/*",
            "Condition": {
                "StringEquals": {
                    "s3:x-amz-acl": "bucket-owner-full-control"
                }
            }
        }
    ]
}
```

<!-- wp:paragraph -->
<p>With these prerequisites out of the way, we can now proceed with deployment of the solution using CloudFormation StackSets.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>StackSet Deployment</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>StackSets are created in the AWS Organizations management account, using the same CloudFormation template concept as a normal CloudFormation stack.   A StackSet is created and targeted at one or more Organizational Units (OUs), and CloudFormation deploys a Stack in each targeted account and region.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>For this deployment, we will be leveraging a pre-built template from AWS, making our job easier.   Follow this procedure to create a new CloudFormation StackSet in your AWS Organizations Management account</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>Log into the AWS Console in your Management account with a user that has Administrator Access permissions</li><li>Navigate to the CloudFormation service console</li><li>In the left-side navigation pane, choose <strong>StackSets</strong>.</li><li>Click the orange <strong>Create StackSet</strong> button to start the new StackSet deployment workflow</li></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>Below I will walk you through each step of the deployment workflow</p>

<!-- wp:heading {"level":5} -->
<h5>Step 1: Choose a template</h5>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Ensure <strong>Service-managed permissions</strong> is selected and select the option to Use a sample template.  From the Sample templates drop down, select the option "<strong>Enable AWS Config with central logging</strong>". </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Then click the orange <strong>Next</strong> button.</p>


![StackSet Deploy Step 1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/669xxi4tr84e02kcpnvm.png)

<!-- wp:heading {"level":5} -->

<h5>Step 2: Specify Stack Set Details</h5>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Give your StackSet a meaningful name.  You can leave all Parameters as default, with the following exception.</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li>Under Central S3 bucket, enter the Name of the S3 bucket you created to store your config logs.   NOTE: do not enter an ARN here, just the name of the bucket</li></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>Optionally, you can modify the following:</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li>You can choose to include global resource types by changing that setting to true.</li><li>You can modify the frequency of with which AWS config delivers configuration snapshots by modifying the Snapshot delivery frequency.</li><li>You can specify an email address for AWS Config notifications in the Notification Email field.</li></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>When finished configuring options, click <strong>Next</strong>.</p>


![StackSet Deploy Step 2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m0sluedssuooa8k7ieyw.png)

<!-- wp:heading {"level":5} -->
<h5>Step 3: Configure Stack Set Options</h5>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Apply any tags that you want for your StackSet in this screen.  You can leave Managed Execution set to Inactive or change it to Active if you want CloudFormation to perform non-conflicting operations concurrently.   When finished click the orange Next button.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":5} -->
<h5>Step 4: Set deployment options</h5>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Leave the deployment option set to Deploy new stacks.  </p>

<p>For deployment targets, I always recommend starting out with a testing OU when deploying new StackSets to make sure there are no issues before targeting the entire organization.   Select the radio button for Deploy to organizational units and enter the AWS OU ID in the text box.  (You can retrieve OU IDs from the AWS Organizations console).   </p>

<p>Leave Automatic deployment set to enabled, in this way if a new account is added to the OU a new stack is automatically pushed to that account. Account removal behavior should be set to delete stacks.</p>


![StackSet Deploy Step 3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xs20gt0neroulu83xieq.png)

<!-- wp:paragraph -->
<p>In the <strong>Specify Regions</strong> section, you can choose the regions you want to target from the drop down menu or click the Add all regions. Note that you should only target regions that are enabled in your target accounts, and those regions must also be enabled in your management account.  See <a href="https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/stacksets-prereqs.html#stacksets-opt-in-regions">this note from AWS</a> for more details on this.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Finally for <strong>Deployment options</strong>, choose the Maximum concurrent accounts you wish the stack to be deployed to at once.  You can leave this at 1 or set a higher value.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>For <strong>Fault tolerance</strong>, I recommend setting this a value higher than 0, otherwise your whole deployment will stop if a single account fails in a single region.  I typically set this value to 2 or 3 so that a single error will not cause the entire StackSet deployment to fail.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Region concurrency</strong> controls how many regions will be deployed at the same time for a given account.  For faster operations, set this to Parallel.</p>

<p>When finished configuring options, click <strong>Next</strong>.</p>
</div></div>


![StackSet Deploy Step 4](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r84rpk830g11zbf97k0g.png)

<!-- wp:heading {"level":5} -->
<h5>Step 5: Review</h5>
<!-- /wp:heading -->

Review all your settings to ensure they are accurate, then click the orange Submit button.  You will be taken to the Operations tab for the new StackSet you just deployed, and it should have a status of Running


![Stack Set Operations Screen showing job status](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/72ohoq4nwdhfwxw3hp3o.png)

If all goes well, the Operation status will change from Running to Succeeded, indicating the stack was deployed successfully in all targeted accounts and regions.

![Stack Set Operation Completed Successfully](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cfggxcvzemziarly6o1o.png)

You can then click on the Stack Instances tab to see the individual CloudFormation stack deployments in each account and region.  You can also use the Stack Instances tab in the case of a deployment failure to see what went wrong.


![Stack Set Instances screen showing all deployed stacks](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tcdzjmk7s10kxlw5fr0g.png)

<!-- wp:heading {"level":3} -->
<h3>Conclusion &amp; Next Steps</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>To wrap up, in this post I've shown you how you can automatically deploy the AWS Config service across all regions for all AWS accounts in your AWS Organization, and have all configuration recorder data stored in a centralized S3 bucket.  By deploying this architecture you will have the ability to assess, audit and evaluate the configuration of all AWS resources in your account(s), and track changes to those resources over time.  In addition with this service enabled across your accounts, you can then implement additional governance across your AWS environments by leveraging <a href="https://docs.aws.amazon.com/config/latest/developerguide/evaluate-config.html" target="_blank" rel="noreferrer noopener">AWS Config Rules</a>.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Some of you may have already deployed AWS Config across your accounts in a decentralized fashion, and may be wondering how to migrate to this centralized architecture.  Stay tuned as I will be covering that in my next post in this series.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>I hope you've found this content useful.  If you have feedback or suggestions on how this content could be improved, please leave a comment below.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Learn More</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>If you want to learn more about AWS Config service here are some links for more research:</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li><a rel="noreferrer noopener" href="https://docs.aws.amazon.com/config/latest/developerguide/WhatIsConfig.html" target="_blank">AWS Config Developer Guide</a></li><li><a href="https://aws.amazon.com/config/faq/" target="_blank" rel="noreferrer noopener">AWS Config FAQs</a></li><li><a rel="noreferrer noopener" href="https://github.com/awslabs/aws-config-rdk" target="_blank">AWS Config Rules Development Kit</a> @ Github</li><li><a rel="noreferrer noopener" href="https://www.youtube.com/watch?v=kErRv4YB_T4" data-type="URL" data-id="https://www.youtube.com/watch?v=kErRv4YB_T4" target="_blank">AWS re:Invent 2018: Centrally Monitoring Resource Configuration &amp; Compliance</a></li><li><a rel="noreferrer noopener" href="https://www.youtube.com/watch?v=JXwEMipnY-o" data-type="URL" data-id="https://www.youtube.com/watch?v=JXwEMipnY-o" target="_blank">Monitoring and Remediating Non-Compliant Resources with AWS Config</a> @ AWS YouTube</li><li><a rel="noreferrer noopener" href="https://aws.amazon.com/blogs/mt/visualizing-aws-config-data-using-amazon-athena-and-amazon-quicksight/" data-type="URL" data-id="https://aws.amazon.com/blogs/mt/visualizing-aws-config-data-using-amazon-athena-and-amazon-quicksight/" target="_blank">Visualizing AWS Config data using Amazon Athena and Amazon&nbsp;QuickSight</a></li></ul>
<!-- /wp:list -->