1. Lifecycle Management for AI Development Environments in AWS ECR

    Python

    Managing the lifecycle of container images within an Amazon Elastic Container Registry (ECR) is a crucial task, especially for AI development environments where models and dependencies change frequently. With Pulumi, we can automate the creation of an ECR repository, as well as implement image lifecycle policies that help manage image retention by automatically removing old or unused images based on defined rules.

    In the code below, I'll show you how to create an ECR repository and attach a lifecycle policy to it using Pulumi and AWS. This lifecycle policy will ensure that only the most recent 5 images are kept in the repository, which can conserve storage and potentially reduce costs. This is a common policy for development environments where only a few latest images are used.

    Here's a detailed Pulumi Python program to set this up:

    import pulumi import pulumi_aws as aws # Create a new ECR repository to store the AI development images. ecr_repository = aws.ecr.Repository("ai_dev_repository", image_tag_mutability="MUTABLE", # Images within can have their tags changed. image_scanning_configuration=aws.ecr.RepositoryImageScanningConfigurationArgs( scan_on_push=True # Enable scanning of images on push for vulnerabilities. ) ) # Define the ECR lifecycle policy. # This given policy retains only the last 5 images. lifecycle_policy = """ { "rules": [ { "rulePriority": 1, "description": "Keep last 5 images", "selection": { "tagStatus": "any", "countType": "imageCountMoreThan", "countNumber": 5 }, "action": { "type": "expire" } } ] } """ # Apply the lifecycle policy to the repository. ecr_lifecycle_policy = aws.ecr.LifecyclePolicy("ai_dev_lifecycle_policy", policy=lifecycle_policy, repository=ecr_repository.name, # More details at: https://www.pulumi.com/registry/packages/aws/api-docs/ecr/lifecyclepolicy/ ) # Export the repository URL for easy access pulumi.export("repository_url", ecr_repository.repository_url) # Output the repository ARN for reference. pulumi.export("repository_arn", ecr_repository.arn)

    Here's an explanation of what's happening in the script:

    • We initiate a new ECR repository that will store the AI environment images using the pulumi_aws.ecr.Repository class. We've enabled image tag mutability and scanning on push, both useful features for a development environment.
    • We define a string that represents the lifecycle policy in JSON format. This policy uses predefined AWS ECR lifecycle policy rules to clean up images. In this scenario, our rule keeps the latest 5 images and expires the rest.
    • The lifecycle policy is then applied to the created ECR repository using the pulumi_aws.ecr.LifecyclePolicy resource.
    • Finally, we export the repository URL and ARN using the pulumi.export function to make them available outside of Pulumi for use in CI/CD pipelines or other processes.

    This program should be saved in a Python file (e.g., ecr_lifecycle_mgmt.py) and can then be deployed using the pulumi up command via the Pulumi CLI. Before running the command, you must ensure your AWS credentials are configured correctly for Pulumi to interact with your AWS account.