Skip to Content
CloudAWS

Secure an AWS Account

Scan an Amazon Web Services account against security and compliance best practices with cnspec.

Scan your Amazon Web Services (AWS) account to find security risks before they become incidents. cnspec evaluates account-level settings such as IAM password policies, access key rotation, MFA enforcement, S3 public access blocks, CloudTrail encryption, and default EBS volume encryption. All you need is your existing AWS CLI credentials.

Prerequisites

To scan AWS with cnspec, you must have:

If you manage multiple accounts, set the AWS_PROFILE environment variable to choose which credentials cnspec uses:

export AWS_PROFILE=production

You can also choose the profile for a single command with --profile production. The --region flag sets the region cnspec uses to authenticate with the AWS API; it doesn't limit discovery to that region. To send API calls to a custom endpoint, pass --endpoint-url.

Verify with a quick AWS check

Confirm that cnspec can reach your AWS account:

cnspec run aws -c 'aws.account.id.length > 0'

cnspec connects to your account and returns the length of the account ID:

[ok] value: 12

If the command fails, verify that your AWS CLI credentials are configured correctly.

Scan an AWS account

Scan the account that your AWS CLI credentials authenticate to:

cnspec scan aws

Add the -o full flag to see results in detail:

cnspec scan aws -o full

To learn how to read the scan report, including scores and remediation guidance, see Understand cnspec Results.

Discover resources during a scan

By default, cnspec scan aws uses the auto discovery target. cnspec scans the account itself and discovers the account's AWS resources, such as S3 buckets, IAM users and groups, security groups, VPCs, EBS volumes, Lambda functions, RDS instances, EKS clusters, KMS keys, and Secrets Manager secrets. It scans each resource as its own asset through the AWS API. auto doesn't scan the operating systems of EC2 instances or the contents of container images.

Use the --discover flag to choose what cnspec discovers:

ValueDiscovers
autoThe account and its AWS resources. This is the default.
allEverything auto discovers, plus instances, ssm-instances, ecr, and ecs
accountsThe account itself, without its resources
instancesEC2 instances, each scanned at the operating system level over SSH. cnspec connects to running instances that have a public IP address, with a key it pushes through EC2 Instance Connect.
ssm-instancesInstances that AWS Systems Manager manages
ecrContainer images in Amazon ECR, each scanned as a container image
ecsContainers in Amazon ECS

Because cnspec connects to EC2 instances over SSH with an EC2 Instance Connect key, the instances target works only with Linux instances that support EC2 Instance Connect. When the SSM agent on such an instance also reports online, cnspec tunnels the SSH connection through an SSM session, which needs the Session Manager plugin on the machine that runs cnspec. To learn more, read Scan an SSM-managed instance from your workstation.

You can also name a single resource type, such as s3-buckets, iam-users, lambda-functions, or eks-clusters. For the complete list of --discover values, run:

cnspec scan aws -h

A --discover value replaces the default instead of adding to it. For example, this command scans only the account's S3 buckets, not the account itself or its other resources:

cnspec scan aws --discover s3-buckets

To keep the default and also scan EC2 instances at the operating system level, combine the values:

cnspec scan aws --discover auto,instances

Scan with the Mondoo AWS Security policy

Mondoo maintains an out-of-the-box AWS Security policy that checks IAM password policies, access key rotation, MFA enforcement, S3 public access blocks, CloudTrail encryption, default EBS volume encryption, and more.

Mondoo Platform users: Enable the policy in your space. In the Mondoo App, go to Findings > Policies, search for "AWS", and add the policy. All future scans of your AWS account automatically evaluate against it. To learn more, read Manage policies in Mondoo Platform.

Open source users: Pass the policy bundle URL directly to cnspec:

cnspec scan aws \
  --policy-bundle https://raw.githubusercontent.com/mondoohq/cnspec/refs/heads/main/content/mondoo-aws-security.mql.yaml

Scan using an assumed role

To assess AWS resources that you don't normally have access to, use an assumed role:

cnspec scan aws --role ROLE-ARN

For ROLE-ARN, substitute the role's Amazon resource name (ARN).

The --role flag takes only the role ARN, so it can't assume a role whose trust policy requires an external ID. For such a role, use one of these approaches:

  • Define a profile for the role in your AWS CLI config file (~/.aws/config), then select it with --profile or the AWS_PROFILE environment variable:

    [profile audit]
    role_arn = arn:aws:iam::123456789012:role/ROLE-NAME
    source_profile = default
    external_id = EXTERNAL-ID
    cnspec scan aws --profile audit
  • Set the role and external-id options on the AWS connection in an inventory file:

    spec:
      assets:
        - name: aws-account
          connections:
            - type: aws
              discover:
                targets:
                  - auto
              options:
                role: arn:aws:iam::123456789012:role/ROLE-NAME
                external-id: EXTERNAL-ID
    cnspec scan --inventory-file inventory.yml

Filter discovered resources

Use the --filters flag to limit which resources cnspec discovers and scans. Filters work with any discovery target, including the default auto. Repeat the flag to combine filters.

