writing-skills
writing-skills
Use when creating new skills, editing existing skills, or verifying skills work before deployment
aws-ecr"Manages container image repositories with ECR for secure storage, scanning" replication, and integration with EKS, ECS, and Lambda for container deployments.
Independent GitHub snapshot · Automated classification · Original documentation governs
name: aws-ecr compatibility: opencode completeness: 95 content-types:
Manage container images securely with vulnerability scanning, lifecycle policies, replication, and integration with container orchestration services.
Use ECR when:
Avoid ECR for:
Primary Purpose: Provide secure, managed container image registry with vulnerability scanning, lifecycle management, and AWS service integration.
Common Use Cases:
AWSTemplateFormatVersion: '2010-09-09'
Resources:
# ECR Repository
ApplicationRepository:
Type: AWS::ECR::Repository
Properties:
RepositoryName: application-images
EncryptionConfiguration:
EncryptionType: KMS
KmsKey: !GetAtt RepositoryEncryptionKey.Arn
ImageScanningConfiguration:
ScanOnPush: true
LifecyclePolicy:
LifecyclePolicyText: |
{
"rules": [
{
"rulePriority": 1,
"description": "Keep last 10 images tagged, expire untagged",
"selection": {
"tagStatus": "tagged",
"tagPrefixList": ["v"],
"countType": "imageCountMoreThan",
"countNumber": 10
},
"action": {
"type": "expire"
}
},
{
"rulePriority": 2,
"description": "Expire untagged images after 7 days",
"selection": {
"tagStatus": "untagged",
"countType": "sinceImagePushed",
"countUnit": "days",
"countNumber": 7
},
"action": {
"type": "expire"
}
}
]
}
RepositoryPolicyText:
Version: '2012-10-17'
Statement:
- Sid: AllowECSTaskPull
Effect: Allow
Principal:
AWS: arn:aws:iam::123456789012:role/ecsTaskRole
Action:
- ecr:GetAuthorizationToken
- ecr:GetDownloadUrlForLayer
- ecr:BatchGetImage
- ecr:BatchCheckLayerAvailability
- Sid: AllowEKSNodesPull
Effect: Allow
Principal:
AWS: arn:aws:iam::123456789012:role/nodeGroupRole
Action:
- ecr:GetAuthorizationToken
- ecr:GetDownloadUrlForLayer
- ecr:BatchGetImage
- ecr:BatchCheckLayerAvailability
- Sid: DenyUnencryptedTransport
Effect: Deny
Principal: '*'
Action: 'ecr:*'
Condition:
Bool:
aws:SecureTransport: 'false'
Tags:
- Key: Application
Value: main
# KMS Key for Image Encryption
RepositoryEncryptionKey:
Type: AWS::KMS::Key
Properties:
Description: KMS key for ECR repository encryption
KeyPolicy:
Version: '2012-10-17'
Statement:
- Sid: Enable IAM User Permissions
Effect: Allow
Principal:
AWS: !Sub 'arn:aws:iam::${AWS::AccountId}:root'
Action: 'kms:*'
Resource: '*'
- Sid: Allow ECR to use key
Effect: Allow
Principal:
Service: ecr.amazonaws.com
Action:
- 'kms:Decrypt'
- 'kms:GenerateDataKey'
Resource: '*'
# ECR Lifecycle Policy for Cleanup
ImageCleanupFunction:
Type: AWS::Lambda::Function
Properties:
FunctionName: ecr-image-cleanup
Runtime: python3.11
Handler: index.handler
Role: !GetAtt CleanupRole.Arn
Code:
ZipFile: |
import boto3
import json
from datetime import datetime, timedelta
ecr = boto3.client('ecr')
def handler(event, context):
repo_name = 'application-images'
max_age_days = 30
# List images
response = ecr.list_images(repositoryName=repo_name)
to_delete = []
for image in response.get('imageIds', []):
if 'imageTag' not in image: # Untagged
image_detail = ecr.describe_images(
repositoryName=repo_name,
imageIds=[image]
)
push_time = image_detail['imageDetails'][0]['imagePushedAt']
age = datetime.now(push_time.tzinfo) - push_time
if age > timedelta(days=max_age_days):
to_delete.append(image)
if to_delete:
ecr.batch_delete_image(
repositoryName=repo_name,
imageIds=to_delete
)
return {
'statusCode': 200,
'deleted': len(to_delete)
}
CleanupRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: sts:AssumeRole
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
Policies:
- PolicyName: ECRAccess
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- ecr:ListImages
- ecr:DescribeImages
- ecr:BatchDeleteImage
Resource: !GetAtt ApplicationRepository.RepositoryUri
# CloudWatch Alarms
ScanFailuresAlarm:
Type: AWS::CloudWatch::Alarm
Properties:
AlarmName: ecr-scan-failures
MetricName: ImageScanFailures
Namespace: AWS/ECR
Statistic: Sum
Period: 3600
EvaluationPeriods: 1
Threshold: 1
ComparisonOperator: GreaterThanOrEqualToThreshold
Dimensions:
- Name: RepositoryName
Value: !Ref ApplicationRepository
Outputs:
RepositoryUri:
Value: !GetAtt ApplicationRepository.RepositoryUri
Description: ECR repository URI
RepositoryName:
Value: !Ref ApplicationRepository
Description: ECR repository name
Key Elements:
Resources:
# Replication Configuration
RegistryReplicationConfig:
Type: AWS::ECR::RegistryReplicationConfig
Properties:
Rules:
- Destinations:
- Region: eu-west-1
RegistryId: !Ref 'AWS::AccountId'
- Region: ap-southeast-1
RegistryId: !Ref 'AWS::AccountId'
RepositoryFilters:
- Filter: PREFIX_LIST
FilterValue:
- prod
Destinations:
- Region: eu-west-1
RegistryId: !Ref 'AWS::AccountId'
- Region: ap-southeast-1
RegistryId: !Ref 'AWS::AccountId'
Key Elements:
ECR + ECS enables:
ECR + EKS enables:
CodePipeline + ECR enables:
Lambda + ECR enables:
Problem: Cannot track versions; hard to rollback.
Solution:
Problem: Repository grows indefinitely; storage costs.
Solution:
Problem: Deploy vulnerable images; security breaches.
Solution:
Problem: Images stored unencrypted; data exposure risk.
Solution:
Problem: Unauthorized users can pull/push images.
Solution:
| Category | Best Practice |
|---|---|
| Tagging | Semantic versioning; commit SHA; no 'latest' |
| Scanning | Enable on push; block high-severity |
| Cleanup | Lifecycle policies; regular maintenance |
| Security | KMS encryption; secure transport only |
| Replication | Multi-region for disaster recovery |
| Skill | Purpose |
|---|---|
cncf-aws-eks | Container orchestration with images |
cncf-aws-iam | Repository access control |
cncf-aws-kms | Image encryption |
Assess Requirements — Understand the use case, scale, integration needs, and existing infrastructure. Checkpoint: Document requirements, constraints, and success criteria.
Design Architecture — Plan component interactions, data flow, and deployment strategy using cloud-native best practices. Checkpoint: Verify the architecture addresses all requirements and follows CNCF conventions.
Implement & Configure — Create manifests, configurations, and deployment scripts. Include resource limits, health checks, and observability hooks. Checkpoint: Validate all YAML against schema and test in a staging environment.
Deploy & Monitor — Apply manifests to the cluster, verify component health, and confirm observability is working. Checkpoint: Confirm all pods/services are running, probes passing, and metrics/alerts configured.
Authoritative documentation links for this skill's domain. The model follows markdown links at load time to resolve external references and inline content.