Skip to content
EgyKode
04 · Infrastructure as CodeLab 25 / 59
Guided labterraform

Amazon ECR Container Registry & S3 Storage Buckets

Stand up the registry your images live in and the buckets your application writes to, both private by default.

Time
23 min
Level
Intermediate
Objectives
4 objectives
Cost
Low cost

Where this fits in the platform

Before you start

CostLow cost

— 500 MB of ECR storage and 5 GB of S3 are free for 12 months. Container images are large, so delete the repository rather than leaving a few GB of layers behind.

Nothing to pay in the browser. Open the terminal runs this against a simulated cloud — the same API calls and the same commands, with no account and no bill. The figure above applies only if you build it in your own.

How to clean up

The scenario#

Images are pushed to Docker Hub with the tag latest, so nobody can say which commit is in production. Nothing scans them. The registry has eleven months of untagged layers nobody can delete safely because nobody knows what references them.

The buckets were created by hand, and one of them is public.

Hands-on environment

Run this lab in a real terminal, free and in your browser. The environment is temporary and yours alone — break it as much as you like.

Open the terminal

Opens in Killercoda, in a new tab — keep this page open for the steps.

Run it on AWS

This lab builds real cloud infrastructure, so it needs your own AWS account. Follow the cost and cleanup notes above — the resources are yours, and so is the bill.

Anything you tick here is your own record. EgyKode cannot see inside that terminal, so the success criteria stay self-assessed even when the environment checks your work for you.

A registry that scans and prunes

Step 1 of 4

What you are building#

Two storage services that get grouped together and behave nothing alike:

  • ECR — a private Docker registry with scanning on push and lifecycle rules for expiring images.
  • S3 — object storage, here for Terraform state and load balancer access logs.

Build it#

Verify it worked#

Terminal
# The scan ran, and you can read it
aws ecr describe-image-scan-findings \
  --repository-name platform/api --image-id imageTag="$SHA" \
  --query 'imageScanFindings.findingSeverityCounts'
 
# Immutable tags are enforced — this must FAIL
docker push "$REG/platform/api:$SHA"     # ImageTagAlreadyExistsException
 
# The lifecycle policy does what you think
aws ecr get-lifecycle-policy-preview --repository-name platform/api \
  --query 'previewResults[].{tag:imageTags[0],action:action.type}' --output table
 
# No bucket is public
aws s3api get-public-access-block --bucket "$(terraform output -raw state_bucket)"

get-lifecycle-policy-preview is worth knowing: it shows what the policy would expire without waiting for it to run.


Clean up#

DestructiveThis removes real resources. Check which environment you are in first.

Terminal
aws ecr delete-repository --repository-name platform/api --force
aws s3 rm "s3://$(terraform output -raw state_bucket)" --recursive
terraform destroy -auto-approve

Cost of this lab: Low. ECR is $0.10/GB-month and S3 about $0.023/GB — a few images and a state file are cents. The --force on the repository is required because it holds images.

Success criteria

0 of 4

The concept behind it

Ready to try it without help?Do the challenge

Next up

Lab 25 of 59 on the project path

Amazon RDS PostgreSQL & AWS Secrets Manager IntegrationRun a Multi-AZ database that survives losing an availability zone, with a password no human ever types.31 minIntermediate

Previous: IAM Roles, IRSA Policies & Security Groups