Skip to content

Commit e6721ca

Browse files
committed
First iteration of ecr-mirror module
1 parent 3db22c1 commit e6721ca

File tree

5 files changed

+79
-2
lines changed

5 files changed

+79
-2
lines changed

README.md

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,42 @@
1-
# terraform-aws-ecr-mirror
2-
A Terraform module that performs a local docker pull and push to a given ECR repository
1+
# Terraform Module: AWS ECR Mirror
2+
3+
A Terraform module that performs a local docker pull and push to a given AWS ECR repository
4+
5+
## Use Case
6+
In late 2020, Docker Hub announced that the Hub service would begin limiting the rate at which images can be pulled under their anonymous and free plans.
7+
8+
The [AWS recommendation](https://aws.amazon.com/blogs/containers/advice-for-customers-dealing-with-docker-hub-rate-limits-and-a-coming-soon-announcement/) for those not wishing to upgrade to a paid plan is to mirror the Dockerhub image to their own AWS ECR repository.
9+
10+
This simple task requires a basic 'pull from Dockerhub-push to ECR' loop for which there exists no good bootstrapping solution (outside of a convolution such as a CodeBuild pipeline) for a new ECR repository that would look to use a Dockerhub image as its 'base' image which can then be used in subsequent builds without the pull limits.
11+
12+
This module is a simple terraform `local-exec` provisioner which runs the required awscli and docker push commands to ECR, and can be woven in to your existing set-up.
13+
14+
## Requirements
15+
16+
- aws-cli installed and configured with a named [profile](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-profiles.html) that has permissions to push to the desired ECR repository
17+
- [Docker installed](https://docs.docker.com/engine/install/) on the machine executing terraform, and permissions for the user executing terraform to run docker commands (e.g. by adding the user to the 'docker' user group)
18+
19+
## Usage Example
20+
21+
```
22+
module "ecr_mirror" {
23+
source = "./docker_init"
24+
aws_account_id = "123456544225"
25+
aws_region = "eu-west-1"
26+
docker_source = "wordpress:php7.4-apache"
27+
aws_profile = "default"
28+
ecr_repo_name = "php_wordpress"
29+
ecr_repo_tag = "base"
30+
}
31+
```
32+
33+
Consider adding an additional `depends_on` attribute if you are using this module in combination with a Terraform resource that also creates the ECR repository being pushed to.
34+
35+
## License
36+
Licensed under the Apache License, Version 2.0 (the "License").
37+
38+
You may obtain a copy of the License at apache.org/licenses/LICENSE-2.0.
39+
40+
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.
41+
42+
See the License for the specific language governing permissions and limitations under the License.

docker_pullpush.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
docker pull $1
2+
aws ecr get-login-password --region $2 --profile $4 | docker login --username AWS --password-stdin $3.dkr.ecr.$2.amazonaws.com
3+
docker tag $1 $3.dkr.ecr.eu-west-1.amazonaws.com/$5:$6
4+
docker push $3.dkr.ecr.eu-west-1.amazonaws.com/$5:$6

main.tf.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
resource "null_resource" "wordpress_pullpush" {
2+
3+
triggers = {
4+
shell_hash = sha256(var.docker_source)
5+
}
6+
provisioner "local-exec" {
7+
// ARGs to script are source image, region, AWS Account ID, aws_profile
8+
command = "${abspath(path.module)}/docker_pullpush.sh ${var.docker_source} ${var.aws_region} ${var.aws_account_id} ${var.aws_profile}"
9+
}
10+
}

outputs.tf

Whitespace-only changes.

variables.tf

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
variable "aws_region" {
2+
description = "The region in which the ECR repository is located."
3+
}
4+
5+
variable "docker_source" {
6+
description = "The source location of the Docker image being pulled."
7+
}
8+
9+
variable "aws_account_id" {
10+
description = "The AWS Account ID where the ECR repository is located."
11+
}
12+
13+
variable "aws_profile" {
14+
description = "The awscli profile name (located in the ~/.aws/credentials file) used to authenticate the ECR login and push."
15+
}
16+
17+
variable "ecr_repo_name" {
18+
description = "The name of the ECR repository being pushed to."
19+
}
20+
21+
variable "ecr_repo_tag" {
22+
description = "The tag of the ECR repository image being pushed."
23+
}

0 commit comments

Comments
 (0)