Skip to content

Commit f4e3948

Browse files
committed
[Native K8s] Use <<-EOF syntax instead of an array of strings
1 parent 99547a8 commit f4e3948

File tree

2 files changed

+62
-54
lines changed

2 files changed

+62
-54
lines changed

terraform/k8s/cleanup/main.tf

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,28 @@ resource "null_resource" "cleanup" {
1010

1111
provisioner "remote-exec" {
1212
inline = [
13+
<<-EOF
1314
# Allow terraform to fail any of the following steps without exiting
14-
"set +e",
15+
set +e
1516
1617
# Uninstall the operator and remove the repo from the EC2 instance
17-
"echo \"LOG: Uninstalling CloudWatch Agent Operator\"",
18-
"helm uninstall --debug --namespace amazon-cloudwatch amazon-cloudwatch-operator --ignore-not-found",
19-
"echo \"LOG: Deleting CloudWatch Agent Operator repo from environment\"",
20-
"[ ! -e amazon-cloudwatch-agent-operator ] || sudo rm -r amazon-cloudwatch-agent-operator",
18+
echo "LOG: Uninstalling CloudWatch Agent Operator"
19+
helm uninstall --debug --namespace amazon-cloudwatch amazon-cloudwatch-operator --ignore-not-found
20+
echo "LOG: Deleting CloudWatch Agent Operator repo from environment"
21+
[ ! -e amazon-cloudwatch-agent-operator ] || sudo rm -r amazon-cloudwatch-agent-operator
2122
2223
# Delete sample app resources
23-
"echo \"LOG: Deleting sample app namespace\"",
24-
"kubectl delete namespace sample-app-namespace",
25-
"echo \"LOG: Deleting sample app deployment files\"",
26-
"[ ! -e frontend-service-depl.yaml ] || rm frontend-service-depl.yaml",
27-
"[ ! -e remote-service-depl.yaml ] || rm remote-service-depl.yaml",
28-
"sleep 10",
24+
echo "LOG: Deleting sample app namespace"
25+
kubectl delete namespace sample-app-namespace
26+
echo "LOG: Deleting sample app deployment files"
27+
[ ! -e frontend-service-depl.yaml ] || rm frontend-service-depl.yaml
28+
[ ! -e remote-service-depl.yaml ] || rm remote-service-depl.yaml
29+
sleep 10
2930
3031
# Print cluster state when done clean up procedures
31-
"echo \"LOG: Printing cluster state after cleanup\"",
32-
"kubectl get pods -A",
32+
echo "LOG: Printing cluster state after cleanup"
33+
kubectl get pods -A
34+
EOF
3335
]
3436
}
3537
}

terraform/k8s/deploy/main.tf

