Hardened AMIs and Container Images
Hardened AMIs and container images are foundational to infrastructure security in AWS because they set a trusted, minimal, and compliant baseline before any workload runs. Domain 3 of SCS-C03 expects you to know how to build, validate, distribute, and maintain these "golden images." A hardened Amazon Machine Image (AMI) starts from a trusted source, such as an AWS-provided image or a CIS-hardened image from AWS Marketplace. Hardening includes: - Removing unnecessary packages and services, and closing unused ports. - Enforcing secure SSH settings, or replacing SSH with Session Manager. - Applying the latest OS patches. - Installing the SSM Agent and the CloudWatch agent, and configuring logging. EC2 Image Builder automates this pipeline using components, recipes, and infrastructure configurations. It can apply STIG or CIS hardening components, run validation tests, and use Amazon Inspector findings before it distributes AMIs across accounts and Regions. Governance is equally important: - Share AMIs through AWS Organizations or AWS RAM. - Encrypt the underlying EBS snapshots with customer managed AWS KMS keys. - Enforce approved AMIs with AWS Config rules (approved-amis-by-id or approved-amis-by-tag), Service Control Policies, or the EC2 Allowed AMIs setting. - Use Systems Manager Patch Manager to keep running instances aligned. - Rebuild images on a schedule to prevent configuration drift, deprecate outdated AMIs, and turn on block public access for AMIs to stop accidental exposure. For containers, hardening means: - Using minimal base images, such as distroless or Alpine, and minimal hosts like Bottlerocket. - Running processes as non-root. - Stripping shells and package managers. - Keeping secrets out of images; inject them at runtime from AWS Secrets Manager or Parameter Store instead. Amazon ECR adds several protections: basic scanning, and enhanced scanning through Amazon Inspector for OS and programming-language package vulnerabilities. It also offers tag immutability, KMS encryption, and repository policies for least-privilege access. Image signing with AWS Signer and Notation, verified at deployment by admission controllers in Amazon EKS, confirms image integrity and provenance. Exam scenarios often test automated image pipelines, enforcement of approved images only, continuous vulnerability scanning, and remediating findings by rebuilding and redeploying images rather than patching live instances or containers.
Hardened AMIs and Container Images: Building Secure Foundations for AWS Workloads
Every EC2 instance starts from an Amazon Machine Image (AMI), and every container task or pod starts from a container image. If that starting point contains vulnerabilities, unnecessary software, default credentials or embedded secrets, every workload launched from it inherits those weaknesses. Hardening means reducing the attack surface of these images and proving they meet a security baseline before they reach production. For the AWS Certified Security - Specialty exam, this topic sits within the Infrastructure Security domain and often appears alongside vulnerability management, patching, compliance and detective controls.
Why It Is Important
- Scale multiplies risk: A single flawed image can be deployed to hundreds of instances or thousands of containers through Auto Scaling groups, ECS services or EKS deployments.
- Shift-left security: Fixing problems at build time is cheaper and safer than fixing them on running hosts.
- Immutable infrastructure: Rather than patching live servers, you build a new patched image and replace instances. This reduces configuration drift and makes rollbacks predictable.
- Compliance: Frameworks such as PCI DSS, HIPAA, FedRAMP and CIS require documented, consistent baselines. Hardened images provide evidence of a controlled baseline.
- Supply chain protection: Public images from unknown publishers may contain malware, crypto miners or outdated libraries. Controlling image sources protects the software supply chain.
- Shared responsibility: AWS secures the underlying hardware and hypervisor, but the guest operating system and everything in the image is your responsibility.
What a Hardened Image Is
A hardened image is a pre-configured, approved and regularly rebuilt image that follows a recognised security benchmark. Typical characteristics include:
- Based on a trusted, current source (AWS-provided AMIs, verified Marketplace publishers, or minimal official base images).
- Fully patched at build time.
- Unnecessary packages, services, ports and users removed.
- Configured to a benchmark such as CIS Benchmarks or DISA STIG.
- No hard-coded secrets, SSH private keys, access keys or passwords.
- Required agents pre-installed: SSM Agent, CloudWatch agent, Amazon Inspector support, EDR tooling.
- Logging and auditing enabled (auditd, time sync, log forwarding).
- Encrypted storage (EBS snapshots backing the AMI encrypted with AWS KMS).
- Configured to require IMDSv2 (AMIs can be registered with imds-support v2.0).
- Versioned, tagged and tracked so you know which workloads run which image.
This is often called a Golden AMI or golden image.
How It Works: Hardened AMIs
1. EC2 Image Builder
EC2 Image Builder is the AWS managed service for automating creation, testing and distribution of AMIs and container images.
- Image recipe: Defines the parent (base) image plus a list of components.
- Components: YAML documents with build and test phases. AWS supplies managed components, including STIG hardening components for Linux and Windows, and CIS-hardened base images are available from AWS Marketplace.
- Infrastructure configuration: Instance type, subnet, security group, IAM instance profile and SNS topic used during the build.
- Distribution configuration: Copies the AMI to other Regions and shares it with other accounts or AWS Organizations OUs, optionally re-encrypting with different KMS keys and setting launch permissions.
- Pipeline schedule: Rebuilds on a schedule or when the parent image is updated, so new patches flow in automatically.
- Vulnerability scanning: Image Builder integrates with Amazon Inspector to scan images during the build and report findings.
- Lifecycle policies: Deprecate, disable or delete old images and snapshots automatically.
2. Traditional Golden AMI Pipeline
Before Image Builder, a common pattern used Packer or CodePipeline + CodeBuild, Systems Manager Automation documents (for example AWS-UpdateLinuxAmi and AWS-UpdateWindowsAmi), Amazon Inspector for assessment, Lambda for approval logic and Parameter Store to publish the latest approved AMI ID. You may still see this pattern in exam scenarios.
3. Publishing and Consuming the Approved AMI
- Store the latest approved AMI ID in SSM Parameter Store; launch templates and CloudFormation reference the parameter so teams always get the current image.
- Share AMIs across accounts using launch permissions or with an Organization/OU ARN. Encrypted AMIs require the KMS key policy to grant the consuming accounts permissions (kms:Decrypt, kms:CreateGrant, kms:DescribeKey, kms:ReEncrypt*, kms:GenerateDataKey*). AMIs encrypted with the AWS managed key (aws/ebs) cannot be shared; use a customer managed key.
- Enable Block Public Access for AMIs at the account/Region level to stop accidental public sharing.
- Use AMI deprecation and disable features to retire outdated images.
4. Enforcing Use of Approved AMIs
- Preventive: IAM policies or Service Control Policies (SCPs) on ec2:RunInstances using conditions such as ec2:ImageId, ec2:Owner or ec2:ResourceTag on the image resource.
- Allowed AMIs: An account-level EC2 setting (also manageable with declarative policies in AWS Organizations) that restricts discovery and launch to AMIs from specified trusted providers.
- Detective: AWS Config managed rules approved-amis-by-id and approved-amis-by-tag flag noncompliant instances; ec2-instance-managed-by-systems-manager verifies SSM coverage.
- Remediation: Config remediation with SSM Automation, or EventBridge triggering Lambda to stop or tag noncompliant instances.
5. Ongoing Vulnerability Management
- Amazon Inspector continuously scans EC2 instances (agent-based through SSM or agentless via EBS snapshots) for software vulnerabilities and network reachability.
- Systems Manager Patch Manager applies patches to running instances based on patch baselines, though the preferred pattern is still to rebuild the AMI and roll instances.
- Security Hub aggregates Inspector and Config findings and checks controls such as the CIS AWS Foundations Benchmark and AWS Foundational Security Best Practices.
How It Works: Hardened Container Images
1. Build Practices
- Use minimal base images (distroless, Alpine, Amazon Linux minimal, Bottlerocket for hosts) to shrink the attack surface.
- Use multi-stage builds so compilers and build tools are not present in the final image.
- Run as a non-root user; drop Linux capabilities; avoid privileged mode.
- Pin base image versions or digests rather than using latest.
- Never bake secrets into layers or environment variables in the Dockerfile; inject at runtime from Secrets Manager or Parameter Store through ECS task definitions or EKS integrations (Secrets Store CSI Driver).
- EC2 Image Builder can also produce container images using container recipes and push them to Amazon ECR.
2. Amazon ECR Security Controls
- Image scanning: Basic scanning (scan on push, CVEs in OS packages) or Enhanced scanning powered by Amazon Inspector (continuous scanning, OS and programming-language package vulnerabilities, rescans when new CVEs are published).
- Tag immutability: Prevents an existing tag from being overwritten, so a tested image cannot be silently replaced.
- Encryption at rest: AES-256 by default or KMS with a customer managed key.
- Repository policies and IAM policies restrict who can push or pull; use VPC endpoints (ecr.api, ecr.dkr and the S3 gateway endpoint) for private access.
- Lifecycle policies remove old, untagged or outdated images.
- Pull through cache lets you cache upstream public images in a private registry so they are scanned and controlled.
- Replication across Regions and accounts.
3. Image Signing and Admission Control
- AWS Signer with the Notation (Notary Project) client signs container images stored in ECR.
- On EKS, admission controllers such as Kyverno or OPA Gatekeeper verify signatures and block unsigned images or images from unapproved registries.
- On ECS, restrict task definitions to images from approved ECR repositories through IAM and pipeline checks.
4. Runtime Hardening
- ECS task definitions: readonlyRootFilesystem, non-root user, no privileged flag.
- EKS: Pod Security Standards (restricted profile), network policies, IRSA or EKS Pod Identity for least-privilege credentials.
- GuardDuty Runtime Monitoring for EKS, ECS (including Fargate) and EC2 detects suspicious behaviour in running containers and hosts.
Putting It Together: A Reference Pipeline
1. Source code and Dockerfile or Image Builder recipe committed to a repository.
2. Pipeline builds the image, applies CIS/STIG hardening, removes unused packages.
3. Inspector or ECR enhanced scanning evaluates vulnerabilities; the pipeline fails on critical or high findings.
4. Image is signed (containers) and tagged as approved; the AMI ID or image digest is published to Parameter Store.
5. Image is distributed to target accounts and Regions with KMS permissions configured.
6. SCPs, Allowed AMIs and admission controllers prevent launching unapproved images.
7. Config, Inspector, Security Hub and GuardDuty provide continuous detection.
8. Scheduled rebuilds pick up new patches; old images are deprecated through lifecycle policies.
Exam Tips: Answering Questions on Hardened AMIs and Container Images
1. Recognise the keywords. Phrases like golden image, standardised baseline, CIS benchmark, STIG, automate AMI creation or least operational overhead to build patched images point to EC2 Image Builder.
2. Prefer managed services over custom code. When two answers both work, the exam usually favours the managed option: Image Builder over hand-built Packer scripts on EC2, ECR enhanced scanning over a self-hosted scanner, Inspector over third-party agents.
3. Preventive vs detective. If the question says prevent launching unapproved AMIs, choose SCPs/IAM conditions on ec2:ImageId or ec2:Owner or the Allowed AMIs setting. If it says identify or report noncompliant instances, choose the AWS Config approved-amis-by-id/by-tag rules.
4. Sharing encrypted AMIs. Remember: AMIs encrypted with the default aws/ebs key cannot be shared. The correct answer involves re-encrypting with a customer managed KMS key and updating its key policy to grant the target account access, plus modifying the AMI launch permissions.
5. Patching strategy. For immutable or Auto Scaling workloads, the best answer is usually to rebuild the AMI and refresh instances (for example with an Auto Scaling instance refresh) rather than patching in place. Patch Manager fits long-lived, mutable servers.
6. Container scanning nuances. Basic scanning covers OS packages on push or manual trigger. Enhanced scanning (Inspector) adds language packages and continuous rescanning when new CVEs appear. If a scenario needs alerts on newly disclosed vulnerabilities in images already stored, pick enhanced scanning.
7. Integrity of images. If the scenario is about ensuring a tested image is not replaced, choose ECR tag immutability and deploying by digest. If it is about proving the image came from a trusted publisher, choose AWS Signer image signing with verification at deploy time.
8. Secrets in images are always wrong. Any answer that stores credentials, access keys or passwords in an AMI, user data in plain text or a Dockerfile should be eliminated. Prefer IAM roles (instance profiles, task roles, IRSA/Pod Identity) and Secrets Manager.
9. Untrusted sources. Public community AMIs or public Docker Hub images in a regulated environment are red flags. Look for answers using AWS-provided or verified Marketplace images, ECR pull through cache with scanning, and organisation-wide restrictions.
10. Accidental exposure. If an AMI or snapshot was made public by mistake, think of Block Public Access for AMIs and for EBS snapshots, and Security Hub/Config checks that detect public images.
11. Multi-account and multi-Region distribution. Image Builder distribution configurations handle copying and sharing to Organizations/OUs; pair with Parameter Store so consumers reference the latest approved ID.
12. Watch for IMDSv2. A hardened AMI scenario may mention credential theft via SSRF. Requiring IMDSv2 (on the AMI or launch template, enforced with SCP condition ec2:MetadataHttpTokens) is the expected control.
13. Runtime threats belong to GuardDuty. Hardened images reduce vulnerabilities, but detecting malicious activity in running containers or instances is a job for GuardDuty Runtime Monitoring and Malware Protection, not image scanning.
14. Eliminate by operational overhead. Many questions include the phrase most operationally efficient. Remove answers that require custom Lambda pipelines, manual approvals or EC2-hosted tooling when a native feature (Image Builder pipeline, ECR scanning, Config rule, SCP) achieves the same outcome.
Quick Summary
- Build: EC2 Image Builder with CIS/STIG components; minimal, non-root, multi-stage container builds.
- Scan: Amazon Inspector for AMIs and EC2; ECR basic or enhanced scanning for containers.
- Protect: KMS encryption, tag immutability, signing with AWS Signer, Block Public Access for AMIs.
- Distribute: Image Builder distribution, Parameter Store for latest IDs, customer managed KMS keys for cross-account sharing.
- Enforce: SCPs, IAM conditions, Allowed AMIs, EKS admission controllers.
- Detect: AWS Config approved-AMI rules, Security Hub, GuardDuty Runtime Monitoring.
- Maintain: Scheduled rebuilds, lifecycle policies, instance refresh for immutable updates.