FilterSelects
regions=us-east-1,us-west-2Scans only the listed regions. cnspec reports an error if a listed region isn't enabled for the account.
exclude:regions=ap-south-1Skips the listed regions. cnspec ignores this filter when you also set regions.
tag:KEY=VALUEOnly resources that carry the tag with that value
exclude:tag:KEY=VALUESkips resources that carry the tag with that value
ec2:instance-ids=i-0123456789abcdef0Only the listed EC2 instances
ec2:exclude:instance-ids=i-0123456789abcdef0Skips the listed EC2 instances
s3:bucket-names=my-bucketOnly the listed S3 buckets
s3:exclude:bucket-names=my-bucketSkips the listed S3 buckets
ecr:private-repository-names=my-repoOnly the listed private ECR repositories
ecr:public-repository-names=my-repoOnly the listed public ECR repositories
ecr:scope=privateOnly private ECR repositories. Use public for only public repositories.
ecr:tags=latestOnly ECR images that carry one of the listed image tags
ecr:exclude:tags=devSkips ECR images that carry one of the listed image tags
ecs:only-running-containers=trueOnly ECS containers that are running
ecs:discover-instances=trueWith the ecs target, also discovers the ECS container instances
propagate-account-tags=trueCopies the account's tags onto every discovered asset as labels. When an asset already has a label with the same key, the asset's own value stays.

Filters that take regions, IDs, names, or tag values accept a comma-separated list. For a tag filter, a resource matches if the tag has any of the listed values, so tag:Environment=production,staging matches either value. Except for regions, an exclude filter wins over an include filter.

Tag filters apply to EC2 instances and to many other resource types, including S3 buckets, RDS instances, Lambda functions, EKS clusters, load balancers, KMS keys, and Secrets Manager secrets. They don't apply to every resource type. cnspec doesn't filter the account itself or resources such as IAM users and groups by tag. The older ec2:tag: and ec2:exclude:tag: filters still work but are deprecated. Use tag: and exclude:tag: instead.

cnspec reads the account's tags for propagate-account-tags from AWS Organizations, which works only with credentials for the organization's management account or a delegated administrator account. If cnspec can't read them, it logs a warning and continues without them.

Scan only the resources tagged Environment=production, among the resource types that tag filters apply to:

cnspec scan aws --filters tag:Environment=production

Scan EC2 instances at the operating system level, skipping those tagged Environment=dev or Environment=test:

cnspec scan aws --discover instances --filters exclude:tag:Environment=dev,test

Limit the scan to two regions:

cnspec scan aws --filters regions=us-east-1,us-west-2

How cnspec names and labels discovered EC2 instances

When cnspec discovers EC2 instances with the instances target, it names each asset after the instance's Name tag, or after the instance ID if the instance has no Name tag. It copies the instance's tags to the asset as labels and adds these labels:

LabelValue
mondoo.com/regionThe instance's AWS region
mondoo.com/instance-idThe EC2 instance ID
mondoo.com/parent-idThe ID of the AWS account the instance belongs to
mondoo.com/instance-typeThe instance type, such as t3.micro
mondoo.com/platformThe platform details AWS reports for the instance, such as Linux/UNIX or Windows
mondoo.com/imageThe ID of the AMI the instance was launched from
mondoo.com/launch-timeWhen the instance was launched
mondoo.com/ssm-connectionOnline. cnspec sets this label only when the SSM agent on the instance reports online.

Instances that cnspec discovers with the ssm-instances target get the same name and their tags as labels, but only the mondoo.com/region and mondoo.com/platform labels. For these instances, mondoo.com/platform holds the platform name that Systems Manager reports.

Explore and test checks interactively

Open a cnspec shell to discover resources and try out checks:

cnspec shell aws

Use help aws to list available resources, or drill down with help aws.iam, help aws.ec2, help aws.eks, and so on.

Query IAM credential reports

cnspec> aws.iam.credentialReport { arn passwordLastChanged mfaActive }

List EC2 instances

cnspec> aws.ec2.instances { arn tags instanceType state }

Filter EC2 instances

Find instances without an owner tag:

cnspec> aws.ec2.instances.where(tags['owner'] == null) { arn instanceType }

Find large (more expensive) instances:

cnspec> aws.ec2.instances.where(instanceType == /\.large$/) { arn instanceType }

List EKS clusters

cnspec> aws.eks.clusters { arn version status }

Query AWS Config recorders

cnspec> aws.config { recorders { name recording region } rules { state arn } }

Example security checks

Ensure CloudTrail trails are encrypted

cnspec> aws.cloudtrail.trails.all(kmsKey != null)
[ok] value: true

Ensure all IAM users have MFA enabled

cnspec> aws.iam.credentialReport.all(mfaActive == true)
[failed] [].all()
  actual:   [
    0: aws.iam.usercredentialreportentry arn="arn:aws:iam::921000052404:user/abel" {
      mfaActive: false
    }
    1: aws.iam.usercredentialreportentry arn="arn:aws:iam::921000052404:user/bob" {
      mfaActive: false
    }
  ]

Ensure no running EC2 instances have a public IP

cnspec> aws.ec2.instances.where(state == "running").all(publicIp == empty)
[ok] value: true

Ensure EBS volume encryption is enabled by default

cnspec> aws.ec2.ebsEncryptionByDefault.values.all(_ == true)
[ok] value: true

Learn more

Last updated on

On this page