EcrBuilder¶
Bases: AbstractAWSResourceBuilder['EcrBuilder', EcrConfig]
Builder for creating AWS ECR repositories with organizational standards.
Creates secure ECR repositories with optional KMS encryption, automatic image scanning, and cross-account access policies. Supports lifecycle management and Parameter Store integration for cross-stack references.
Source code in mare_aws_common_lib/builders/ecr_builder.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 | |
Attributes¶
_resource_type = AWSResourceType.ECR
class-attribute
instance-attribute
¶
Functions¶
_add_image_retention_policy(repo)
¶
Add lifecycle rule to limit retained images.
Adds a lifecycle policy to keep only the specified number of most recent images, automatically cleaning up older images.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
repo
|
Repository
|
ECR repository to add the policy to |
required |
Source code in mare_aws_common_lib/builders/ecr_builder.py
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | |
_control_consistency()
¶
Validate builder configuration and internal state consistency.
Ensures repository base name is set and validates the complete configuration using the EcrConfig model.
Raises:
| Type | Description |
|---|---|
ValueError
|
If repository base name is not set |
ValidationError
|
If configuration validation fails |
Source code in mare_aws_common_lib/builders/ecr_builder.py
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 | |
_create_cross_account_policy_for_ecr_kms_key(principals)
¶
Add cross-account decrypt policy to the KMS key.
Grants specified principals permissions to decrypt images encrypted with the repository's KMS key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
principals
|
List[ArnPrincipal]
|
List of IAM principals to grant access to |
required |
Source code in mare_aws_common_lib/builders/ecr_builder.py
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 | |
_create_cross_account_policy_for_ecr_repos(principals)
¶
Add cross-account read access policy to the ECR repository.
Grants specified principals permissions to pull images and access repository metadata across accounts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
principals
|
List[ArnPrincipal]
|
List of IAM principals to grant access to |
required |
Source code in mare_aws_common_lib/builders/ecr_builder.py
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 | |
_create_ecr_repo(scope, repo_name, kms_key=None)
¶
Create the ECR repository with security best practices.
Creates repository with image scanning enabled, proper removal policy, and optional KMS encryption.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scope
|
Construct
|
CDK construct scope |
required |
repo_name
|
str
|
Full repository name |
required |
kms_key
|
Key
|
Optional KMS key for encryption |
None
|
Returns:
| Type | Description |
|---|---|
Repository
|
Configured ECR Repository instance |
Source code in mare_aws_common_lib/builders/ecr_builder.py
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | |
_create_kms_key(scope, name)
¶
Create a KMS key for ECR repository encryption.
Creates a customer-managed KMS key with proper alias and tagging for encrypting ECR repository images.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scope
|
Construct
|
CDK construct scope |
required |
name
|
str
|
Base name for the KMS key alias |
required |
Returns:
| Type | Description |
|---|---|
Key
|
Configured KMS Key instance |
Source code in mare_aws_common_lib/builders/ecr_builder.py
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | |
_create_repo_from_config(scope)
¶
Create ECR repository and optional KMS key from configuration.
Creates the repository with encryption, lifecycle policies, and Parameter Store entries. Returns both the repository and KMS key (if created).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scope
|
Construct
|
CDK construct scope |
required |
Returns:
| Type | Description |
|---|---|
Tuple[Repository, Key]
|
Tuple of (ECR Repository, KMS Key or None) |
Source code in mare_aws_common_lib/builders/ecr_builder.py
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | |
_get_principals()
¶
Get IAM principals for cross-account access policies.
Returns account root principals based on sharing configuration. Either current account only or all environment accounts.
Returns:
| Type | Description |
|---|---|
List[ArnPrincipal]
|
List of IAM principals for policy statements |
Source code in mare_aws_common_lib/builders/ecr_builder.py
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | |
_grant_encrypt_decrypt_for_kms_key(principals)
¶
Grant encrypt/decrypt permissions on KMS key to principals.
Uses CDK's built-in grant methods to provide comprehensive KMS access for image encryption operations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
principals
|
List[ArnPrincipal]
|
List of IAM principals to grant access to |
required |
Source code in mare_aws_common_lib/builders/ecr_builder.py
256 257 258 259 260 261 262 263 264 265 266 | |
_set_config()
¶
Create and validate the ECR repository configuration.
Merges the base builder configuration with ECR-specific parameters to create a validated EcrConfig object. The configuration includes repository naming, KMS encryption settings, image retention policies, cross-account sharing options, and other ECR-specific parameters required for repository creation.
The method combines
- Base configuration from self._builder_config (tags, environment, etc.)
- ECR-specific parameter: ecr_repo_base_name from self._ecr_repo_base_name
Raises:
| Type | Description |
|---|---|
ValidationError
|
If the EcrConfig validation fails. |
ValueError
|
If ecr_repo_base_name has not been set via set_ecr_repo_base_name() |
Side Effects
Sets self._config to a validated EcrConfig instance that can be accessed through the config property after this method completes.
Source code in mare_aws_common_lib/builders/ecr_builder.py
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 | |
build(scope)
¶
Build and return the configured ECR repository.
Creates an ECR repository with optional KMS encryption, cross-account access policies, and lifecycle rules. Automatically stores repository references in Parameter Store.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scope
|
Construct
|
CDK construct scope where the repository will be created |
required |
Returns:
| Type | Description |
|---|---|
Repository
|
Configured ECR Repository instance |
Raises:
| Type | Description |
|---|---|
ValidationError
|
If repository configuration validation fails |
ValueError
|
If required repository base name is not set |
Source code in mare_aws_common_lib/builders/ecr_builder.py
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | |
reset()
¶
Reset the builder to its initial state.
Clears all internal state including repository instance, KMS key, and repository base name. Called internally to prepare for a new build.
Source code in mare_aws_common_lib/builders/ecr_builder.py
24 25 26 27 28 29 30 31 32 33 | |
set_ecr_repo_base_name(name)
¶
Set the base name for the ECR repository.
The base name will be used to construct the final repository name following organizational naming conventions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Base name for the repository (e.g., 'my-app', 'data-processor') |
required |
Returns:
| Type | Description |
|---|---|
EcrBuilder
|
Self for method chaining |
Source code in mare_aws_common_lib/builders/ecr_builder.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 | |