This check ensures that AWS CodeBuild projects are encrypted using customer-managed KMS keys rather than the default AWS-managed keys. Using the default AWS-managed key (for example, encryption_key = data.aws_kms_key.by_alias.arn with key_id = "alias/aws/s3") limits your control over key rotation, access policies, and audit logging, potentially increasing the risk of unauthorized data access. To maximize security and compliance, the encryption_key attribute should reference a customer-managed KMS key (such as key_id = "alias/myAlias"), allowing organizations to fully manage encryption controls for sensitive CodeBuild project data.
Compliant Code Examples
provider"aws"{region="us-east-1"}data"aws_kms_key""by_alias2"{key_id="alias/myAlias"}# No policy attached to this role because it is for testing purposes
resource"aws_iam_role""codebuild2"{name="codebuild-cloudrail-test"assume_role_policy=<<EOF{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "codebuild.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
EOF}resource"aws_codebuild_project""project-cloudrail-test2"{name="project-cloudrail-test"description="project-cloudrail-test"build_timeout="5"queued_timeout="5"service_role=aws_iam_role.codebuild2.arnencryption_key=data.aws_kms_key.by_alias2.arnartifacts{type="NO_ARTIFACTS"}cache{type="LOCAL"modes=["LOCAL_DOCKER_LAYER_CACHE","LOCAL_SOURCE_CACHE"]}environment{compute_type="BUILD_GENERAL1_SMALL"image="aws/codebuild/standard:1.0"type="LINUX_CONTAINER"image_pull_credentials_type="CODEBUILD"environment_variable{name="SOME_KEY1"value="SOME_VALUE1"}}source{type="GITHUB"location="https://github.com/foo/bar.git"git_clone_depth=1}}
Non-Compliant Code Examples
provider"aws"{region="us-east-1"}# No policy attached to this role because it is for testing purposes
resource"aws_iam_role""codebuild"{name="codebuild-cloudrail-test"assume_role_policy=<<EOF{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "codebuild.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
EOF}resource"aws_codebuild_project""project-cloudrail-test"{name="project-cloudrail-test"description="project-cloudrail-test"build_timeout="5"queued_timeout="5"service_role=aws_iam_role.codebuild.arnencryption_key="alias/aws/s3"artifacts{type="NO_ARTIFACTS"}cache{type="LOCAL"modes=["LOCAL_DOCKER_LAYER_CACHE","LOCAL_SOURCE_CACHE"]}environment{compute_type="BUILD_GENERAL1_SMALL"image="aws/codebuild/standard:1.0"type="LINUX_CONTAINER"image_pull_credentials_type="CODEBUILD"environment_variable{name="SOME_KEY1"value="SOME_VALUE1"}}source{type="GITHUB"location="https://github.com/foo/bar.git"git_clone_depth=1}}
# encryption_key set via a data source reference (data.aws_kms_key.by_alias.arn).
# See positive4.json, a real terraform plan for this exact configuration
# (data sources requiring a live provider call still produce a usable
# plan JSON even when that call fails, since Terraform can't compute any
# attribute of aws_codebuild_project.project-cloudrail-test without the
# resolved key, so the whole resource drops out of planned_values, but
# configuration.root_module.resources keeps its unevaluated expressions
# regardless). Detected via
# tf_lib.uses_aws_managed_key_data_source_ref_config_only.
provider"aws"{region="us-east-1"}data"aws_kms_key""by_alias"{key_id="alias/aws/s3"}# No policy attached to this role because it is for testing purposes
resource"aws_iam_role""codebuild"{name="codebuild-cloudrail-test"assume_role_policy=<<EOF{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "codebuild.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
EOF}resource"aws_codebuild_project""project-cloudrail-test"{name="project-cloudrail-test"description="project-cloudrail-test"build_timeout="5"queued_timeout="5"service_role=aws_iam_role.codebuild.arnencryption_key=data.aws_kms_key.by_alias.arnartifacts{type="NO_ARTIFACTS"}cache{type="LOCAL"modes=["LOCAL_DOCKER_LAYER_CACHE","LOCAL_SOURCE_CACHE"]}environment{compute_type="BUILD_GENERAL1_SMALL"image="aws/codebuild/standard:1.0"type="LINUX_CONTAINER"image_pull_credentials_type="CODEBUILD"environment_variable{name="SOME_KEY1"value="SOME_VALUE1"}}source{type="GITHUB"location="https://github.com/foo/bar.git"git_clone_depth=1}}
provider"aws"{region="us-east-1"}# No policy attached to this role because it is for testing purposes
resource"aws_iam_role""codebuild3"{name="codebuild-cloudrail-test3"assume_role_policy=<<EOF{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "codebuild.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
EOF}resource"aws_codebuild_project""project-cloudrail-test3"{name="project-cloudrail-test3"description="project-cloudrail-test3"build_timeout="5"queued_timeout="5"service_role=aws_iam_role.codebuild3.arnencryption_key="arn:aws:kms:us-east-1:123456789012:alias/aws/s3"artifacts{type="NO_ARTIFACTS"}cache{type="LOCAL"modes=["LOCAL_DOCKER_LAYER_CACHE","LOCAL_SOURCE_CACHE"]}environment{compute_type="BUILD_GENERAL1_SMALL"image="aws/codebuild/standard:1.0"type="LINUX_CONTAINER"image_pull_credentials_type="CODEBUILD"environment_variable{name="SOME_KEY1"value="SOME_VALUE1"}}source{type="GITHUB"location="https://github.com/foo/bar.git"git_clone_depth=1}}
1
2
rulesets:- Terraform / AWS # Rules to enforce / AWS.
Request a personalized demo
Get Started with Datadog
Ask AI
AI-generated responses may be inaccurate. Verify important info.