https://github.com/jameswoolfenden/terraform-aws-ecr
A terraform module to create a container registry (ECR - Elastic Container Registry) in AWS
https://github.com/jameswoolfenden/terraform-aws-ecr
aws ecr module terraform
Last synced: 6 months ago
JSON representation
A terraform module to create a container registry (ECR - Elastic Container Registry) in AWS
- Host: GitHub
- URL: https://github.com/jameswoolfenden/terraform-aws-ecr
- Owner: JamesWoolfenden
- License: apache-2.0
- Created: 2019-01-05T15:30:58.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2024-09-06T12:02:32.000Z (almost 2 years ago)
- Last Synced: 2025-10-07T11:46:06.891Z (9 months ago)
- Topics: aws, ecr, module, terraform
- Language: HCL
- Homepage:
- Size: 127 KB
- Stars: 1
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# terraform-aws-ecr
[](https://github.com/JamesWoolfenden/terraform-aws-ecr)
[](https://github.com/JamesWoolfenden/terraform-aws-ecr/releases/latest)
[](https://github.com/JamesWoolfenden/terraform-aws-ecr/releases/latest)

[](https://www.bridgecrew.cloud/link/badge?vcs=github&fullRepo=JamesWoolfenden%2Fterraform-aws-ecr&benchmark=CIS+AWS+V1.2)
[](https://github.com/pre-commit/pre-commit)
[](https://www.checkov.io/)
[](https://www.bridgecrew.cloud/link/badge?vcs=github&fullRepo=JamesWoolfenden%2Fterraform-aws-ecr&benchmark=INFRASTRUCTURE+SECURITY)
---
Terraform module to provision an AWS [`Elastic Container Registry`](https://aws.amazon.com/ecr/)
This registry is immutable, so you cannot push the "latest" container twice.
It's 100% Open Source and licensed under the [APACHE2](LICENSE).

## Usage
Include this repository as a module in your existing Terraform code:
```terraform
module ecr {
source = "github.com/JamesWoolfenden/terraform-aws-ecr"
version = "v0.2.30"
name = var.name
repositorypolicy = data.aws_iam_policy_document.allowlocals.json
}
```
## Costs
```text
Monthly cost estimate
Project: .
Name Monthly Qty Unit Monthly Cost
module.ecs.aws_efs_file_system.examplea
└─ Storage (standard) Cost depends on usage: $0.33 per GB-months
module.ecs.aws_elb.admin
├─ Classic load balancer 730 hours $21.46
└─ Data processed Cost depends on usage: $0.0084 per GB
module.ecs.aws_elb.appstore
├─ Classic load balancer 730 hours $21.46
└─ Data processed Cost depends on usage: $0.0084 per GB
module.ecs.aws_instance.docker_host
├─ Instance usage (Linux/UNIX, on-demand, t3.micro) 730 hours $8.61
├─ EC2 detailed monitoring 7 metrics $2.10
├─ CPU credits 0 vCPU-hours $0.00
└─ root_block_device
├─ Storage (magnetic) 100 GB-months $5.80
└─ I/O requests Cost depends on usage: $0.06 per 1M request
PROJECT TOTAL $59.44
```
### Logging-in to ECR
First get your account number with:
```shell
aws sts get-caller-identity
```
Then use that to login with:
_aws ecr get-login-password --region eu-west-2 | docker login --username AWS --password-stdin .dkr.ecr..amazonaws.com_
```shell
aws ecr get-login-password --region eu-west-2 | docker login --username AWS --password-stdin 481212720429.dkr.ecr.eu-west-2.amazonaws.com
```
Now build your Dockerfile
```Dockerfile
FROM alpine:3.5
```
```shell
$docker build .
$docker images ✔ │ 4s │ 11:41:46
REPOSITORY TAG IMAGE ID CREATED SIZE
bb8a493bc0a0 24 months ago 4MB
```
Then tag the image:
```shell
docker tag bb8a493bc0a0 481212720429.dkr.ecr.eu-west-2.amazonaws.com/node-terraform
```
WARNING:This used the "latest" tag up and as this repo is immutable you cannot use it twice.
```shell
docker push 481212720429.dkr.ecr.eu-west-2.amazonaws.com/node-terraform
docker push 481212720429.dkr.ecr.eu-west-2.amazonaws.com/node-terraform
Using default tag: latest
The push refers to repository [481212720429.dkr.ecr.eu-west-2.amazonaws.com/node-terraform]
f566c57e6f2d: Pushed
```
## Update Docker file
Add this to the Dockerfile:
```Dockerfile
RUN apk add --update py2-pip
```
Build it and get image - cce562ae9658
Tag it the same and try to push it:
```shell
docker tag cce562ae9658 481212720429.dkr.ecr.eu-west-2.amazonaws.com/node-terraform
docker push 481212720429.dkr.ecr.eu-west-2.amazonaws.com/node-terraform
The push refers to repository [481212720429.dkr.ecr.eu-west-2.amazonaws.com/node-terraform]
75867783c8d9: Pushed
f566c57e6f2d: Layer already exists
tag invalid: The image tag 'latest' already exists in the 'node-terraform' repository and cannot be overwritten because the repository is immutable.
```
## Requirements
No requirements.
## Providers
| Name | Version |
|------|---------|
| [aws](#provider\_aws) | n/a |
## Modules
No modules.
## Resources
| Name | Type |
|------|------|
| [aws_ecr_lifecycle_policy.cleanup](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ecr_lifecycle_policy) | resource |
| [aws_ecr_repository.repository](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ecr_repository) | resource |
| [aws_ecr_repository_policy.policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ecr_repository_policy) | resource |
## Inputs
| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| [common\_tags](#input\_common\_tags) | Implements the common tags scheme | `map(any)` | n/a | yes |
| [encryption\_type](#input\_encryption\_type) | n/a | `string` | `"KMS"` | no |
| [image\_tag\_mutability](#input\_image\_tag\_mutability) | For sone strange reason this is optional behaviour. | `string` | `"IMMUTABLE"` | no |
| [kms\_key](#input\_kms\_key) | n/a | `string` | n/a | yes |
| [name](#input\_name) | The name of the registry | `string` | n/a | yes |
| [repositorypolicy](#input\_repositorypolicy) | Pass this variable a Json Policy | `string` | n/a | yes |
| [scan\_on\_push](#input\_scan\_on\_push) | Security Scan your new container before you add it to the registry | `bool` | `true` | no |
## Outputs
| Name | Description |
|------|-------------|
| [ecr](#output\_ecr) | The full details of the repo |
| [ecr\_arn](#output\_ecr\_arn) | The Amazon resource name for the repository |
| [ecr\_repo\_name](#output\_ecr\_repo\_name) | The name of the repository |
| [ecr\_repository\_url](#output\_ecr\_repository\_url) | The URL of your new registry |
## Policy
This is the policy required to build this project:
The Terraform resource required is:
```golang
resource "aws_iam_policy" "terraform_pike" {
name_prefix = "terraform_pike"
path = "/"
description = "Pike Autogenerated policy from IAC"
policy = jsonencode({
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": [
"ec2:DescribeAccountAttributes"
],
"Resource": "*"
},
{
"Sid": "VisualEditor1",
"Effect": "Allow",
"Action": [
"ecr:CreateRepository",
"ecr:DeleteLifecyclePolicy",
"ecr:DeleteRepository",
"ecr:DeleteRepositoryPolicy",
"ecr:DescribeRepositories",
"ecr:GetLifecyclePolicy",
"ecr:GetRepositoryPolicy",
"ecr:ListTagsForResource",
"ecr:PutImageScanningConfiguration",
"ecr:PutLifecyclePolicy",
"ecr:SetRepositoryPolicy",
"ecr:TagResource",
"ecr:UntagResource"
],
"Resource": "*"
},
{
"Sid": "VisualEditor2",
"Effect": "Allow",
"Action": [
"kms:CreateGrant",
"kms:DescribeKey"
],
"Resource": "*"
}
]
})
}
```
## Information
A terraform module to create a container registry (ECR - Elastic Container Registry) in AWS.
There are two policies attached, a life-cycle policy which details how to look after the containers and a permission policy which has a default allow allusers in the account.
I use this to store the customised containers I use in Codebuild and for storing containers for Fargate.
## Related Projects
Check out these related projects.
- [terraform-aws-codebuild](https://github.com/jameswoolfenden/terraform-aws-codebuild) - Making a Build pipeline
## Help
**Got a question?**
File a GitHub [issue](https://github.com/jameswoolfenden/terraform-aws-ecr/issues).
## Contributing
### Bug Reports & Feature Requests
Please use the [issue tracker](https://github.com/jameswoolfenden/terraform-aws-ecr/issues) to report any bugs or file feature requests.
## Copyrights
Copyright © 2019-2022 James Woolfenden
## License
[](https://opensource.org/licenses/Apache-2.0)
See [LICENSE](LICENSE) for full details.
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
### Contributors
[![James Woolfenden][jameswoolfenden_avatar]][jameswoolfenden_homepage]
[James Woolfenden][jameswoolfenden_homepage]
[jameswoolfenden_homepage]: https://github.com/jameswoolfenden
[jameswoolfenden_avatar]: https://github.com/jameswoolfenden.png?size=150