Skip to content

Commit 7833fd0

Browse files
arcegacardenasLuis Arcega
andauthored
update: updating to templatefile function instead of template provider for troubleshooting module (#1595)
Co-authored-by: Luis Arcega <arcega@amazon.com>
1 parent 9f223e2 commit 7833fd0

File tree

4 files changed

+42
-64
lines changed

4 files changed

+42
-64
lines changed

manifests/modules/troubleshooting/pod/.workshop/terraform/deployment_image.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ spec:
3434
readOnlyRootFilesystem: true
3535
runAsNonRoot: true
3636
runAsUser: 1000
37-
image: "public.ecr.aws/aws-containers/retailing-store-sample-ui:1.0.0"
37+
image: "public.ecr.aws/aws-containers/retailing-store-sample-ui:1.2.1"
3838
imagePullPolicy: IfNotPresent
3939
ports:
4040
- name: http

manifests/modules/troubleshooting/pod/.workshop/terraform/main.tf

Lines changed: 25 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -67,19 +67,26 @@ resource "aws_iam_instance_profile" "ecr_ec2" {
6767
role = resource.aws_iam_role.ecr_ec2_role.name
6868
}
6969

70+
resource "aws_ecr_repository" "ui" {
71+
name = "retail-sample-app-ui"
72+
image_tag_mutability = "MUTABLE"
73+
force_delete = true
74+
75+
}
76+
7077
resource "aws_instance" "ui_to_ecr" {
7178
ami = data.aws_ssm_parameter.eks_ami.value
7279
instance_type = "m5.large"
7380
user_data = <<-EOF
7481
#!/bin/bash
75-
sudo yum update -y
76-
sudo yum install -y docker
77-
sudo service docker start
78-
sudo usermod -a -G docker ec2-user
79-
docker pull public.ecr.aws/aws-containers/retail-store-sample-ui:1.0.0
80-
docker images | grep retail-store | awk '{ print $3 }' | xargs -I {} docker tag {} ${resource.aws_ecr_repository.ui.repository_url}:1.0.0
81-
aws ecr get-login-password | docker login --username AWS --password-stdin ${data.aws_caller_identity.current.account_id}.dkr.ecr.${data.aws_region.current.id}.amazonaws.com
82-
docker push ${resource.aws_ecr_repository.ui.repository_url}:1.0.0
82+
sudo dnf update -y
83+
curl -L https://github.com/containerd/nerdctl/releases/download/v1.7.7/nerdctl-1.7.7-linux-amd64.tar.gz | sudo tar -xz -C /usr/local/bin
84+
sudo systemctl start containerd
85+
sudo systemctl enable containerd
86+
sudo /usr/local/bin/nerdctl pull public.ecr.aws/aws-containers/retail-store-sample-ui:1.2.1
87+
sudo /usr/local/bin/nerdctl images | grep retail-store | awk '{ print $3 }' | xargs -I {} sudo /usr/local/bin/nerdctl tag {} ${resource.aws_ecr_repository.ui.repository_url}:1.2.1
88+
aws ecr get-login-password | sudo /usr/local/bin/nerdctl login --username AWS --password-stdin ${data.aws_caller_identity.current.account_id}.dkr.ecr.${data.aws_region.current.id}.amazonaws.com
89+
sudo /usr/local/bin/nerdctl push ${resource.aws_ecr_repository.ui.repository_url}:1.2.1
8390
EOF
8491
subnet_id = element(data.aws_subnets.selected.ids, 0)
8592
iam_instance_profile = resource.aws_iam_instance_profile.ecr_ec2.name
@@ -89,12 +96,6 @@ resource "aws_instance" "ui_to_ecr" {
8996
depends_on = [resource.aws_ecr_repository.ui]
9097
}
9198

92-
resource "aws_ecr_repository" "ui" {
93-
name = "retail-sample-app-ui"
94-
image_tag_mutability = "MUTABLE"
95-
force_delete = true
96-
97-
}
9899

99100
data "aws_iam_policy_document" "private_registry" {
100101
statement {
@@ -132,31 +133,19 @@ resource "aws_ecr_repository_policy" "example" {
132133
depends_on = [resource.aws_instance.ui_to_ecr]
133134
}
134135

135-
data "template_file" "deployment_yaml1" {
136-
template = file("${path.module}/deployment_permissions.yaml.tpl")
137-
138-
vars = {
139-
image = "${resource.aws_ecr_repository.ui.repository_url}:1.0.0"
140-
}
141-
}
142-
143-
144-
resource "local_file" "deployment_yaml1" {
145-
filename = "${path.module}/deployment_permissions.yaml"
146-
content = data.template_file.deployment_yaml1.rendered
147-
}
148-
149136
resource "null_resource" "kustomize_app1" {
150137
triggers = {
151138
always_run = timestamp()
152139
}
153140

154141
provisioner "local-exec" {
155-
command = "kubectl apply -f ${path.module}/deployment_permissions.yaml"
156-
when = create
142+
command = "kubectl apply -f - <<EOF\n${templatefile("${path.module}/deployment_permissions.yaml.tpl", {
143+
image = "${resource.aws_ecr_repository.ui.repository_url}:1.2.1"
144+
})}\nEOF"
145+
when = create
157146
}
158147

159-
depends_on = [resource.local_file.deployment_yaml1, resource.aws_instance.ui_to_ecr]
148+
depends_on = [resource.aws_instance.ui_to_ecr]
160149
}
161150

162151

@@ -228,28 +217,17 @@ resource "aws_vpc_security_group_egress_rule" "allow_all_traffic_ipv4" {
228217
ip_protocol = "-1" # semantically equivalent to all ports
229218
}
230219

231-
data "template_file" "deployment_yaml2" {
232-
template = file("${path.module}/deployment_crash.yaml.tpl")
233-
234-
vars = {
235-
filesystemid = resource.aws_efs_file_system.efs.id
236-
}
237-
}
238-
239-
resource "local_file" "deployment_yaml2" {
240-
filename = "${path.module}/deployment_crash.yaml"
241-
content = data.template_file.deployment_yaml2.rendered
242-
}
243-
244220
resource "null_resource" "kustomize_app3" {
245221
triggers = {
246222
always_run = timestamp()
247223
}
248224

249225
provisioner "local-exec" {
250-
command = "kubectl apply -f ${path.module}/deployment_crash.yaml"
251-
when = create
226+
command = "kubectl apply -f - <<EOF\n${templatefile("${path.module}/deployment_crash.yaml.tpl", {
227+
filesystemid = resource.aws_efs_file_system.efs.id
228+
})}\nEOF"
229+
when = create
252230
}
253231