Lines changed: 47 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -23,66 +23,72 @@ resource "null_resource" "deploy" {
2323

2424
provisioner "remote-exec" {
2525
inline = [
26+
<<-EOF
2627
# Make the Terraform fail if any step throws an error
27-
"set -e",
28+
set -e
2829
2930
# Ensure environment is clean
30-
"echo \"LOG: Rerunning cleanup commands in case of cleanup failure in previous run\"",
31-
"helm uninstall --debug --namespace amazon-cloudwatch amazon-cloudwatch-operator --ignore-not-found",
32-
"kubectl delete namespace sample-app-namespace --ignore-not-found=true",
33-
"[ ! -e amazon-cloudwatch-agent-operator ] || sudo rm -r amazon-cloudwatch-agent-operator",
34-
"[ ! -e frontend-service-depl.yaml ] || rm frontend-service-depl.yaml",
35-
"[ ! -e remote-service-depl.yaml ] || rm remote-service-depl.yaml",
31+
echo "LOG: Rerunning cleanup commands in case of cleanup failure in previous run"
32+
helm uninstall --debug --namespace amazon-cloudwatch amazon-cloudwatch-operator --ignore-not-found
33+
kubectl delete namespace sample-app-namespace --ignore-not-found=true
34+
[ ! -e amazon-cloudwatch-agent-operator ] || sudo rm -r amazon-cloudwatch-agent-operator
35+
[ ! -e frontend-service-depl.yaml ] || rm frontend-service-depl.yaml
36+
[ ! -e remote-service-depl.yaml ] || rm remote-service-depl.yaml
3637
3738
# Clone and install operator onto cluster
38-
"echo \"LOG: Cloning operator repo\"",
39-
"git clone https://github.com/aws/amazon-cloudwatch-agent-operator -q",
40-
"cd amazon-cloudwatch-agent-operator/helm/",
41-
"echo \"LOG: Installing CloudWatch Agent Operator using Helm\"",
42-
"helm upgrade --install --debug --namespace amazon-cloudwatch amazon-cloudwatch-operator ./ --create-namespace --set region=us-east-1 --set clusterName=k8s-cluster-${var.test_id}",
39+
echo "LOG: Cloning operator repo"
40+
git clone https://github.com/aws/amazon-cloudwatch-agent-operator -q
41+
cd amazon-cloudwatch-agent-operator/helm/
42+
echo "LOG: Installing CloudWatch Agent Operator using Helm"
43+
helm upgrade --install --debug --namespace amazon-cloudwatch amazon-cloudwatch-operator ./ --create-namespace --set region=us-east-1 --set clusterName=k8s-cluster-${var.test_id}
4344
44-
"sleep 60", # wait for pods to exist before checking if they're ready
45-
"kubectl wait --for=condition=Ready pods --all --selector=app.kubernetes.io/name=amazon-cloudwatch-observability -n amazon-cloudwatch --timeout=60s",
46-
"kubectl wait --for=condition=Ready pods --all --selector=app.kubernetes.io/name=cloudwatch-agent -n amazon-cloudwatch --timeout=60s",
45+
# Wait for pods to exist before checking if they're ready
46+
sleep 60
47+
kubectl wait --for=condition=Ready pods --all --selector=app.kubernetes.io/name=amazon-cloudwatch-observability -n amazon-cloudwatch --timeout=60s
48+
kubectl wait --for=condition=Ready pods --all --selector=app.kubernetes.io/name=cloudwatch-agent -n amazon-cloudwatch --timeout=60s
4749
4850
# Create sample app namespace
49-
"echo \"LOG: Creating sample app namespace\"",
50-
"kubectl create namespace sample-app-namespace",
51+
echo "LOG: Creating sample app namespace"
52+
kubectl create namespace sample-app-namespace
5153
5254
# Set up secret to pull image with
53-
"echo \"LOG: Creating secret \"",
54-
"ACCOUNT=$(aws sts get-caller-identity --query 'Account' --output text)",
55-
"SECRET_NAME=ecr-secret",
56-
"TOKEN=`aws ecr --region=${var.aws_region} get-authorization-token --output text --query authorizationData[].authorizationToken | base64 -d | cut -d: -f2`",
55+
echo "LOG: Creating secret to access ECR images"
56+
ACCOUNT=$(aws sts get-caller-identity --query 'Account' --output text)
57+
SECRET_NAME=ecr-secret
58+
TOKEN=`aws ecr --region=${var.aws_region} get-authorization-token --output text --query authorizationData[].authorizationToken | base64 -d | cut -d: -f2`
5759
58-
"echo \"LOG: Deleting secret if it exists\"",
59-
"kubectl delete secret -n sample-app-namespace --ignore-not-found $SECRET_NAME",
60-
"echo \"LOG: Creating secret for pulling sample app ECR\"",
61-
"kubectl create secret -n sample-app-namespace docker-registry $SECRET_NAME \\",
62-
"--docker-server=https://$ACCOUNT.dkr.ecr.${var.aws_region}.amazonaws.com \\",
63-
"--docker-username=AWS \\",
64-
"--docker-password=\"$${TOKEN}\"",
60+
echo "LOG: Deleting secret if it exists"
61+
kubectl delete secret -n sample-app-namespace --ignore-not-found $SECRET_NAME
62+
echo "LOG: Creating secret for pulling sample app ECR"
63+
kubectl create secret -n sample-app-namespace docker-registry $SECRET_NAME \
64+
--docker-server=https://$ACCOUNT.dkr.ecr.${var.aws_region}.amazonaws.com \
65+
--docker-username=AWS \
66+
--docker-password="$${TOKEN}"
6567
6668
# Deploy sample app
67-
"echo \"LOG: Pulling sample app deployment files\"",
68-
"cd ~", # To ensure everything is downloaded into root directory so cleanup is easy
69-
"aws s3api get-object --bucket aws-appsignals-sample-app-prod-us-east-1 --key frontend-service-depl.yaml frontend-service-depl.yaml",
70-
"aws s3api get-object --bucket aws-appsignals-sample-app-prod-us-east-1 --key remote-service-depl.yaml remote-service-depl.yaml",
71-
"echo \"LOG: Applying sample app deployment files\"",
72-
"kubectl apply -f frontend-service-depl.yaml",
73-
"kubectl apply -f remote-service-depl.yaml",
69+
echo "LOG: Pulling sample app deployment files"
70+
71+
# cd to ensure everything is downloaded into root directory so cleanup is each
72+
cd ~
73+
aws s3api get-object --bucket aws-appsignals-sample-app-prod-us-east-1 --key frontend-service-depl.yaml frontend-service-depl.yaml
74+
aws s3api get-object --bucket aws-appsignals-sample-app-prod-us-east-1 --key remote-service-depl.yaml remote-service-depl.yaml
75+
echo "LOG: Applying sample app deployment files"
76+
kubectl apply -f frontend-service-depl.yaml
77+
kubectl apply -f remote-service-depl.yaml
7478
7579
# Expose sample app on port 30100
76-
"echo \"LOG: Exposing main sample app on port 30100\"",
77-
"kubectl expose deployment sample-app-deployment-${var.test_id} -n sample-app-namespace --type=\"NodePort\" --port 8080",
78-
"kubectl patch service sample-app-deployment-${var.test_id} -n sample-app-namespace --type='json' --patch='[{\"op\": \"replace\", \"path\": \"/spec/ports/0/nodePort\", \"value\":30100}]'",
80+
echo "LOG: Exposing main sample app on port 30100"
81+
kubectl expose deployment sample-app-deployment-${var.test_id} -n sample-app-namespace --type="NodePort" --port 8080
82+
kubectl patch service sample-app-deployment-${var.test_id} -n sample-app-namespace --type='json' --patch='[{"op": "replace", "path": "/spec/ports/0/nodePort", "value":30100}]'
7983
8084
# Wait for sample app to be reach ready state
81-
"kubectl wait --for=condition=Ready --request-timeout '5m' pod --all -n sample-app-namespace",
85+
kubectl wait --for=condition=Ready --request-timeout '5m' pod --all -n sample-app-namespace
8286
8387
# Emit remote service pod IP
84-
"echo \"LOG: Outputting remote service pod IP to SSM using put-parameter API\"",
85-
"aws ssm put-parameter --region ${var.aws_region} --name remote-service-ip --type String --overwrite --value $(kubectl get pod --selector=app=remote-app -n sample-app-namespace -o jsonpath='{.items[0].status.podIP}')",
88+
echo "LOG: Outputting remote service pod IP to SSM using put-parameter API"
89+
aws ssm put-parameter --region ${var.aws_region} --name remote-service-ip --type String --overwrite --value $(kubectl get pod --selector=app=remote-app -n sample-app-namespace -o jsonpath='{.items[0].status.podIP}')
90+
91+
EOF
8692
]
8793
}
8894
}

0 commit comments

Comments
 (0)