Skip to content

Commit 329a625

Browse files
authored
Release 0.0.8: Addition of architecture flag and outputs (#6)
* Addition of architecture flag and outputs
1 parent 0fb7586 commit 329a625

File tree

8 files changed

+204
-4
lines changed

8 files changed

+204
-4
lines changed

.header.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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 simple bootstrapping solution. The typical use case is 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 v2 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+
- Experimental mode enabled if you wish to use `architecture` argument. See [Leverage multi-CPU architecture support](https://docs.docker.com/desktop/multi-arch/) in Docker documentation.
19+
20+
## Idempotence
21+
22+
As this module is essentially running a series of bash commands, it ensures idempotence by triggering only when any of the values of the `docker_source`, `ecr_repo_name` or `ecr_repo_tag` variables are changed.
23+
24+
## Usage Example
25+
26+
```
27+
module "ecr_mirror" {
28+
source = "TechToSpeech/ecr-mirror/aws"
29+
version = "0.0.8"
30+
architecture = "linux/arm64/v8"
31+
aws_account_id = "123456544225"
32+
aws_region = "eu-west-1"
33+
docker_source = "wordpress:php7.4-apache"
34+
aws_profile = "default"
35+
ecr_repo_name = "php_wordpress"
36+
ecr_repo_tag = "base"
37+
}
38+
```
39+
40+
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.
41+
42+
## License
43+
Licensed under the Apache License, Version 2.0 (the "License").
44+
45+
You may obtain a copy of the License at apache.org/licenses/LICENSE-2.0.
46+
47+
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.
48+
49+
See the License for the specific language governing permissions and limitations under the License.

.terraform-docs.yml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
version: ""
3+
4+
formatter: <FORMATTER_NAME>
5+
6+
header-from: .header.md
7+
footer-from: ""
8+
9+
sections:
10+
hide: []
11+
show: []
12+
13+
content: |-
14+
{{ .Header }}
15+
{{ .Footer }}
16+
{{ .Inputs }}
17+
{{ .Modules }}
18+
{{ .Outputs }}
19+
{{ .Requirements }}
20+
{{ .Resources }}
21+
22+
output:
23+
file: ""
24+
mode: inject
25+
template: |-
26+
<!-- BEGIN_TF_DOCS -->
27+
{{ .Content }}
28+
<!-- END_TF_DOCS -->
29+
30+
output-values:
31+
enabled: false
32+
from: ""
33+
34+
sort:
35+
enabled: true
36+
by: name
37+
38+
settings:
39+
anchor: true
40+
color: true
41+
default: true
42+
description: false
43+
escape: true
44+
html: true
45+
indent: 2
46+
required: true
47+
sensitive: true
48+
type: true

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 0.0.8
4+
5+
Added support for overriding the desired platform architecture of the docker image. (Note: Experimental mode must be enabled on your local docker client)
6+
37
## 0.0.7
48

59
Added bash as command interpreter of local-exec.

README.md

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ As this module is essentially running a series of bash commands, it ensures idem
2525
```
2626
module "ecr_mirror" {
2727
source = "TechToSpeech/ecr-mirror/aws"
28+
version = "0.0.8"
29+
architecture = "linux/arm64/v8"
2830
aws_account_id = "123456544225"
2931
aws_region = "eu-west-1"
3032
docker_source = "wordpress:php7.4-apache"
@@ -43,4 +45,83 @@ You may obtain a copy of the License at apache.org/licenses/LICENSE-2.0.
4345

4446
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.
4547

46-
See the License for the specific language governing permissions and limitations under the License.
48+
See the License for the specific language governing permissions and limitations under the License.
49+
<!-- BEGIN_TF_DOCS -->
50+
# Terraform Module: AWS ECR Mirror
51+
52+
A Terraform module that performs a local docker pull and push to a given AWS ECR repository
53+
54+
## Use Case
55+
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.
56+
57+
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.
58+
59+
This simple task requires a basic 'pull from Dockerhub-push to ECR' loop for which there exists no simple bootstrapping solution. The typical use case is 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.
60+
61+
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.
62+
63+
## Requirements
64+
65+
- aws-cli v2 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
66+
- [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)
67+
- Experimental mode enabled if you wish to use `architecture` argument. See [Leverage multi-CPU architecture support](https://docs.docker.com/desktop/multi-arch/) in Docker documentation.
68+
69+
## Idempotence
70+
71+
As this module is essentially running a series of bash commands, it ensures idempotence by triggering only when any of the values of the `docker_source`, `ecr_repo_name` or `ecr_repo_tag` variables are changed.
72+
73+
## Usage Example
74+
75+
```
76+
module "ecr_mirror" {
77+
source = "TechToSpeech/ecr-mirror/aws"
78+
version = "0.0.8"
79+
architecture = "linux/arm64/v8"
80+
aws_account_id = "123456544225"
81+
aws_region = "eu-west-1"
82+
docker_source = "wordpress:php7.4-apache"
83+
aws_profile = "default"
84+
ecr_repo_name = "php_wordpress"
85+
ecr_repo_tag = "base"
86+
}
87+
```
88+
89+
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.
90+
91+
## License
92+
Licensed under the Apache License, Version 2.0 (the "License").
93+
94+
You may obtain a copy of the License at apache.org/licenses/LICENSE-2.0.
95+
96+
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.
97+
98+
See the License for the specific language governing permissions and limitations under the License.
99+
100+
## Inputs
101+
102+
| Name | Description | Type | Default | Required |
103+
|------|-------------|------|---------|:--------:|
104+
| <a name="input_architecture"></a> [architecture](#input\_architecture) | The override flag to pull an image of a specific architecture. e.g. `linux/arm64/v8` | `string` | `""` | no |
105+
| <a name="input_aws_account_id"></a> [aws\_account\_id](#input\_aws\_account\_id) | The AWS Account ID where the ECR repository is located. | `any` | n/a | yes |
106+
| <a name="input_aws_profile"></a> [aws\_profile](#input\_aws\_profile) | The awscli profile name (located in the ~/.aws/credentials file) used to authenticate the ECR login and push (Optional) | `string` | `""` | no |
107+
| <a name="input_aws_region"></a> [aws\_region](#input\_aws\_region) | The region in which the ECR repository is located. | `any` | n/a | yes |
108+
| <a name="input_docker_source"></a> [docker\_source](#input\_docker\_source) | The source location of the Docker image being pulled. | `any` | n/a | yes |
109+
| <a name="input_ecr_repo_name"></a> [ecr\_repo\_name](#input\_ecr\_repo\_name) | The name of the ECR repository being pushed to. | `any` | n/a | yes |
110+
| <a name="input_ecr_repo_tag"></a> [ecr\_repo\_tag](#input\_ecr\_repo\_tag) | The tag of the ECR repository image being pushed. | `any` | n/a | yes |
111+
## Modules
112+
113+
No modules.
114+
## Outputs
115+
116+
| Name | Description |
117+
|------|-------------|
118+
| <a name="output_ecr_repo_arn"></a> [ecr\_repo\_arn](#output\_ecr\_repo\_arn) | The repository URL with tag of the pushed image. |
119+
## Requirements
120+
121+
No requirements.
122+
## Resources
123+
124+
| Name | Type |
125+
|------|------|
126+
| [null_resource.docker_pullpush](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
127+
<!-- END_TF_DOCS -->

docker_pullpush.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ if [[ ${#6} -gt 0 ]]; then
44
profile="--profile $6"
55
fi
66

7-
docker pull $1
7+
if [[ ${#7} -gt 0 ]]; then
8+
platform="--platform $7"
9+
fi
10+
11+
docker pull $platform $1
812
aws ecr get-login-password --region $2 $profile | docker login --username AWS --password-stdin $3.dkr.ecr.$2.amazonaws.com
913
docker tag $1 $3.dkr.ecr.$2.amazonaws.com/$4:$5
1014
docker push $3.dkr.ecr.$2.amazonaws.com/$4:$5

main.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ locals {
55
resource "null_resource" "docker_pullpush" {
66

77
triggers = {
8-
shell_hash = sha256("${var.docker_source}${var.ecr_repo_name}${var.ecr_repo_tag}")
8+
shell_hash = sha256("${var.docker_source}${var.ecr_repo_name}${var.architecture}${var.ecr_repo_tag}")
99
}
1010
provisioner "local-exec" {
1111
interpreter = ["bash", "-c"]
12-
command = "${local.module_path}/docker_pullpush.sh ${var.docker_source} ${var.aws_region} ${var.aws_account_id} ${var.ecr_repo_name} ${var.ecr_repo_tag} ${var.aws_profile}"
12+
command = "${local.module_path}/docker_pullpush.sh ${var.docker_source} ${var.aws_region} ${var.aws_account_id} ${var.ecr_repo_name} ${var.ecr_repo_tag} ${var.aws_profile} ${var.architecture}"
1313
}
1414
}

outputs.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
output "ecr_repo_url" {
2+
value = "${var.aws_account_id}.dkr.ecr.${var.aws_region}.amazonaws.com/${var.ecr_repo_name}"
3+
description = "The repository URL of the pushed image."
4+
}
5+
6+
output "ecr_repo_url_tag" {
7+
value = "${var.aws_account_id}.dkr.ecr.${var.aws_region}.amazonaws.com/${var.ecr_repo_name}:${var.ecr_repo_tag}"
8+
description = "The repository URL with tag of the pushed image."
9+
}

variables.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,9 @@ variable "ecr_repo_name" {
2121

2222
variable "ecr_repo_tag" {
2323
description = "The tag of the ECR repository image being pushed."
24+
}
25+
26+
variable "architecture" {
27+
description = "The override flag to pull an image of a specific architecture. e.g. `linux/arm64/v8`"
28+
default = ""
2429
}

0 commit comments

Comments
 (0)