254-
depends_on = [resource.local_file.deployment_yaml2, resource.aws_efs_file_system.efs]
232+
depends_on = [resource.aws_efs_file_system.efs]
255233
}

website/docs/troubleshooting/pod/private_image.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,34 +40,34 @@ Events:
4040
Type Reason Age From Message
4141
---- ------ ---- ---- -------
4242
Normal Scheduled 5m15s default-scheduler Successfully assigned default/ui-private-7655bf59b9-jprrj to ip-10-42-33-232.us-west-2.compute.internal
43-
Normal Pulling 3m53s (x4 over 5m15s) kubelet Pulling image "1234567890.dkr.ecr.us-west-2.amazonaws.com/retail-sample-app-ui:1.0.0"
44-
Warning Failed 3m53s (x4 over 5m14s) kubelet Failed to pull image "1234567890.dkr.ecr.us-west-2.amazonaws.com/retail-sample-app-ui:1.0.0": failed to pull and unpack image "1234567890.dkr.ecr.us-west-2.amazonaws.com/retail-sample-app-ui:1.0.0": failed to resolve reference "1234567890.dkr.ecr.us-west-2.amazonaws.com/retail-sample-app-ui:1.0.0": unexpected status from HEAD request to https:/"1234567890.dkr.ecr.us-west-2.amazonaws.com/v2/retail-sample-app-ui/manifests/1.0.0: 403 Forbidden
43+
Normal Pulling 3m53s (x4 over 5m15s) kubelet Pulling image "1234567890.dkr.ecr.us-west-2.amazonaws.com/retail-sample-app-ui:1.2.1"
44+
Warning Failed 3m53s (x4 over 5m14s) kubelet Failed to pull image "1234567890.dkr.ecr.us-west-2.amazonaws.com/retail-sample-app-ui:1.2.1": failed to pull and unpack image "1234567890.dkr.ecr.us-west-2.amazonaws.com/retail-sample-app-ui:1.2.1": failed to resolve reference "1234567890.dkr.ecr.us-west-2.amazonaws.com/retail-sample-app-ui:1.2.1": unexpected status from HEAD request to https:/"1234567890.dkr.ecr.us-west-2.amazonaws.com/v2/retail-sample-app-ui/manifests/1.2.1: 403 Forbidden
4545
Warning Failed 3m53s (x4 over 5m14s) kubelet Error: ErrImagePull
4646
Warning Failed 3m27s (x6 over 5m14s) kubelet Error: ImagePullBackOff
47-
Normal BackOff 4s (x21 over 5m14s) kubelet Back-off pulling image "1234567890.dkr.ecr.us-west-2.amazonaws.com/retail-sample-app-ui:1.0.0"
47+
Normal BackOff 4s (x21 over 5m14s) kubelet Back-off pulling image "1234567890.dkr.ecr.us-west-2.amazonaws.com/retail-sample-app-ui:1.2.1"
4848
```
4949
5050
From the events of the pod, we can see the 'Failed to pull image' warning, with cause as 403 Forbidden. This indicates that the kubelet faced access denied while trying to pull the image used in the deployment. Let's get the URI of the image used in the deployment.
5151
5252
```bash
5353
$ kubectl get deploy ui-private -o jsonpath='{.spec.template.spec.containers[*].image}'
54-
"1234567890.dkr.ecr.us-west-2.amazonaws.com/retail-sample-app-ui:1.0.0"
54+
"1234567890.dkr.ecr.us-west-2.amazonaws.com/retail-sample-app-ui:1.2.1"
5555
```
5656
5757
### Step 3: Check the image reference
5858
5959
From the image URI, the image is referenced from the account where our EKS cluster is in. Let's check the ECR repository to see if any such image exists.
6060
6161
```bash
62-
$ aws ecr describe-images --repository-name retail-sample-app-ui --image-ids imageTag=1.0.0
62+
$ aws ecr describe-images --repository-name retail-sample-app-ui --image-ids imageTag=1.2.1
6363
{
6464
"imageDetails": [
6565
{
6666
"registryId": "1234567890",
6767
"repositoryName": "retail-sample-app-ui",
6868
"imageDigest": "sha256:b338785abbf5a5d7e0f6ebeb8b8fc66e2ef08c05b2b48e5dfe89d03710eec2c1",
6969
"imageTags": [
70-
"1.0.0"
70+
"1.2.1"
7171
],
7272
"imageSizeInBytes": 268443135,
7373
"imagePushedAt": "2024-10-11T14:03:01.207000+00:00",
@@ -78,10 +78,10 @@ $ aws ecr describe-images --repository-name retail-sample-app-ui --image-ids ima
7878
}
7979
```
8080
81-
The image path we have in deployment i.e. account_id.dkr.ecr.us-west-2.amazonaws.com/retail-sample-app-ui:1.0.0 have a valid registryId i.e. account-number, valid repositoryName i.e. "retail-sample-app-ui" and valid imageTag i.e. "1.0.0". Which confirms the path of the image is correct and is not a wrong reference.
81+
The image path we have in deployment i.e. account_id.dkr.ecr.us-west-2.amazonaws.com/retail-sample-app-ui:1.2.1 have a valid registryId i.e. account-number, valid repositoryName i.e. "retail-sample-app-ui" and valid imageTag i.e. "1.2.1". Which confirms the path of the image is correct and is not a wrong reference.
8282
8383
:::info
84-
Alternatively, you can also check from the ECR console. Click the button below to open the ECR Console. Then click on retail-sample-app-ui repository and the image tag 1.0.0.
84+
Alternatively, you can also check from the ECR console. Click the button below to open the ECR Console. Then click on retail-sample-app-ui repository and the image tag 1.2.1.
8585
<ConsoleButton
8686
url="https://us-west-2.console.aws.amazon.com/ecr/private-registry/repositories?region=us-west-2"
8787
service="ecr"

website/docs/troubleshooting/pod/public_image.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ Events:
4040
Type Reason Age From Message
4141
---- ------ ---- ---- -------
4242
Normal Scheduled 48s default-scheduler Successfully assigned default/ui-new-5654dd8969-7w98k to ip-10-42-33-232.us-west-2.compute.internal
43-
Normal BackOff 23s (x2 over 47s) kubelet Back-off pulling image "public.ecr.aws/aws-containers/retailing-store-sample-ui:1.0.0"
43+
Normal BackOff 23s (x2 over 47s) kubelet Back-off pulling image "public.ecr.aws/aws-containers/retailing-store-sample-ui:1.2.1"
4444
Warning Failed 23s (x2 over 47s) kubelet Error: ImagePullBackOff
45-
Normal Pulling 12s (x3 over 47s) kubelet Pulling image "public.ecr.aws/aws-containers/retailing-store-sample-ui:1.0.0"
46-
Warning Failed 12s (x3 over 47s) kubelet Failed to pull image "public.ecr.aws/aws-containers/retailing-store-sample-ui:1.0.0": rpc error: code = NotFound desc = failed to pull and unpack image "public.ecr.aws/aws-containers/retailing-store-sample-ui:1.0.0": failed to resolve reference "public.ecr.aws/aws-containers/retailing-store-sample-ui:1.0.0": public.ecr.aws/aws-containers/retailing-store-sample-ui:1.0.0: not found
45+
Normal Pulling 12s (x3 over 47s) kubelet Pulling image "public.ecr.aws/aws-containers/retailing-store-sample-ui:1.2.1"
46+
Warning Failed 12s (x3 over 47s) kubelet Failed to pull image "public.ecr.aws/aws-containers/retailing-store-sample-ui:1.2.1": rpc error: code = NotFound desc = failed to pull and unpack image "public.ecr.aws/aws-containers/retailing-store-sample-ui:1.2.1": failed to resolve reference "public.ecr.aws/aws-containers/retailing-store-sample-ui:1.2.1": public.ecr.aws/aws-containers/retailing-store-sample-ui:1.2.1: not found
4747
Warning Failed 12s (x3 over 47s) kubelet Error: ErrImagePull
4848
```
4949

@@ -55,31 +55,31 @@ Let's check the image used by the pod.
5555

5656
```bash
5757
$ kubectl get pod $POD -o jsonpath='{.spec.containers[*].image}'
58-
public.ecr.aws/aws-containers/retailing-store-sample-ui:1.0.0
58+
public.ecr.aws/aws-containers/retailing-store-sample-ui:1.2.1
5959
```
6060

6161
From the image URI, we can see that the image is referenced from public ECR repository of AWS.
6262

6363
### Step 4: Verify image existence
6464

65-
Let's check if image named retailing-store-sample-ui with tag 1.0.0 exists at [aws-containers ECR](https://gallery.ecr.aws/aws-containers). Search for the "retailing-store-sample-ui" and you will notice that no such image repository shows up. You can also easily verify the image existence in public ECR by using the image URI in a browser. In our case [image-uri](https://gallery.ecr.aws/aws-containers/retailing-store-sample-ui) will show a "Repository not found" message.
65+
Let's check if image named retailing-store-sample-ui with tag 1.2.1 exists at [aws-containers ECR](https://gallery.ecr.aws/aws-containers). Search for the "retailing-store-sample-ui" and you will notice that no such image repository shows up. You can also easily verify the image existence in public ECR by using the image URI in a browser. In our case [image-uri](https://gallery.ecr.aws/aws-containers/retailing-store-sample-ui) will show a "Repository not found" message.
6666

6767
![RepoDoesNotExist](assets/rep-not-found.webp)
6868

6969
### Step 5: Update the deployment with the correct image
7070

71-
To resolve the issue, we will have to update the deployment/pod spec with correct image reference. In our case it is public.ecr.aws/aws-containers/retail-store-sample-ui:1.0.0.
71+
To resolve the issue, we will have to update the deployment/pod spec with correct image reference. In our case it is public.ecr.aws/aws-containers/retail-store-sample-ui:1.2.1.
7272

7373
#### 5.1. Verify if image exists
7474

75-
Before we update the deployment, let's verify if this image exists using above mentioned method i.e. by visiting the [image-uri](https://gallery.ecr.aws/aws-containers/retail-store-sample-ui). You should be able to see the retail-store-sample-ui image with multiple tags available, including 1.0.0.
75+
Before we update the deployment, let's verify if this image exists using above mentioned method i.e. by visiting the [image-uri](https://gallery.ecr.aws/aws-containers/retail-store-sample-ui). You should be able to see the retail-store-sample-ui image with multiple tags available, including 1.2.1.
7676

7777
![RepoExist](assets/repo-found.webp)
7878

7979
#### 5.1. Update image in the deployment with correct reference
8080

8181
```bash
82-
$ kubectl patch deployment ui-new --patch '{"spec": {"template": {"spec": {"containers": [{"name": "ui", "image": "public.ecr.aws/aws-containers/retail-store-sample-ui:1.0.0"}]}}}}'
82+
$ kubectl patch deployment ui-new --patch '{"spec": {"template": {"spec": {"containers": [{"name": "ui", "image": "public.ecr.aws/aws-containers/retail-store-sample-ui:1.2.1"}]}}}}'
8383
deployment.apps/ui-new patched
8484
```
8585

0 commit comments

Comments
 (0)